• 【Mybatis】动态sql


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

    if

    if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中

    <!--List<emp> getEmpByCondition(Emp emp);-->
        <!-- if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中 -->
        <select id="getEmpByConditionOne" resultType="Emp">
            select * from t_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>
            <if test="email !=null and email !=''">
               and  email=#{email}
            </if>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    where

    where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现

    <!-- where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现-->
        <select id="getEmpByConditionTwo" resultType="Emp">
            select * from t_emp
            <where>
                <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>
                <if test="email !=null and email !=''">
                    and  email=#{email}
                </if>
            </where>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    trim

    prefix/suffix:将trim标签中内容前面或后面添加指定内容
    prefixOverrides/suffixOverrides:将trim标签中内容前面或后面去掉指定内容
    若标签中没有内容:trim标签也没有任何效果

     <select id="getEmpByCondition" resultType="Emp">
            select * from t_emp
            <trim prefix="where" suffixOverrides="and|or">
                <if test="empName !=null and empName !='' ">
                    emp_name=#{empName} and
                </if>
                <if test="age !=null and age !=''">
                    age=#{age}  and
                </if>
                <if test="sex !=null and sex!=''">
                  sex=#{sex}   and
                </if>
                <if test="email !=null and email !=''">
                   email=#{email}  and
                </if>
            </trim>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    choose、when、otherwise

    choose:
    when:
    otherwise:
    choose标签为父标签,when与otherwise需要写在choose中
    这一套标签相当于Java中的if,else if else,when相当于if,else if ,otherwise相当于else
    when至少要有一个,otherwise最多只能有一个

     <!-- List<Emp> getEmpByChoose(Emp emp);-->
        <select id="getEmpByChoose" resultType="Emp">
            select * from t_emp
            <where>
                <choose>
                    <when test="empName !=null and empName !='' ">
                        emp_name=#{empName}
                    </when>
                    <when test="age !=null and age !='' ">
                        age=#{age}
                    </when>
                    <when test="sex !=null and sex !='' ">
                        sex=#{sex}
                    </when>
    
                    <when test="email !=null and email !='' ">
                        email=#{email}
                    </when>
                    <otherwise>
                        did=1
                    </otherwise>
                </choose>
            </where>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    foreach

    场景:批量操作:添加;删除
    foreach:
    collection:需要设置循环的数组或集合 即 传来的数据-即数组
    item:表示数组或集合中的每一个数据 即 in里面数据的属性
    separator:循环体之间的分隔符 以什么为分隔符
    open:以什么开始:foreach标签所循环的所有内容的开始符
    close以什么结束:foreach标签所循环的所有内容的结束符
    通过这两个标签可以省略()
    separator:以什么为分隔符
    item:in里面数据的属性,即数组中的每一个数据

     <!--int deleteMoreByArray(@Param("eids") Integer[] eids);-->
        <delete id="deleteMoreByArray">
            delete from t_emp where
                <foreach collection="eids" item="eid" separator="or" >
                    eid=#{eid}
                </foreach>
            <!--delete from t_emp where eid in
                (
            <foreach collection="eids" item="eid" separator=",">
                #{eid}
            </foreach>
                    )-->
    
        </delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    <!--批量增加-->
        <!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
        <insert id="insertMoreByList" >
            insert into t_emp values
            <foreach collection="emps" item="emp" separator=",">
                (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
            </foreach>
        </insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    sql

    将常用的sql片段进行记录,需要用的时候可以直接引入
           <sql id="" ></sql>//id自己命名
           <include refid=" "></include>//这里填id名称
    
    • 1
    • 2
    • 3
    <sql id="empColumns">
    eid,ename,age,sex,did
    </sql>
    select <include refid="empColumns"></include> from t_emp
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    数据库的分库分表
    html播放视频
    技术派Spring事件监听机制及原理
    Windows也能拥有好用的命令行吗?Powershell+Terminal折腾记录(v1.0版本)
    JavaWeb之前后端分离的三步骤
    尚硅谷-Spring-注解驱动篇
    猿创征文|【.Net实用方法总结】 整理并总结System.IO中FileStream类及其方法介绍
    PySpark的存储不同格式文件
    压缩状态DP位运算
    【系统架构设计】架构核心知识: 5 系统安全性与保密性设计
  • 原文地址:https://blog.csdn.net/qq_54796785/article/details/125899105