小言_互联网的博客

【MyBatis】| 动态SQL(重要)

545人阅读  评论(0)

目录

一: 动态SQL

1. if标签

2. where标签

3. trim标签

4. set标签

5. choose when otherwise

6. foreach标签

7. sql标签与include标签(了解)


一: 动态SQL

有的业务场景,也需要SQL语句进⾏动态拼接,例如:

①批量删除  

delete from t_car where id in(1,2,3,4,5,6,....这⾥的值是动态的,根据⽤户选择的id不同,值是不同的);

②多条件查询

select * from t_car where brand like '丰⽥%' and guide_price > 30 and .....;

前期准备:

创建模块:mybatis-009-dynamic-sql

打包⽅式:jar

引⼊依赖:mysql驱动依赖、mybatis依赖、junit依赖、logback依赖

pojo:com.powernode.mybatis.pojo.Car

mapper接⼝:com.powernode.mybatis.mapper.CarMapper

引⼊配置⽂件:mybatis-config.xml、jdbc.properties、logback.xml

mapper配置⽂件:com/powernode/mybatis/mapper/CarMapper.xml

编写测试类:com.powernode.mybatis.test.CarMapperTest

拷⻉⼯具类:com.powernode.mybatis.utils.SqlSessionUtil

需求:多条件查询

可能的条件包括:品牌(brand)、指导价格(guide_price)、汽⻋类型(car_type)

1. if标签

三兄弟之一:CarMapper接口,编写方法

参数有多个,使用@Param注解进行变量的定义,增强可读性


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 多条件查询,使用if
  7. List<Car> selectByMultiCondition (@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句

使用if标签,当条件满足就会把SQL语句拼接上去:

①if标签中test属性是必须的,test属性的值是true或者false。

②test属性可以使用的是:

第一:当使用了@Param注解,test中要出现的必须是@Param注解指定的参数名

第二:当没有使用@Param注解,test中出现的是:arg0、arg1或者param1、param2....

第三:当使用了POJO,test中出现的是POJO类的属性

③在MyBatis的动态SQL当中,不能使用&&,只能使用and。


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <select id="selectByMultiCondition" resultType="Car">
  7. select * from t_car where
  8. <if test="brand != null and brand != ''">
  9. brand like "%"#{brand}"%"
  10. </if>
  11. <if test="guidePrice != null and guidePrice !=''">
  12. and guide_price > #{guidePrice}
  13. </if>
  14. <if test="carType != null and carType != ''">
  15. and car_type = #{carType}
  16. </if>
  17. </select>
  18. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类

①假设三个参数都不为空,查询的结果完全没问题!

②假设三个参数都为空,就会出现问题,原来的SQL语句就变成:select * from t_car where,这是不符合语法的!怎么办?在where后面加上一个恒成立的条件 1=1

③假设第一个条件不为空,后两个两条都为空,那就是把第一条SQL语句直接拼上去,也会出现问题: select * from t_car where 1==1 brand like "%"?"%",两个条件中间需要一个and作为连接,所以第一条语句前面也要加上and


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testSelectByMultiCondition (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. // 假设三个条件都不为空
  14. List<Car> cars = mapper.selectByMultiCondition( "比亚迪", 2.0, "新能源");
  15. // 假设三个条件都为空
  16. List<Car> cars = mapper.selectByMultiCondition( "", null, "");
  17. // 假设第一个不为空,后两个为空
  18. List<Car> cars = mapper.selectByMultiCondition( "比亚迪", null, "");
  19. cars.forEach(car -> System.out.println(car));
  20. sqlSession.close();
  21. }
  22. }

所以最终的SQL语句修改为:增加1=1 和 and条件


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <select id="selectByMultiCondition" resultType="Car">
  7. select * from t_car where 1=1
  8. <if test="brand != null and brand != ''">
  9. and brand like "%"#{brand}"%"
  10. </if>
  11. <if test="guidePrice != null and guidePrice !=''">
  12. and guide_price > #{guidePrice}
  13. </if>
  14. <if test="carType != null and carType != ''">
  15. and car_type = #{carType}
  16. </if>
  17. </select>
  18. </mapper>

2. where标签

where标签的作⽤:让where⼦句更加动态智能 

①所有条件都为空时,where标签保证不会⽣成where⼦句。

②⾃动去除某些条件前⾯多余的and或or。

 三兄弟之一:CarMapper接口,编写方法

