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>
相关推荐
露天赏雪2 天前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
captain3762 天前
▲统一功能——DispatcherServlet源码分析
java·spring·java-ee·log4j·maven
骑着蜗牛撵大象3272 天前
MySQL 8.0 迁移后 SQL 变慢:执行计划里 3 类被忽略的成本差异
android·数据库·sql·mysql·优化器·执行计划
zhengfei6112 天前
Go-fastdfs /reload 未授权访问: 有效PoC
数据库·sql
大维2 天前
DBX 实测:从 SQLite 查询到 MiniMax-M3 自然语言生成 SQL
数据库·sql·sqlite
景煜拾年2 天前
InnoAI SQL 助手
运维·数据库·笔记·sql
TeamDev2 天前
使用 JxBrowser 在桌面应用中集成 Spring
java·前端·后端·spring·混合应用·jxbrowser·浏览器控件
SL_staff3 天前
从配置失效到逻辑复用:JVS-Logic 如何构建可治理的业务编排中枢
java·spring·产品
程序员吕洞宾3 天前
开源多维表格SmartTable v1.6.10 —— 用户名登录、数据评论及@人、表单分享二维码、性能优化、部署及升级优化
spring·性能优化·开源
liangbo73 天前
Spring系列03:Bean 的三种注册方式
java·spring