mybats动态sql

2022/8/22 2:26:26

本文主要是介绍mybats动态sql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

动态SQL

根据特定条件动态拼装SQL的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点

IF标签

根据标签中test属性所对应的表达式来决定标签中的内容,是否拼接到语句中

当if标签不成立,where空了 / and关键字多余了怎么办

1=1恒等式

List<Emp> getEmpDYById(@Param("emp")Emp emp);
    <select id="getEmpDYById" resultType="Emp">
        select * from t_emp where 1=1
        <if test="eid != null and eid !=''">
            and eid = #{eid}
        </if>
        <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 eid = #{eid}
        </if>
    </select>

where标签

当where标签中有内容时会生成where关键字,并且将内容多余的 and/or 自动去除,内容的去除不了,当where标签中没有内容时,不会生成where关键字

    <select id="getEmpDYById" resultType="Emp">
        select * from t_emp 
        <where>
            <if test="eid != null and eid !=''">
                eid = #{eid}
            </if>
            <if test="empName != null and empName !=''">
                emp_name = #{empName}
            </if>
            <if test="age != null and age !=''">
                age = #{age}
            </if>
            <if test="sex != null and sex !=''">
                sex = #{sex}
            </if>
            <if test="email != null and email !=''">
                eid = #{eid}
            </if>
        </where>
    </select>

trim标签

若标签中没有内容时,trim标签没有任何效果

若标签中有内容时候:

  • suffix:将trim标签中内容面添加指定内容

  • prefix:将trim标签中内容面添加指定内容

  • suffixOverrides:将trim标签中内容面去除指定内容,如果有多个指定的内容用 | 隔开

  • prefixOverrides:将trim标签中内容面去除指定内容,如果有多个指定的内容用 | 隔开

这里的意思就是 最后生成的sql语句,不管从哪个if语句开始,最前面要加上where,最后一个and/or一定要去除

    <select id="getEmpDYById" resultType="Emp">
        select * from t_emp
        <trim prefix="where" suffixOverrides="and|or">
            <if test="eid != null and eid !=''">
                eid = #{eid} and 
            </if>
            <if test="empName != null and empName !=''">
                emp_name = #{empName} or 
            </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 !=''">
                eid = #{eid} and 
            </if>
        </trim>
    </select>

choose、when、otherwise(类似于if、else if、else)

<choose>
    <when test="">
        ...
    </when>
    <when test="">
        ...
    </when>
    <otherwise>
        ...
    </otherwise>
</choose>

根据标签中test属性所对应的表达式来决定标签中的内容,是否拼接到语句中,when标签至少有一个,otherwise只能有一个

    <select id="getEmpDYById" resultType="Emp">
        select * from t_emp where 1=1
        <choose>
            <when test="eid != null and eid !=''">
                and eid = #{eid}
            </when>
            <when test="empName != null and empName !=''">
                and emp_name = #{empName}
            </when>
            <when test="age != null and age !=''">
                and age = #{age}
            </when>
            <when test="sex != null and sex !=''">
                and sex = #{sex}
            </when>
            <when test="email != null and email !=''">
                and eid = #{eid}
            </when>
        </choose>
    </select>

foreach标签

foreach用于循环一个数组

  • collection:需要循环的数组/集合

  • item:数组/集合中的项

  • separator:循环体之间分隔符

  • open:在foreach循环所有步骤之前以什么字符开始

  • close:在foreach循环所有步骤完后以什么字符结束

  • foreach前后自带一个空格

数组批量删除

int deleteEmpByArray(@Param("eIds")Integer[] eIds)
   <delete id="deleteEmpByArray">
        delete from t_emp where eid in
        <foreach collection="eids" item="eid" separator="," open="(" close=")">
            #{eid}
        </foreach>
    </delete>

list批量添加

int insertEmpByList(@Param("emps")List<Emp> emps)
    <insert id="insertEmpByList">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null, #{emp.empName}, #{emp.age}, #{emp.sex}, #{emp.email})
        </foreach>
    </insert>

sql / include标签

sqll标签:将常用的一个sql片段进行记录

  • id:设置你的sql片段的名字

include标签:将sql标签进行引用

  • refid:填入需要引用的sql标签片段的名字

  <sql id="insert">
        (null, #{emp.empName}, #{emp.age}, #{emp.sex}, #{emp.email})
    </sql>
  <!--int insertEmpByList(@Param("emps")List<Emp> emps);-->
    <insert id="insertEmpByList">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            <include refid="insert">
        </foreach>
    </insert>


这篇关于mybats动态sql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程