整个方法的结构不变,就改了一下方法名


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 使用where标签,让where子句更加的智能
  7. List<Car> selectByMultiConditionWithWhere (@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句

使用where标签专门处理where子句,所以我们就可以按照原来的思路进行SQL语句的编写

注:如果把and写在语句后面,不能去除掉,只能去除掉前面多余的and,例如:brand like "%"#{brand}"%" and


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <select id="selectByMultiConditionWithWhere" resultType="Car">
  7. select * from t_car
  8. <where>
  9. <if test="brand != null and brand != ''">
  10. brand like "%"#{brand}"%"
  11. </if>
  12. <if test="guidePrice != null and guidePrice !=''">
  13. and guide_price > #{guidePrice}
  14. </if>
  15. <if test="carType != null and carType != ''">
  16. and car_type = #{carType}
  17. </if>
  18. </where>
  19. </select>
  20. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类

①假设三个都为空,where标签就不会生成where子句

②假设第一个为空,后面两个都不为空,那么第二个带and的子句就会拼接上去,where标签会自动剔除掉多余的and


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testSelectByMultiConditionWithWhere (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. // 三个都为空
  14. List<Car> cars = mapper.selectByMultiCondition( "", null, "");
  15. // 假设第一个为空,后两个不为空
  16. List<Car> cars = mapper.selectByMultiCondition( "", 2.0, "新能源");
  17. cars.forEach(car -> System.out.println(car));
  18. sqlSession.close();
  19. }
  20. }

3. trim标签

trim标签的属性:

①prefix:在trim标签中的语句前添加内容(加前缀)

②suffix:在trim标签中的语句后添加内容 (加后缀)

③prefixOverrides:前缀覆盖掉(去掉前缀)

④suffixOverrides:后缀覆盖掉(去掉后缀)

 三兄弟之一:CarMapper接口,编写方法


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 使用trim标签
  7. List<Car> selectByMultiConditionWithTrim (@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句

不使用where标签,使用prefix和suffixOverrides属性来完成查询操作,并且我们把and放到后面,也同样能完成操作,例如:

①prefix="where",表示在trim标签所有内容的前面添加where

②suffixOverrides="and|or",表示把trim标签中内容的后缀and或or去掉


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <select id="selectByMultiConditionWithTrim" resultType="Car">
  7. select * from t_car
  8. <trim prefix="where" suffixOverrides="and|or">
  9. <if test="brand != null and brand != ''">
  10. brand like "%"#{brand}"%" and
  11. </if>
  12. <if test="guidePrice != null and guidePrice !=''">
  13. guide_price > #{guidePrice} and
  14. </if>
  15. <if test="carType != null and carType != ''">
  16. car_type = #{carType}
  17. </if>
  18. </trim>
  19. </select>
  20. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类

①三个都为空,也是完全没有问题的,前缀增加where也不是随便就加的,需要先判断子句有没有,有子句才会加where

②第一个不为空,其余都为空,这样where子句后缀就多了一个and,前面我们定义的是suffixOverrides="and|or",把trim标签中内容的后缀and或or去掉,也是完全没问题的


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testSelectByMultiConditionWithTrim (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. // 三个都不为空
  14. List<Car> cars = mapper.selectByMultiConditionWithTrim( "", null, "");
  15. // 第一个不为空,其它都为空
  16. List<Car> cars = mapper.selectByMultiConditionWithTrim( "比亚迪", null, "");
  17. cars.forEach(car -> System.out.println(car));
  18. sqlSession.close();
  19. }
  20. }

4. set标签

主要使⽤在update语句当中,⽤来⽣成set关键字,同时去掉最后多余的“,”

⽐如:我们只更新提交的不为空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新。

(1)测试使用最初的方式进行更新

 三兄弟之一:CarMapper接口,编写方法


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 通过id进行更新
  7. int updateById (Car car);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <update id="updateById">
  7. update t_car set
  8. car_num = #{carNum},
  9. brand = #{brand},
  10. guide_price = #{guidePrice},
  11. produce_time = #{produceTime},
  12. car_type = #{carType}
  13. where
  14. id = #{id}
  15. </update>
  16. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testUpdateById (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. Car car = new Car( 1L, null, null, null, null, "新能源");
  14. int count = mapper.updateById(car);
  15. System.out.println(count);
  16. sqlSession.commit();
  17. sqlSession.close();
  18. }
  19. }

问题:原来已经存在的数据,但是我们更新时并没有传数据进去,它会把原来已经存的数据更新为空!

(2)使用set标签进行更新

 三兄弟之一:CarMapper接口,编写方法


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 通过set标签进行更新
  7. int updateBySet (Car car);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句

这里的set标签实际上有两个功能:

①完成数据的更新,对于没有赋值的字段不会更新,只更新提交数据不为空的字段

