MyBatis Mapper常用动态SQL之foreach 、if、choose、where、when

if语句

<if>判断语句,用于单条件分支条件判断

使用<if>元素对参数username和jobs进行非空判断,并动态组装SQL:

select * from t_person where 1=1 
     <if test="username !=null and username !=''">
	and username like concat('%',#{username}, '%')
     </if>
     <if test="jobs !=null and jobs !=''">
	and jobs = #{jobs}
     </if>

choose 、when 、 otherwise 语句

<choose>用于多条件分支判断,相当于Javaswitch ... case ... default

使用<choose>及其子元素依次对条件进行非空判断,并动态组装SQL:

  select * from t_person where 1=1
  <choose>
       <when test="username !=null and username !=''">
            and username like concat('%',#{username}, '%')
       </when>
       <when test="jobs !=null and jobs !=''">
            and jobs= #{jobs}
       </when>
       <when test="phone != null and phone != ''">
            and phone= #{phone}
       </when>
       <otherwise>
            1=1
       </otherwise>
  </choose>

加入了条件“where 1=1”后,既保证了where后面的条件成立,又避免了where后面第一个词是and或者or之类的关键词。
当用户一个条件都不选时,可以在<otherwise>中写上1=1让语法成立,反之,若选择了条件则会返回正常结果

where 、 trim 语句

针对上述情况中“where 1=1”,在MyBatis的SQL中就可以使用<where><trim>元素进行动态处理

<select id="selectPerson">
select * from t_person
<trim prefix="where" prefixOverrides="and">
     <if test="username !=null and username !=''">
         and username like concat('%',#{username}, '%')
     </if>
     <if test="jobs !=null and jobs !=''">
         and jobs= #{jobs}
     </if>
</trim>
</select>
<select id="selectPerson">
select * from t_person
<where>
    <if test="username !=null and username !=''">
            and username like concat('%',#{username},'%')
    </if>
    <if test="jobs !=null and jobs !=''">
            and jobs= #{jobs}
    </if>
</where>
</select>

trim标签:prefixOverrides=“and|or” prefix=“where” suffixOverrides=“” suffix=“”

属性介绍常用值
prefixOverrides前缀覆盖and或or
prefix前缀一般是where或者set(只执行一次)
suffixOverrides后缀覆盖一般是逗号,
suffic后缀一般是更新语句的查询条件

<trim>标签可以处理<where><set>标签所需要处理的问题,根据需求使用即可,举个例子,下面通过输入哪些参数,就更新哪些参数:

<update id="">
update t_person 
<trim prefix="set" suffix="where id=#{personId}" suffixOverrides="," >
<if test="username !=null and username !=''">
   username = #{username}
</if>
<if test="jobs !=null and jobs !=''">
   jobs = #{jobs}
</if>
<if test="phone !=null and phone !=''">
   phone = #{phone}
</if>
</trim>
</update>

使用和元素对username和jobs进行更新判断,并动态组装SQL。这样就只需要传入想要更新的字段即可

<update id="updatePerson"  parameterType="com.ssy.po.Person">
        update t_person
        <set>
            <if test="username !=null and username !=''">
                  username=#{username},
            </if>
            <if test="jobs !=null and jobs !=''">
                  jobs=#{jobs},
            </if>
        </set>
        where id=#{id}
</update>

使用<set><if>结合的方式来组装update语句。<set>(进行更新字段时,要确保更新的字段不能为空)元素会动态前置 SET关键字,同时也会消除多余的‘,’

foreach语句

foreach五大标签:

标签描述
collection该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,默认使用list
item表示集合中每一个元素进行迭代时的别名,随便起的变量名;
index也就是索引,用于表示在迭代过程中,每次迭代到的位置;
open表示该语句以什么开始,常用“(”;
separator表示在每次进行迭代之间以什么符号作为分隔符,常用“,”;
close表示以什么结束,常用“)”;

对于collection如果dao层接口没有用@Param注解时,默认使用list作为集合,如果是数组则为array
如果不在open、close、separate标签中明确,就得在mapper循环前后加上(),以及英文逗号,

批量插入操作,传入参数是一个list,数组中存放的对象,示例:

<insert id="batchInsert">
    insert into t_person (a, b, c, d)
    values
    <foreach collection="list" item="item" separator=",">
        (#{item.a,jdbcType=BIGINT}, #{item.b,jdbcType=VARCHAR},
        #{item.c,jdbcType=DECIMAL}, #{item.d,jdbcType=DECIMAL})
    </foreach>
</insert>   

批量查询操作,通常和in语句已使用,示例:

List<ActSmsSendAudit> batchQueryByIdList(@Param("idList") List<Integer> idList);
  <select id="batchQueryByIdList">
    select *
    from t_person
    where id in
      <foreach collection="idList" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
  </select>

如果不使用foreach直接写sql语句:

  <select id="batchQueryByIdList">
    select *
    from t_person
    where id in (  ,  , )
  </select>

可以看出,如果使用MyBatis的动态sql可以传任意的数量id,使用传统查询只能固定数量的数据。

批量更新操作

  <update id="updateActivityStatus" parameterType="int" >
    UPDATE marketing_activity_list SET status=0
    WHERE
    <foreach collection="list" item="id" separator="or">
      activity_seq = #{id}
    </foreach>
  </update>

灵活运用foreach可以做到事半功倍的效果!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值