MyBatis动态SQL标签

MyBatis动态SQL标签

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

1、if

if主要时可以实现多条件查询

/**
 * 多条件查询
 */
public List<Emp> getEmpByCondition(Emp emp);
<!--List<Emp> getEmpByCondition(Emp emp)-->
<select id="getEmpByCondition" 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>

注意:

  • if标签可通过test属性(即传递过来的数据)的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行

  • 使用if标签实现动态SQL查询最好在where条件后面加上恒等的条件1=1,这样可以避免SQL语法错误。

    • 这个恒成立条件并不会影响查询的结果
    • 这个1=1可以用来拼接and语句,例如:当empName为null时,如果不加上恒成立条件,则SQL语句为select * from t_emp where and age = ? and sex = ? and email = ?,此时where会与and连用,SQL语句会报错,如果加上一个恒成立条件,则SQL语句为select * from t_emp where 1= 1 and age = ? and sex = ? and email = ?,此时不报错

2、where

  • 当where标签中有内容时,会自动生成where关键字,并将if标签中内容前多余的and或or关键字去掉
  • 若if条件全都不成立,则认为where标签中没有内容,即where标签不生效
<!--List<Emp> getEmpByCondition(Emp emp)-->
<select id="getEmpByCondition" resultType="emp">
    select * from t_emp
    <where>
        <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>
    </where>
</select>

**注意:**where只会将if标签内容前多余的and或or去掉,而不会将内容后多余的and或or去掉。

3、trim

  • 当trim标签中有内容时,会自动生成指定内容,并将if标签中内容前或后多余的指定内容去掉
  • 若if条件全都不成立,则认为trim标签中没有内容,即trim标签不生效

trim标签有四个属性

  • prefix:在trim标签内容的前面添加指定内容
  • sufix:在trim标签内容的后面添加指定内容
  • prefixOverrides:将trim标签内容前面的指定内容去掉
  • sufixOverrides:将trim标签内容后面的指定内容去掉
<!--List<Emp> getEmpByCondition(Emp emp)-->
<select id="getEmpByCondition" resultType="emp">
    select * from t_emp
    <trim prefix="where" suffix="" prefixOverrides="" 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>

**注意:**上面代码中定义的trim标签意思为在trim标签生效时先加上where后将if标签内容后面的and或or去掉,以此类推。

4、choose、when、otherwise

这一组标签相当于if…else if…else,其中choose是主标签,when、otherwise是其子标签

  • when相当于if或else if
  • otherwise相当于else
/**
 * 测试choose、when、otherwise
 */
public List<Emp> getEmpByChoose(Emp emp);
<!--List<Emp> getEmpByChoose()-->
<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>

5、foreach

循环取数组中元素,有5个属性

  • collection:访问数组的方式
  • item:数组中元素的名字
  • separator:分隔符
  • open:以…开始
  • close:以…结束

但MyBatis会自动将数组或集合放入map集合中,array为键,数组为值,arg0为键,数组为值。

通过数组批量删除

/**
 * 通过数组实现批量删除
 */
Integer deleteMoreByArray(Integer[] eids);
<!--deleteMoreByArray(Integer[] eids);-->
<delete id="deleteMoreByArray">
    delete from t_emp where eid in
    (
        <foreach collection="array" item="eid" separator=",">
            #{eid}
        </foreach>
    )
</delete>

同样的,可以通过@param注解重命名

/**
 * 通过数组实现批量删除
 */
Integer deleteMoreByArray(@Param("eids") Integer[] eids);
<!--deleteMoreByArray(Integer[] eids);-->
<delete id="deleteMoreByArray">
    delete from t_emp where eid in
    (
        <foreach collection="eids" item="eid" separator=",">
            #{eid}
        </foreach>
    )
</delete>

同时可以去掉括号用open和close实现

<!--deleteMoreByArray(Integer[] eids);-->
<delete id="deleteMoreByArray">
	<!--方法1-->
    delete from t_emp where eid in
    <foreach collection="eids" item="eid" separator="," open="(" close=")">
        #{eid}
    </foreach>
</delete>

还有第二种SQL语句实现批量删除

<delete id="deleteMoreByArray">
    <!--方法2-->
    delete from t_emp where
    <foreach collection="eids" item="eid" separator="or">
        eid = #{eid}
    </foreach>
</delete>

通过list批量添加

/**
 * 通过list实现批量添加
 */
public Integer insertMoreByList(@Param("emps") List<Emp> emps);
<!--Integer insertMoreByList(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>

6、sql

可在类外设置一个sql片段,使用时插入到sql语句中

<sql id="empColumns">eid,emp_name,age,email</sql>

引用sql片段

在这里插入图片描述
将*换成了定义的sql片段

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值