②删除多余的逗号,比如:最后一个字段car_type为空,这样就会多余一个逗号出来,set标签可以去除掉这个多余的逗号


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <update id="updateBySet">
  7. update t_car
  8. <set>
  9. <if test="carNum != null and carNum !=''">car_num = #{carNum}, </if>
  10. <if test="brand != null and brand !=''">brand = #{brand}, </if>
  11. <if test="guidePrice != null and guidePrice !=''">guide_price = #{guidePrice}, </if>
  12. <if test="produceTime != null and produceTime !=''">produce_time = #{produceTime}, </if>
  13. <if test="carType != null and carType !=''">car_type = #{carType} </if>
  14. </set>
  15. where
  16. id = #{id}
  17. </update>
  18. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testUpdateBySet (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. Car car = new Car( 2L, null, "奔驰C200", null, null, "新能源");
  14. int count = mapper.updateBySet(car);
  15. System.out.println(count);
  16. sqlSession.commit();
  17. sqlSession.close();
  18. }
  19. }

更新结果如下,对于提交的数据是空或者"",这个字段将不会更新

5. choose when otherwise

这三个标签是在⼀起使⽤的,语法格式如下:


  
  1. <choose >
  2. < when > < / when >
  3. < when > < / when >
  4. < when > < / when >
  5. <otherwise > < /otherwise >
  6. < /choose >

 就等同于:


  
  1. if(){
  2. } else if(){
  3. } else if(){
  4. } else if(){
  5. } else{
  6. }

只有⼀个分⽀会被选择!

需求:先根据品牌查询,如果没有提供品牌,再根据指导价格查询,如果没有提供指导价格,就根据⽣产⽇期查询。

 三兄弟之一:CarMapper接口,编写方法


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 根据chose when otherwise
  7. List<Car> selectByChoose (@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句

三个条件:实际上是三选一,只要有其中一条执行了,其它分支就不会执行;所以都不需要加and


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <select id="selectByChoose" resultType="Car">
  7. select * form t_car
  8. <where>
  9. <choose>
  10. <when test="brand != null and brand !=''">
  11. brand like "%"#{brand}"%"
  12. </when>
  13. <when test="guidePrice != null and guidePrice !=''">
  14. guide_price > #{guidePrice}
  15. </when>
  16. <otherwise>
  17. car_type = #{carType}
  18. </otherwise>
  19. </choose>
  20. </where>
  21. </select>
  22. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类

①当三个条件都不为空,就按照第一个条件进行查询;当前面的条件为空,就依次按照后面的顺序进行查询

②特殊情况的三个条件都为空,实际上走的是最后otherwise标签的查询语句,只不过是把null传过去,查询不到数据而已


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testSelectByChoose (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. // 都不为空,按照第一个字段进行查询
  14. List<Car> cars = mapper.selectByChoose( "比亚迪", 2.0, "新能源");
  15. // 都为空,按照最后一个字段进行查询,把null传过去
  16. List<Car> cars = mapper.selectByChoose( "", null, "");
  17. cars.forEach(car -> System.out.println(car));
  18. sqlSession.close();
  19. }
  20. }

6. foreach标签

循环数组或集合,动态⽣成sql,⽐如这样的SQL:

批量删除


  
  1. delete from t_car where id in( 1, 2, 3);
  2. delete from t_car where id = 1 or id = 2 or id = 3;

 批量添加


  
  1. insert into t_car values
  2. ( null, '1001', '凯美瑞', 35.0, '2010-10-11', '燃油⻋'),
  3. ( null, '1002', '⽐亚迪唐', 31.0, '2020-11-11', '新能源'),
  4. ( null, '1003', '⽐亚迪宋', 32.0, '2020-10-11', '新能源')

(1) 批量删除

 三兄弟之一:CarMapper接口,编写方法

方法的参数是一个数组,这是第一次接触,重点掌握!


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 批量删除,foreach标签
  7. int deleteByIds (@Param("ids") Long[] ids);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句

(1)foreach标签的属性:

①collection:指定数组或集合

②item:代表数组或集合中的元素,其实就是变量

③separater:循环之间的分隔符,肯定是逗号,因为SQL语句是这样的形式:delete from t_car where id in(1,2,3);

④open:foreach循环拼接的所有SQL语句的最前面以什么开始

⑤close:foreach循环拼接的所有SQL语句的最后面以什么结束

(2)collection参数怎么传一个数组或者集合?直接把ids拿过来?这样会报错,错误信息是:【array,arg0】说明底层创建了一个Map集合存储数据,例如:map.put("array",数组)或者map.put("arg0",数组);所以参数要使用array或者arg0;但是为了可读性,上面使用了@Param注解,就可以直接使用ids了

