mybatis条件判断/ 动态sql

1.if标签--单条件判断:判断完第一个条件,对下一个条件进行判断,看是否能满足条件,满足则执行

<select id="findByCondition" resultType="com.dyt.entity.Account">
        select * from account where 1=1
        <if test="name!=null and name!=''">
             and  name=#{name}
        </if>
        <if test="money!=null">
             and  money=#{money}
        </if>
    </select>

choose标签 多条件分支判断

假如第一个满足条件,则开始执行,并不会判断下一个条件,如第一个不满足则判断下一个,满足则执行

<select id="findByCondition02" resultType="com.dyt.entity.Account">
        select * from account where 1=1
        <choose>
             <when test="name!=null and name!=''">
                 and  name=#{name}
             </when>
            <when test="money!=null">
                and  money=#{money}
            </when>
            <otherwise>
                and isdeleted=0
            </otherwise>
        </choose>
    </select>

where标签

我们观察到上面的sql都加了 where 1=1 ,如果不使用where 1=1 那么你的动态sql可能会出错。 我们能不能不加where 1=1呢! 可以 那么我们就可以使用where标签,作用:可以自动为你添加where关键字,并且可以帮你去除第一个and |or

<select id="findByCondition" resultType="com.dyt.entity.Student">
        select * from Student
        <where>
            <if test="name!=null and name!=''">
                 and  name=#{name}
            </if>
            <if test="money!=null">
                 and  money=#{money}
            </if>
        </where>
    </select>

动态sql

set标签

这个配合if标签一起用,一般用在修改语句。如果传递的参数值为null,那么应该不修改该列的值

  <update id="update">
          update student
          <set>
             <if test="name!=null and name!=''">
                name=#{name},
             </if>
             <if test="age!=null">
                age=#{age},
             </if>
             <if test="sex!=null">
                 sex=#{sex},
             </if>
          </set>
          where id=#{id}
    </update>

foreach标签

循环标签.

查询:

<!-- select * from student where id in(2,3,5,7,0)
        如果你使用的为数组array  如果你使用的为集合 那么就用list
        collection:类型
        item:数组中每个元素赋值的变量名
        open: 以谁开始
        close:以谁结束
        separator:分割符

    -->
    <select id="findByIds" resultType="com.dyt.entity.Student">
        select * from student where id in
        <foreach collection="array" item="id" open="(" close=")" separator=",">
             #{id}
        </foreach>
    </select> 

删除:

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

添加:

insert into student(name,isdeleted) values('ldh',0),('ldh2',0),('ldh4',0)
   <insert id="saveBatch">
        insert into student(name,isdeleted) values
        <foreach collection="list" item="acc" separator=",">
            (#{acc.name},#{acc.isdeleted})
        </foreach>
    </insert>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值