MyBatis动态SQL语句

MyBatis动态SQL语句

一、MyBatis动态语句分为4种元素:

元素作用描述
if条件判断单条件判断
choose(when、otherwise)条件选择,相当Java when多条件分支判断
where、set辅助处理sql语句拼接问题
foreache循环循环

二、Mybatis动态sql语句使用方式、例子

1、if元素,如下

<select id="selByName" resultType="yuan.yuanmybatis.entity.Account">
       select id,name,created,updated from account where 1=1
       <if test="name !=null and name !=''">
           and name like concat('%',#{name},'%')
       </if>
    </select>

2.choose

<select id="selByChoose" resultType="yuan.yuanmybatis.entity.Account">
        select id,name,created,updated,money from account where 1=1
        <choose>
            <when test="name !=null and name !=''">
                and name like concat('%',#{name},'%')
            </when>
            <when test="money !=null and money !=''">
                and name =#{money}
            </when>
            <otherwise>
                and isdeleted=1
            </otherwise>
        </choose>
    </select>

3、where元素,细心的读者会发现1、2点会有一个1=1,如果没有1=1,那么会变成如下错误语句:

select id,name,created,updated,money from account where and name like concat('%',#{name},'%') 

这个时候我们可以用where元素处理sql:

 <select id="selByName" resultType="yuan.yuanmybatis.entity.Account">
    select id,name,created,updated from account
    <where>
    <if test="name !=null and name !=''">
        and name like concat('%',#{name},'%')
    </if>
    </where>
    </select>

where元素在里面的条件成立的时候,才会加入where这个sql关键字。

4、trim元素可以帮助我们去掉一下and、or等,prefix代表语句前缀, prefixOverrides代表要去掉的字符串

 <select id="selByChoose" resultType="yuan.yuanmybatis.entity.Account">
        select id,name,created,updated,money from account
        <trim prefix="where" prefixOverrides="and">
        <choose>
            <when test="name !=null and name !=''">
                and name like concat('%',#{name},'%')
            </when>
            <when test="money !=null and money !=''">
                and name =#{money}
            </when>
            <otherwise>
                and isdeleted=1
            </otherwise>
        </choose>
        </trim>
    </select>

5、set元素,它可以在遇到逗号的时候,把对应的逗号去掉

<update id="updateAccout" parameterType="yuan.yuanmybatis.entity.Account">
        update account
        <set>
            <if test="name !=null and name !=''">
               name=#{name},
            </if>
            <if test="money!=null and money!=''">
               money=#{money}
            </if>
        </set>
        where id=#{id}
    </update>

6、foreach元素,它时一个循环语句,作用时用来遍历集合,支持数组、List、Set接口集合

<select id="selIn" resultType="yuan.yuanmybatis.entity.Account">
        select id,name,created,updated from account where name in
        <foreach collection="nameList" index="index" item="name" open="(" separator="," close=")">
        #{name}
       </foreach>
   </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值