mybatuis update批量更新

关键字解释

id Mapper里的方法名
parameterType Mapper传递的参数类型 支持 Collection-> List,Map,Set
collection 列表名
item 列表的单个元素
index 索引
separator 分割符号,foreach循环拼接sql语句,每拼接一个选择是否加分隔符
open sql片段前面是否加东西
close 后面是否加东西
foreach 循环拼接sql

重点:&allowMultiQueries=true

重点:数据库连接一定要添加&allowMultiQueries=true配置,批量操作需要,单个不需要
比如:jdbc:mysql://127.0.0.1/test_dev?characterEncoding=UTF-8&allowMultiQueries=true

更新类型

1.根据多个条件批量更新不同数据的多个字段

  <update id="updateBatchByModelId"  parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            update table_name
            set state = #{item.state},name = #{item.name}
            where model_id = #{item.id}
        </foreach>
    </update>
 </update>
list=[{id:0,state:1,name:"dhl"},
	  {id:1,state:0,name:"zy"}]

最终执行的mysql语句是:

UPDATE table_name SET state=1 ,name='dhl' WHERE id= 0;
UPDATE table_name SET state=0 ,name='zy' WHERE id= 1;

原理:通过foreach循环拼接sql语句,通过separator选择是否加分割符号,拼接好一起打包到数据库执行。

2.其他场景

case … when , where in

   <update id="updateBatch"parameterType="java.util.List">
    update mydata_table 
    set  status=
    <foreach collection="list" item="item" index="index" 
        separator=" " open="case ID" close="end">
        when #{item.id} then #{item.status}
    </foreach>
    where id in
    <foreach collection="list" index="index" item="item" 
        separator="," open="(" close=")">
        #{item.id,jdbcType=BIGINT}
    </foreach>
 </update>

if test

<trim prefix="status =case" suffix="end,">
     <foreach collection="list" item="item" index="index">
         <if test="item.status !=null and item.status != -1">
             when id=#{item.id} then #{item.status}
         </if>
         <if test="item.status == null or item.status == -1">
             when id=#{item.id} then mydata_table.status      //这里就是原数据
         </if>
     </foreach>
</trim>

<select id="getMaxDepartId" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT MAX(DEPART_ID) FROM T_P_DEPART 
        <where>
            <if test="_parameter!=null and _parameter!=''">  
                AND DEPART_PID = #{departId,jdbcType=VARCHAR} 
            </if>
            <if test="_parameter==null or _parameter==''">  
                AND DEPART_PID IS NULL
            </if>
        </where>
    </select>


<select id="findShopByName" parameterType="ShopVo" resultType="ShopCustomer">
    select * from shop 
    <where>
            <if test="shopCustomer.shopname!=null and  shopCustomer.shopname!=''">
                shop.shopname like '%${shopCustomer.shopname}%'
            </if>
            <if test="shopCustomer.shopname==null or shopCustomer.shopname==''">  
                AND shop.shopname is null
            </if>
    </where>
</select>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值