Mybaits动态sql 常用标签

Mybatis实现动态SQL,主要通过以下标签:if,where,choose(when,otherwise),trim,set,foreach。接下来,我将逐一来说明各标签的用法。有错误的地方麻烦指正~

if标签

就是简单的条件判断,利用if语句我们可以实现某些简单的条件选择。 
这个一般用于动态选项的查询,即多值结合查询,选项值有可能为空,因此需要用到动态if标签来判断多个值存在与否。 
例1:通过年龄和居住地查询用户表。

<select id="find" parameterType="Admin" resultType="Admin">
        select * from admin where 1=1 
        <if test="age!= null and age!='' ">
                and age=age
        </if>
        <if test="city!= null and city!='' ">
                and city=city
        </if>
</select>

注意:上面“1=1”始终为true,所以后边可以直接接上and 语句,不然就会出现“where and age=age”这样的错误。

where标签

补充相应的where的sql语句,解决了if标签在上面所容易造成错误的问题。更好地简化了where的条件判断。 
例2:例1的升级版。

<select id="find" parameterType="Admin" resultType="Admin">
        select * from admin
    <where>
        <if test="age!= null and age!='' ">
                and age=age
        </if>
        <if test="city!= null and city!='' ">
                and city=city
        </if>
    </where>
</select>


这里就不需要再补充“1=1”的语句了,这是因为where标签会自动帮你处理,如果where后边是and,or开头的会被自动忽略掉。如果使用了and,or的话,mybatis也会自动为你在前后补充空格,妈妈就再也不用担心出现“ageandpassword”的错误出现~

choose标签

相当于switch语句,通常与when和otherwise搭配使用。 
例3:根据年龄和居住地查找表

<select id="find" parameterType="Admin" resultType="Admin">
    select * from admin where 1=1
    <choose>
        <when test="age!= null and age!=''">
            and age=#{age}
        </when>
        <when test="city!= nulland city!=''">
            and city=#{city}
        </when>
        <otherwise>
             and aname=#{aname}
        </otherwise>
    <choose>
<select>

这里与例1和例2不同的是,这里不会每一条都执行,类似于switch case语句。从上往下执行,当when中有条件满足的时候,就会跳出choose。when中条件都不执行则会执行otherwise语句。即最终choose里面仅有一条语句执行。

set标签

类似于where功能,主要用于sql赋值更新操作,聪明的你一个可以领悟得到啦~

<update id ="update" parameterType="Admin">
    update admin
    <set>
         <if test="aname !=null and aname !='' ">
                aname=#{aname},
        </if>
        <if test="age!= null and age!='' ">
                age=#{age},
        </if>
        <if test="city!= null and city!='' ">
                password=#{password},
        </if>
    </set>
    where aid=#{aid}
</update>

补充:set标签会自动忽略不必要的后缀逗号。

foreach标签

类似于for循环,循环的对象可以是list和数组。 
时常用于sql的批量操作,如批量删除,批量添加数据等。 
例子:批量添加用户

<insert id="addAdmin" >
        insert into amdin
        (aid,aname,pwd,city,address)
        values
        <foreach collection="list" item="item" index="index" separator=",">  
            (#{item.aid},#{item.aname},#{item.pwd},#{item.city},#{item.address)       
        </foreach>  
</insert>

与之相应的sql语句为:insert into admin (…) values(…);。foreach就将对象一个个存入数据库中。

例子:批量删除用户

<delete id="delAdmin">
     delete from admin where aid in
     <foreach collection="array" item="aid" open="(" separator=","  close=")">
           #{aid}
     </foreach>
</delete>

相应的语句为:delete from admin where aid into(…);。foreach会将数组转为(aid,aid,aid)的形式,主要看foreach的后三个属性就知道啦

trim标签

十分强大!可以自定义添加前后缀,与之对应的属性是prefix和suffix。同时通过prefixOverrides和suffixOverrides分别来覆盖首尾部的内容,即忽略不必要的前后缀。就是说它可以充当where标签,也可以充当set标签啦~ 
例: 
充当where标签:

<trim prefix = "where" prefixOverrides="and|or" >
    ...
</trim>

充当set标签:

<trim prefix="set" suffixOverrides=",">
    ...
</trim>

例子:动态添加用户属性

<insert id="find" resultType="Admin">
    insert into admin 
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test = "aname != null and aname !='' ">
            aname,
        </if>
        <if test = "city != null and city !='' ">
            city,
        </if>
        <if test = "age != null and age !='' ">
            age,
        </if>
    </trim>
    <trim prefix="values(" suffix=")" suffixOverrides=",">
        <if test = "aname != null and aname !='' ">
            #{aname},
        </if>
        <if test = "city != null and city !='' ">
            #{city},
        </if>
        <if test = "age != null and age !='' ">
            #{age},
        </if>
    </trim>
</insert>


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值