(3)第一、二种方法使用的是in的方式,分隔符用的是" 逗号, ";第三种方法使用or的方式,分隔符实际上就是or


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <delete id="deleteByIds">
  7. <!--第一种方法-->
  8. delete from t_car where id in(
  9. <foreach collection="ids" item="id" separator=",">
  10. #{id}
  11. </foreach>
  12. )
  13. <!--第二种方法-->
  14. delete from t_car where id in
  15. <foreach collection="ids" item="id" separator="," open="(" close=")">
  16. #{id}
  17. </foreach>
  18. <!--第三种方法:使用or的形式-->
  19. delete from t_car where
  20. <foreach collection="ids" item="id" separator="or">
  21. id=#{id}
  22. </foreach>
  23. </delete>
  24. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类

准备一个对应的数组传过去,数组中的数据是我们要删除元素对应的id


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testDeleteByIds (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. // 准备一个数组
  14. Long[] ids = { 1L, 2L, 4L};
  15. int count = mapper.deleteByIds(ids);
  16. System.out.println(count);
  17. sqlSession.commit();
  18. sqlSession.close();
  19. }
  20. }

(2)批量插入

 三兄弟之一:CarMapper接口,编写方法

批量插入:参数不在是一个数组,而是一个List集合,并且使用@Param注解起别名


  
  1. package com.bjpowernode.mybatis.mapper;
  2. import com.bjpowernode.mybatis.pojo.Car;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface CarMapper {
  6. // 批量插入,一次插入多条Car信息,foreach标签
  7. int insertBatch (@Param("cars") List<Car> cars);
  8. }

三兄弟之二:CarMapper.xml文件,编写sql语句

①这里最主要的就是如何传参?我们使用的是集合里面的元素Car,所以参数就是Car对应的属性名,直接点就可以了。

②同样这里集合中的元素也是通过逗号的形式作为分隔符,所以separator的参数也是" , "


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.bjpowernode.mybatis.mapper.CarMapper">
  6. <insert id="insertBatch">
  7. insert into t_car values
  8. <foreach collection="cars" item="car" separator=",">
  9. (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
  10. </foreach>
  11. </insert>
  12. </mapper>

三兄弟之三:CarMappeTest类,用来编写测试类

准备Car对象的数据,然后把这些Car对象的数据添加到List集合当中,最后把这个List集合当做参数传到方法当中


  
  1. package com.bjpowernode.mybatis.test;
  2. import com.bjpowernode.mybatis.mapper.CarMapper;
  3. import com.bjpowernode.mybatis.pojo.Car;
  4. import com.bjpowernode.mybatis.utils.SqlSessionUtil;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.Test;
  7. import java.util.List;
  8. public class CarMapperTest {
  9. @Test
  10. public void testInsertBatch (){
  11. SqlSession sqlSession = SqlSessionUtil.openSession();
  12. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  13. // 准备数据
  14. Car car1 = new Car( null, "1200", "帕萨特1", 30.0, "2020-11-11", "燃油车");
  15. Car car2 = new Car( null, "1201", "帕萨特2", 30.0, "2020-11-11", "燃油车");
  16. Car car3 = new Car( null, "1202", "帕萨特3", 30.0, "2020-11-11", "燃油车");
  17. // 把数据添加到List集合当中
  18. List<Car> cars = new ArrayList<>();
  19. cars.add(car1);
  20. cars.add(car2);
  21. cars.add(car3);
  22. // 把集合传过去
  23. int count = mapper.insertBatch(cars);
  24. System.out.println(count);
  25. sqlSession.commit();
  26. sqlSession.close();
  27. }
  28. }

7. sql标签与include标签(了解)

sql标签:⽤来声明sql⽚段,有id属性作为唯一标识。 

include标签:调用refid属性(传sql标签的id值),将声明的sql⽚段包含到某个sql语句当中。

作⽤:代码复⽤,易维护。


  
  1. < ! --重复的代码抽取出来-->
  2. < sql id ="carColumnNameSql" >
  3. id,
  4. car_num as carNum,
  5. brand,
  6. guide_price as guidePrice,
  7. produce_time as produceTime,
  8. car_type as carType
  9. < / sql >
  10. < ! --代码复用-->
  11. < select id ="selectAll" resultType ="Car" >
  12. select
  13. <include refid ="carColumnNameSql" / >
  14. from t_car;
  15. < / select >


转载:https://blog.csdn.net/m0_61933976/article/details/128570677
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场