spring和Mybatis的动态sql

文章目录

九、动态sql

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

9.1、if

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

java 复制代码
/**
 * 根据条件查询员工信息
 * @param emp
 * @return
 */
List<Emp> getEmpByCondition(Emp emp);
xml 复制代码
<!--List<Emp> getEmpByCondition(Emp emp);-->
<select id="getEmpByCondition" resultType="emp">
    select * from emp where 1=1
    <if test="empName != null and empName !=''">
      and emp_name = #{empName}
    </if>

    <if test="age != null and age !=''">
       and age = #{age}
    </if>

    <if test="sex != null and sex !=''">
       and sex = #{sex}
    </if>
</select>
java 复制代码
@Test
public void testGetEmpByCondition(){
    Emp emp = new Emp(null,null,22,null);
    List<Emp> emps = empMapper.getEmpByCondition(emp);
    emps.forEach(System.out::println);
}

9.2、where

where和if一般是结合使用

  1. 如果where标签中的if条件满足,则where标签会自动添加where关键字
  2. 会自动将条件最前方多余的and去掉,注意:where标签不能去掉条件最后多余的and
  3. 如果where标签中的if条件都不满足,则where标签没有任何功能,则不添加where关键字
xml 复制代码
<!-- List<Emp> getEmpByCondition(Emp emp);-->
<select id="getEmpByCondition" resultType="emp">
    select * from emp
    <where>
        <if test="empName != null and empName !=''">
            emp_name = #{empName} 
        </if>

        <if test="age != null and age !=''">
            and age = #{age} and
        </if>

        <if test="sex != null and sex !=''">
            and sex = #{sex}
        </if>
    </where>
</select>

9.3 trim

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

xml 复制代码
<!-- List<Emp> getEmpByCondition(Emp emp);-->
<!--
    prefix:在trim标签中的内容的前面添加某些内容
    prefixOverrides:在trim标签中的内容的前面去掉某些内容
    suffix:在trim标签中的内容的后面添加某些内容
    suffixOverrides:在trim标签中的内容的后面去掉某些内容
-->
<select id="getEmpByCondition" resultType="emp">
    select * from emp
    <trim prefix="where"  prefixOverrides="and">
        <if test="empName != null and empName !=''">
             emp_name = #{empName}
        </if>

        <if test="age != null and age !=''">
            and age = #{age}
        </if>

        <if test="sex != null and sex !=''">
            and   sex = #{sex}
        </if>
    </trim>
</select>

9.4、choose、when、otherwise

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

xml 复制代码
<!-- List<Emp> getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="emp">
    select * from emp
    <where>
        <choose>
            <when test="empName != null and empName !=''">
                emp_name = #{empName}
            </when>
            <when test="age != null and age !=''">
                and age = #{age}
            </when>
            <when test="sex != null and sex !=''">
                and sex = #{sex}
            </when>
        </choose>
    </where>
</select>

9.5、foreach

循环:批量,有规律

⑴批量的添加

java 复制代码
/**
 * 批量添加员工信息
 * @param emps
 */
void insertMoreEmp(@Param("emps") List<Emp> emps);
xml 复制代码
<!--
    collection:设置要循环的数组或者集合
    separator:设置每次循环的数据之间的分隔符
    index:用一个字符串表示数组或者集合中的每一个下标
    item:用一个字符串表示数组或者集合中的每一个数据
    open:循环的所有内容以什么开始
    close:循环的所有内容以什么结束
-->
<!--void insertMoreEmp(@Param("emps") List<Emp> emps);-->
<insert id="insertMoreEmp">
    insert into emp values
        <foreach collection="emps" item="emp" separator="," >
             (null,#{emp.empName},#{emp.age},#{emp.sex})
        </foreach>
</insert>
java 复制代码
@Test
public void testInsertMoreEmp(){
    Emp emp1 = new Emp(null,"小明",22,"男");
    Emp emp2 = new Emp(null,"小红",22,"男");
    Emp emp3 = new Emp(null,"小马",22,"男");
    List<Emp> emps = Arrays.asList(emp1,emp2,emp3);
    empMapper.insertMoreEmp(emps);
}

⑵批量删除

第一种

java 复制代码
/**
 * 批量删除员工信息
 * @param empIds
 * @return
 */
Integer deleteMoreEmp(@Param("empIds") Integer[] empIds);
xml 复制代码
<!--Integer deleteMoreEmp(@Param("empIds") Integer[] empIds);-->
<delete id="deleteMoreEmp">
    delete from emp where emp_id in(
        <foreach collection="empIds" item="empId" separator=",">
            #{empId}
        </foreach>
        )
</delete>
java 复制代码
@Test
public void testDeletMoreEmp(){
    Integer[] empIds = new Integer[]{10,11,12};
    Integer integer = empMapper.deleteMoreEmp(empIds);
    if(integer>0){
        System.out.println("删除成功");
    }else {
        System.out.println("删除失败");
    }
}

第二种

xml 复制代码
<!--Integer deleteMoreEmp(@Param("empIds") Integer[] empIds);-->
<delete id="deleteMoreEmp">
    delete from emp where emp_id in
    <foreach collection="empIds" item="empId" separator="," open="(" close=")">
        #{empId}
    </foreach>
</delete>

第三种

xml 复制代码
<!--Integer deleteMoreEmp(@Param("empIds") Integer[] empIds);-->
<delete id="deleteMoreEmp">
    delete from emp where
    <foreach collection="empIds" item="empId" separator="or" >
        emp_id = #{empId}
    </foreach>
</delete>

9.6、sql片段

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

xml 复制代码
<!--sql片段-->
<sql id="empColumns">
    emp_name,age,sex
</sql>

引用

xml 复制代码
<include refid="empColumns"></include>

{empId}

复制代码
### 9.6、sql片段

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

```xml
<!--sql片段-->
<sql id="empColumns">
    emp_name,age,sex
</sql>

引用

xml 复制代码
<include refid="empColumns"></include>
相关推荐
IKUN家族5 小时前
Spring MVC(一)
java·spring·mvc
wang090710 小时前
自己动手写一个spring之aop_1
java·后端·spring
小Ti客栈12 小时前
SpringBoot 事件发布机制:原理与应用
java·spring boot·spring
音视频工程实战13 小时前
PromptQL 新手入门与实战指南
数据库·sql·算法
清川渡水13 小时前
NL2SQL 的正确打开方式:从「AI 猜 SQL」到「置信度闭环」的工程化落地
数据库·人工智能·sql
ZYH_060114 小时前
InnoAI SQL 助手安装
linux·数据库·sql
IT界的老黄牛16 小时前
PostgreSQL 常用命令速查:MySQL 用户平滑上手,psql 元命令 + 库表管理 + 运维排查一篇通
sql·postgresql·psql·数据库运维·命令速查·mysql 对比
阿里云云原生16 小时前
AI Agent 上线容易稳定难?阿里云 AgentLoop 推出“经验自进化”闭环治理方案
人工智能·阿里云·mybatis·agentscope
XS03010617 小时前
Spring框架
java·后端·spring
名字还没想好☜18 小时前
Spring @Async 不生效排查:自调用失效、默认线程池坑与异步方法里的异常去哪了
java·python·spring·异步