ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mybats动态sql

2022-08-20 13:00:58  阅读:180  来源: 互联网

标签:标签 age sex emp sql mybats 动态 where eid


动态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>

标签:标签,age,sex,emp,sql,mybats,动态,where,eid
来源: https://www.cnblogs.com/phonk/p/16607548.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有