mybatis常用标签简单介绍(trim,foreach,include,set)

在使用Mybatis时写sql语句是必须的,在写sql语句时如果涉及到根据条件拼接的sql语句的话就要处理wher、and、 in 这样的字符串,而Mybatis中有满足这样的标签可以让我们不用为处理这样的字符串而苦恼。下面收集和总结了一下比较常用的标签:

trim标签

               prefix:前缀覆盖并增加其内容 不写的话默认替换为空

               suffix:后缀覆盖并增加其内容 不写的话默认替换为空

                prefixOverrides:前缀判断的条件

               suffixOverrides:后缀判断的条件

一个trim只能去掉一个需要去掉的sql的前缀或者后缀。

<span style="font-size:18px;"><select id="pageListBookByNameAndTypeAndPages" resultMap="pojoBooks">
    select * from books where 1=1 and
    <trim suffixOverrides="and" prefixOverrides="and">
        <if test="books!=null">

            <if test="books.book_name!=null and books.book_name==''">
                and book_name like"%" #{books.book_name} "%" and
            </if>
            <if test="books.book_type!=null and books.book_type==''">
              and  book_type like "%"#{books.book_type} "%"
            </if>

        </if>

        <if test="pages!=null">
          and  limit #{pages.pageStart},#{pages.pageEnd} and
        </if>
    </trim>
</select></span>

打印出的sql:

SQL: select* from books where 1=1 and          book_name like "%" ? "%" and    and  book_type like "%"? "%"   and limit ?,?


可见我们的第一个and和最后一个and被去除了,但是中间的没有被去掉。所以说trim是针对trim标签中的整个sql而言。

我们在看另一个sql语句:

<span style="font-size:18px;"><select id="pageListBookByNameAndTypeAndPages" resultMap="pojoBooks">
    select * from books where 1=1 and
    <trim suffixOverrides="and" prefixOverrides="and">
        <if test="books!=null">
            <if test="books.book_name!=null and books.book_name==''">
                and book_name like"%" #{books.book_name} "%" and
            </if>
            <if test="books.book_type!=null and books.book_type==''">
                book_type like"%" #{books.book_type} "%"
            </if>
        </if>
        <trim prefixOverrides="and">
        <if test="pages!=null">
           and limit#{pages.pageStart},#{pages.pageEnd} and
        </if>
        </trim>
    </trim>
</select></span>

这样得到的结果是:

select *from books where 1=1 and book_name like "%" ? "%" and book_type like "%" ? "%" limit ?,?

可见,一个trim只能去掉trim标签内的一个前缀或者后缀。

 

Include标签

<include refid="" ></include>是包含一个<sql> 的语句片段。

 

Foreach标签:

用来遍历我们的list或者数组;如:

<span style="font-size:18px;"><delete id="delBooks" >
    delete from books
    <where>
        book_id in
 <foreach collection="list" item="bookid" open="(" separator="," close=")" >
            #{bookid}
        </foreach>
    </where>
</delete></span>

对于要指定范围的bookid我们就可以这样达到效果,这里值得注意的是如果没有设置@param那个list或者数组的参数名的话,MyBatis会自动将它包装在一个 Map ,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键index是当前迭代的次数,item的值是本次迭代获取的元素。当使用字典(或者Map.Entry对象的集合)时,index是键,item是值

 

Where标签:

Where标签中如果都是if标签那至少需要一个if标签满足才插入where。如果里面有没有被<if>标签包裹的字符串,那这个where就会被拼接到最后的sql语句中;有同学说 如果若最后的内容是“AND”或“OR”开头的,where 标签可以自己将他们去除。我做了一下验证:

<span style="font-size:18px;">    <delete id="delBooks" >
        delete from books
        <where>
            and
            book_id in
            <foreach collection="mydelbookids" item="bookid" open="(" separator="," close=")">
                #{bookid}
            </foreach>
            and
        </where>
    </delete></span>
打印的sql语句是:

Preparing: delete from books WHERE book_id in ( ? , ? ) and 

    <delete id="delBooks" >
        delete from books
        <where>
            and book_id=70 and
        </where>
    </delete>
打印的sql语句是:
delete from books          WHERE  book_id=70 and

where 能去掉开头的 and 但是不能去除结尾的and (也可能我书写的方式不对)。

<set>标签:

<span style="font-size:18px;"><update id="updateBook">
    <if test="books.book_state==0">
        update books
        <set>
            <if test="books.book_name!=null">
               book_name=#{books.book_name},
            </if>
            <if test="books.book_price!=null">
                book_price=#{books.book_price},
            </if>
            <if test="books.book_content!=null">
               book_content=#{books.book_content},
            </if>
            <if test="books.book_type!=null">
               book_type=#{books.book_type},
            </if>
            <if test="books.cover!=null">
                book_cover=#{books.cover}
            </if>
        </set>
    </if>
    where book_id = #{books.book_id}
</update></span>

用于update中,满足条件的才更新值。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值