日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis笔记——配置文件完成增删改查

發布時間:2023/12/10 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis笔记——配置文件完成增删改查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

l 完成品牌數據的增刪改查操作

§ 要完成的功能列表清單:□ 查詢? 查詢所有數據? 查看詳情? 條件查詢□ 添加□ 修改? 修改全部字段? 修改動態字段□ 刪除? 刪除一個? 批量刪除準備環境:§ 數據庫表tb_brand drop table if exists tb_brand;CREATE TABLE tb_brand(-- id主鍵id int PRIMARY KEY auto_increment,-- 品牌名稱brand_name varchar(20),-- 企業名稱company_name varchar(20),-- 排序字段ordered int,-- 描述信息description varchar(100),-- 狀態:0:禁用 1:啟用status int );-- 添加數據 insert into tb_brand (brand_name, company_name, ordered, description, status) values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火',0), ('華為', '華為技術有限公司', 100, '華為致力于構建萬物互聯的智能世界',1), ('小米', '小米科技有限公司', 50, 'are you ok',1);select * from tb_brand; § 實體類Brand§ 測試用例§ 安裝MybatisX插件□ MybatisX是一款基于IDEA的快速開發插件,為效率而生。□ 主要功能:? XML和接口方法相互跳轉? 根據接口方法生成statrment□ 安裝:


接口:BrandMapper.java

