• 7.动态SQL


    Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。

    动态sql

    1.if.通过test属性中的表达式判断标签中的内容是否有效(是否会拼接到sql中) 

    2.where

    a.若where标签中有条件成立,会自动生成where关键字

    b.会自己将where标签中内容前多余的and去掉,但是其中内容后多余的and无法去掉

    c.若where标签中没有任何一个条件成立,则where没有任何功能

    3.trim

    prefix,suffix:在标签中内容前面或后面添加指定内容

    prefixOverrides,suffixOverrides:在标签中内容前面或后面去掉指定内容

    4.choose,when,otherwise

    相关于java中的if...else if...else

    when至少设置一个,otherwise最多设置一个

    5.foreach

    collection:设置要循环的数组或集合

    item:用一个字符串表示数组或集合中的每一个数据

    separator:设置每次循环的数据之间的分隔符

    open:循环的所有内容以什么开始

    close:循环的所有内容以什么结束

    6.sql片段

    可以记录一段sql,在需要用的地方使用include标签进行引用


               emp_id,emp_name,age,gender,dept_id

    select from t_emp

    ①.if 

    if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之 标签中的内容不会执行

    mapper接口: 

    1. /**
    2. * 根据条件查询员工信息
    3. * @param emp
    4. * @return
    5. */
    6. List getEmpListByCondition(Emp emp);

     mapper映射文件:

    1. <select id="getEmpListByConditionOne" resultType="Emp">
    2. select * from t_emp where 1 = 1
    3. <if test="empName != '' and empName != null">
    4. and emp_name = #{empName}
    5. if>
    6. <if test="age != '' and age != null">
    7. and age = #{age}
    8. if>
    9. <if test="gender != '' and gender != null">
    10. and gender = #{gender}
    11. if>
    12. select>

    测试类:

    1. @Test
    2. public void testGetEmpByCondition(){
    3. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    4. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
    5. Emp emp = new Emp(null, "", 20, "男");
    6. List list = mapper.getEmpListByCondition(emp);
    7. list.forEach(System.out::println);

    ②.where

    where和if一般结合使用:

    a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字 b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的 and去掉

    注意:where标签不能去掉条件最后多余的and 

    mapper映射文件: 

    1. <select id="getEmpByConditionTwo" resultType="Emp">
    2. select * from t_emp
    3. <where>
    4. <if test="empName != null and empName != ''">
    5. and emp_name = #{empName}
    6. if>
    7. <if test="age != null and age != ''">
    8. and age = #{age}
    9. if>
    10. <if test="gender != null and gender !=''">
    11. and gender = #{gender}
    12. if>
    13. where>
    14. select>

    测试类:

    1. @Test
    2. public void testGetEmpByCondition(){
    3. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    4. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
    5. Emp emp = new Emp(null, "", 20, "男");
    6. List list = mapper.getEmpListByCondition(emp);
    7. list.forEach(System.out::println);
    8. Emp emp2 = new Emp(null, "", null, "男");
    9. List list2 = mapper.getEmpListByCondition(emp2);
    10. list2.forEach(System.out::println);
    11. sqlSession.close();
    12. }

    ③.trim

    trim用于去掉或添加标签中的内容

    常用属性: prefix:在trim标签中的内容的前面添加某些内容

    prefixOverrides:在trim标签中的内容的前面去掉某些内容

    suffix:在trim标签中的内容的后面添加某些内容

    suffixOverrides:在trim标签中的内容的后面去掉某些内容

    mapper映射文件: 

    1. <select id="getEmpListByCondition" resultType="Emp">
    2. select * from t_emp
    3. <trim prefix="where" suffixOverrides="and">
    4. <if test="empName != '' and empName != null">
    5. emp_name = #{empName} and
    6. if>
    7. <if test="age != '' and age != null">
    8. age = #{age} and
    9. if>
    10. <if test="gender != '' and gender != null">
    11. gender = #{gender}
    12. if>
    13. trim>
    14. select>

    和上面测试类一样,这几个标签都可以处理多条件查询问题

    ④choose、when、otherwise

    mapper接口 

    1. /**
    2. * 使用choose查询员工信息
    3. * @param emp
    4. * @return
    5. */
    6. List getEmpByChoose(Emp emp);

    mapper映射文件 

    1. <select id="getEmpByChoose" resultType="Emp">
    2. select * from t_emp
    3. <where>
    4. <choose>
    5. <when test="empName != null and empName != ''">
    6. emp_name = #{empName}
    7. when>
    8. <when test="age != null and age != ''">
    9. age = #{age}
    10. when>
    11. <when test="gender != null and gender != ''">
    12. gender = #{gender}
    13. when>
    14. choose>
    15. where>
    16. select>

    测试类:

    1. @Test
    2. public void testGetEmpByChoose() {
    3. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    4. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
    5. Emp emp = new Emp(null, "张三", 20, "男");
    6. List list = mapper.getEmpChoose(emp);
    7. list.forEach(System.out::println);
    8. }

    choose、when、 otherwise相当于if...else if..else 

    ⑤.foreach

    实现批量添加:

    为什么推荐使用@Param因为它简单,你不设置可能要想这个东西它如何去获取,如果@Param之后,这个东西就不需要去思考了,直接就是这个注解的value属性值,就一定能访问到它

    mapper接口:

    1. /**
    2. * 批量添加员工信息
    3. * @param emps
    4. */
    5. void insertMoreEmp(@Param("emps") List emps);

     mapper映射文件:

    1. <insert id="insertMoreEmp" >
    2. insert into t_emp values
    3. <foreach collection="emps" item="emp" separator=",">
    4. (null,#{emp.empName},#{emp.age},#{emp.gender},null)
    5. foreach>
    6. insert>

    测试类:

    1. @Test
    2. public void testInsertMoreEmp(){
    3. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    4. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
    5. Emp emp1 = new Emp(null, "猫猫1", 20, "男");
    6. Emp emp2 = new Emp(null, "猫猫2", 20, "男");
    7. Emp emp3 = new Emp(null, "猫猫3", 20, "男");
    8. List list = Arrays.asList(emp1, emp2, emp3);
    9. mapper.insertMoreEmp(list);
    10. sqlSession.close();
    11. }

    实现批量删除:

    mapper接口:

    1. /**
    2. * 批量删除
    3. * @param empIds
    4. */
    5. void deleteMoreEmp(@Param("empIds") Integer[] empIds);

     mapper映射文件:

    方式一:

    1. <delete id="deleteMoreEmp">
    2. delete from t_emp where emp_id in
    3. <foreach collection="empIds" item="empId" separator="," open="(" close=")">
    4. #{empId}
    5. foreach>
    6. delete>

    方式二: 

    1. <delete id="deleteMoreEmp">
    2. delete from t_emp where
    3. <foreach collection="empIds" item="empId" separator="or">
    4. emp_id = #{empId}
    5. foreach>
    6. delete>

    测试类: 

    1. @Test
    2. public void testDeleteMoreEmp(){
    3. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    4. DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
    5. Integer[] empIds = new Integer[]{6,7};
    6. mapper.deleteMoreEmp(empIds);
    7. sqlSession.close();
    8. }

    ⑥:SQL片段

    sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入

    1. <sql id="empColumns">
    2. emp_id,emp_name,age,gender,dept_id
    3. sql>
    4. <select id="getEmpListByCondition" resultType="Emp">
    5. select <include refig="empColumns">include> from t_emp
    6. <if test="empName != '' and empName != null">
    7. and emp_name = #{empName}
    8. if>
    9. <if test="age != '' and age != null">
    10. and age = #{age}
    11. if>
    12. <if test="gender != '' and gender != null">
    13. and gender = #{gender}
    14. if>
    15. select>
  • 相关阅读:
    【刷题】代码随想录算法训练营第二十三天|669、修剪二叉搜索树,108、将有序数组转换为二叉搜索树,538、把二叉搜索树转换为累加树
    前端URL拼接路径参数
    系统介绍浏览器缓存机制及前端优化方案
    win11远程桌面用不了怎么解决
    (附源码)python方块新闻 毕业设计 091600
    初学python的感受
    vue2时间处理插件——dayjs
    cat()函数和print()函数的区别
    04 后端开发总结
    基于多云构建监控告警系统
  • 原文地址:https://blog.csdn.net/m0_56379670/article/details/127572109