import com.itheima.Pojo.Brand; import org.apache.ibatis.annotations.Param;import java.util.List; import java.util.Map;public interface BrandMapper {/*** 查詢所有* @return*/List<Brand> selectAll();/*** 查看詳情:根據id查詢*/Brand selectById(int id);//使用${}占位符時要加入注解 // Brand selectById(@Param("id") int id);/****查詢-多條件查詢* *參數接收* 1.散裝參數:如果方法有多個參數,需要@Param("SQL參數占位符名稱")* 2.Brand對象參數:對象的屬性名稱要和參數占位符名稱一致* 3.map集合參數:map集合的鍵名稱要和參數占位符名稱保持一致** @param status* @param companyName* @param brandName* @return*///參數包含所有的查詢條件//當存在多個參數時,方法有多個參數,需要@Param注解標注一下,參數需要傳遞給誰/*List<Brand> selectByCondition(@Param("status")int status,@Param("companyName")String companyName,@Param("brandName")String brandName);*///List<Brand> selectByCondition(Brand brand);List<Brand> selectByCondition(Map map);/*** 單條件動態查詢* @param brand* @return*/List<Brand> selectByConditionSingle(Brand brand);/*** 添加*/void add(Brand brand);/*** 修改*/int update(Brand brand);/*** 根據id刪除一行*/int deleteById(int id);/*** 根據id批量刪除*/int deleteByIds(@Param("ids")int[] ids); } BrandMapper.xml配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace:名稱空間 --> <mapper namespace="com.itheima.mapper.BrandMapper"><!--數據庫表的字段名稱 和 實體類的屬性名稱不一樣,則不能自動封裝*起別名:給不一樣的列名起別名,讓列名和實體類的屬性名一樣*缺點:每次查詢都要定義一次別名*<使用sql片段可以解決>*缺點:不靈活*resultMap:1.定義<resultMap>標簽2.<select>標簽中,使用resultMap屬性替換resultType--><resultMap id="brandResultMap" type="Brand"><!--id:完成主鍵字段的映射column:表的列名property:實體類的屬性名result:完成一般字段的映射column:表的列名property:實體類的屬性名--><result column="brand_name" property="brandName"/><result column="company_name" property="companyName"/></resultMap><!--查詢所有--><select id="selectAll" resultMap="brandResultMap">select *from tb_brand;</select><!--根據id查看詳情--><!--*參數占位符:1.#{}: 會將其替換成?2.${}: 拼sql,會存在sql注入問題3.使用時機*參數傳遞的時候:#{}*表名或者列名不固定的情況下:${}*參數類型:parameterType可以省略*特殊字符處理:1.轉義字符(&lt;)轉義"<"符號2.CDATA區--><select id="selectById" resultMap="brandResultMap">select *from tb_brand where id = #{id};</select><!--<select id="selectById" resultMap="brandResultMap">select * &#45;&#45; from tb_brand where id &lt; #{id};<![CDATA[<]]></select>--><!--查詢-多條件查詢--><!--<select id="selectByCondition" resultMap="brandResultMap">select *from tb_brandwhere status = #{status}and company_name like #{companyName}and brand_name like #{brandName};</select>--><!--動態條件查詢,動態SQL*if 條件判斷:*test:邏輯表達式*有個問題就是當第一個條件不成立并且后面條件成立時sql語句就變成了where后面加and xxx*解決方案:1.恒等式,在where后面加一個恒等式,并且在第一個條件的執行語句前加and2.<where>替換掉sql中的where關鍵字--><select id="selectByCondition" resultMap="brandResultMap">select *from tb_brand/*where 1 = 1*/<where><if test="status != null">and status = #{status}</if><if test="companyName != null and companyName != ''">and company_name like #{companyName}</if><if test="brandName != null and brandName != ''">and brand_name like #{brandName};</if></where></select><!--單條件動態查詢--><select id="selectByConditionSingle" resultMap="brandResultMap">select *from tb_brandwhere<choose> <!--相當于switch--><when test="status != null">/*相當于case*/status = #{status}</when><when test="companyName != null and companyName != ''">/*相當于case*/company_name like #{companyName}</when><when test="brandName != null and brandName != ''">/*相當于case*/brand_name like #{brandName}</when><otherwise> <!--相當于default-->1=1</otherwise></choose></select><!--添加--><insert id="add" useGeneratedKeys="true" keyProperty="id">insert into tb_brand(brand_name, company_name, ordered, description, status)value (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});</insert><!--修改--><!--<update id="update">update tb_brandset brand_name = #{brandName},company_name = #{companyName},ordered = #{ordered},description = #{description},status = #{status}where id = #{id};</update>--><!--動態修改--><update id="update">update tb_brand<set><if test="brandName != null and brandName != ''">brand_name = #{brandName},</if><if test="companyName != null and companyName != ''">company_name = #{companyName},</if><if test="ordered != null">ordered = #{ordered},</if><if test="description != null and description != ''">description = #{description},</if><if test="status != null">status = #{status}</if></set>where id = #{id};</update><!--刪除一個--><delete id="deleteById">delete from tb_brand where id = #{id};</delete><!--批量刪除--><delete id="deleteByIds">delete from tb_brand where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete><!--sql片段--><!--<sql id="brand_column">id, brand_name as brandName, company_name as companyName, ordered, description, status</sql><select id="selectAll" resultType="Brand">select<include refid="brand_column"/>from tb_brand;</select>&lt;!&ndash;statement&ndash;&gt;<select id="selectAll" resultType="Brand">select *from tb_brand;</select>--></mapper> 測試代碼: import com.itheima.Pojo.Brand; import com.itheima.mapper.BrandMapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test;import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map;public class MybatisTest {/*** 測試練習:查詢所有數據** @throws Exception*/@Testpublic void testSelectAll() throws Exception {//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession();//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句List<Brand> brands = brandMapper.selectAll();System.out.println(brands);//5.釋放資源sqlSession.close();}/*** 測試練習:查看詳情,根據id查詢** @throws Exception*/@Testpublic void testSelectById() throws Exception {//定義局部變量接收參數int id = 1;//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession();//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句Brand brand = brandMapper.selectById(id);System.out.println(brand);//5.釋放資源sqlSession.close();}/*** 查詢-多條件查詢** @throws Exception*/@Testpublic void testSelectByCondition() throws Exception {//定義局部變量接收參數int status = 1;String companyName = "華為";String brandName = "華為";//處理參數//采用模糊查詢需要對輸入的參數進行處理companyName = "%" + companyName + "%";brandName = "%" + brandName + "%";//封裝對象/*Brand brand = new Brand();brand.setStatus(status);brand.setCompanyName(companyName);brand.setBrandName(brandName);*///創建map集合,將map集合作為參數傳入Map<Object, Object> map = new HashMap<Object, Object>();map.put("status", status); // map.put("companyName", companyName); // map.put("brandName", brandName);//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession();//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句//散裝參數//List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);//Brand對象參數//List<Brand> brands = brandMapper.selectByCondition(brand);//Map集合參數List<Brand> brands = brandMapper.selectByCondition(map);System.out.println(brands);//5.釋放資源sqlSession.close();}/*** 查詢-單條件查詢** @throws Exception*/@Testpublic void testSelectByConditionSingle() throws Exception {//定義局部變量接收參數int status = 1;String companyName = "華為";String brandName = "華為";//處理參數//采用模糊查詢需要對輸入的參數進行處理companyName = "%" + companyName + "%";brandName = "%" + brandName + "%";//封裝對象Brand brand = new Brand();brand.setStatus(status);brand.setCompanyName(companyName);brand.setBrandName(brandName);//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession();//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句//散裝參數//List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);//Brand對象參數List<Brand> brands = brandMapper.selectByConditionSingle(brand);System.out.println(brands);//5.釋放資源sqlSession.close();}/*** 添加** @throws Exception*/@Testpublic void testAdd() throws Exception {//定義局部變量接收參數int status = 1;String companyName = "香飄飄食品有限公司";String brandName = "香飄飄";int ordered = 100;String description = "一年銷量繞地球三圈";// //處理參數 // //采用模糊查詢需要對輸入的參數進行處理 // companyName = "%" + companyName + "%"; // brandName = "%" + brandName + "%";//封裝對象Brand brand = new Brand();brand.setBrandName(brandName);brand.setCompanyName(companyName);brand.setOrdered(ordered);brand.setDescription(description);brand.setStatus(status);//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession(true);//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句brandMapper.add(brand);Integer i = brand.getId();System.out.println(i);//提交事務 // sqlSession.commit(true);//5.釋放資源sqlSession.close();}/*** 修改** @throws Exception*/@Testpublic void testUpdate() throws Exception {//定義局部變量接收參數int id = 11;int status = 1;String companyName = "香飄飄食品有限公司";String brandName = "香飄飄";int ordered = 200;String description = "香飄飄超好喝,一年銷量繞地球三圈";// //處理參數 // //采用模糊查詢需要對輸入的參數進行處理 // companyName = "%" + companyName + "%"; // brandName = "%" + brandName + "%";//封裝對象Brand brand = new Brand();brand.setId(id); // brand.setBrandName(brandName); // brand.setCompanyName(companyName);brand.setOrdered(ordered); // brand.setDescription(description); // brand.setStatus(status);//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession(true);//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句int count = brandMapper.update(brand);System.out.println(count);//提交事務 // sqlSession.commit(true);//5.釋放資源sqlSession.close();}/*** 根據id刪除** @throws Exception*/@Testpublic void testDeleteById() throws Exception {//定義局部變量接收參數int id = 11;//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession(true);//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句int count = brandMapper.deleteById(id);System.out.println(count);//提交事務 // sqlSession.commit(true);//5.釋放資源sqlSession.close();}/*** 根據id批量刪除** @throws Exception*/@Testpublic void testDeleteByIds() throws Exception {//定義局部變量接收參數int[] ids = {1, 2};//1.加載mybatis核心配置文件,獲取SqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.獲取SqlSession對象,用它執行sqlSqlSession sqlSession = sqlSessionFactory.openSession(true);//3.獲取接口代理對象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.執行方法即執行sql語句int count = brandMapper.deleteByIds(ids);System.out.println(count);//提交事務 // sqlSession.commit(true);//5.釋放資源sqlSession.close();} }

總結

以上是生活随笔為你收集整理的MyBatis笔记——配置文件完成增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产专区一区二区三区 | 成人小说亚洲一区二区三区 | 囯产精品久久久久久 | 性欧美长视频 | 亚洲AV无码成人精品区在线观 | 国产一区导航 | 日韩r级在线观看 | 自拍偷拍亚洲图片 | 亚洲av乱码一区二区 | www嫩草 | 欧美人妖另类 | 中文字幕久久久久 | 手机在线看黄色 | 天天想你在线观看完整版电影高清 | a视频| 欧美人与禽性xxxxx杂性 | 日韩不卡在线观看 | 成人黄色三级 | 欧美另类z0zx974| 超碰在线91 | asian性开放少妇pics | 欧美精品成人一区二区三区四区 | 美女啪啪无遮挡 | 500福利视频导航 | 男人天堂av网 | 一本久道久久综合无码中文 | 亚洲午夜一区二区三区 | 国产操 | av女星全部名单 | av一区二区三区四区 | 亚欧美在线观看 | 午夜精品久久久久久久99热浪潮 | 日本成人在线免费视频 | 天天曰夜夜操 | 国产亚洲精久久久久久无码77777 | 午夜av中文字幕 | 毛片视频网址 | av日韩国产 | 一区二区三区在线播放视频 | 香蕉视频日本 | 亚洲黄色小说图片 | 成人蜜桃av | 99热这里有精品 | 亚洲成人免费在线观看 | 性欧美精品男男 | 欧美丝袜一区二区三区 | 欧美一区二区大片 | av三级在线播放 | 香蕉视频18 | 国产一区二区三区四区五区在线 | 久久精品中文字幕 | 午夜av剧场 | 91视频在线免费看 | www.天堂av.com | 高清中文字幕在线a片 | 亚洲阿v天堂 | 日本不卡一区二区 | 玖玖爱国产 | 亚洲性天堂 | 亚洲综合五区 | 久久久午夜影院 | 一区二区在线观看免费 | 午夜免费一区 | 久久久精品影视 | 手机在线成人av | japanese国产在线| 天天干夜夜添 | 日本成人毛片 | 日韩欧美精品一区二区三区 | 中文资源在线播放 | 成人污在线观看 | 绿帽人妻精品一区二区 | 久久精品伦理 | 久久麻豆精品 | 欧美 日韩 国产 成人 在线 91 | 黄色一级大片免费看 | 色欲久久久天天天综合网精品 | 成人免费一区二区三区在线观看 | 国产又粗又猛又黄又爽 | xxxx少妇| 夜夜爽妓女8888视频免费观看 | 92久久 | 丝袜 亚洲 另类 欧美 重口 | 红猫大本营在线观看的 | 30一40一50老女人毛片 | 国产一区二区三区四区在线观看 | 韩国一级片在线观看 | 美攻壮受大胸奶汁(高h) | 婷婷五月花 | 亚洲自拍电影 | 国产三级视频网站 | 美女视频一区二区 | 99久久精品国产成人一区二区 | 亚洲天堂一区二区三区四区 | 亚洲色图网址 | 久久色在线视频 | 国产99在线视频 | 国产精品96| 九月婷婷 |