使用Mybatis对mysql的增删改查批量操作示例

在使用批量更新之前 要先设置 mysql 支持 批量执行多条sql 在连接信息后加上[&allowMultiQueries=true]
在这里插入图片描述

删除


    <delete id="deleteGenTableColumnByIds" parameterType="Long">
        delete from gen_table_column where table_id in 
        <foreach collection="array" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </delete>

查询

示例: 排除多个状态

示例中 excludeStatus为数组 请注意数组情况下判空的方式

        <select id="uList" resultType="User">
				select *
				from user
		<where>
				<!-- 排除指定状态-->
            <if test="excludeStatus != null  and excludeStatus.length>0 ">
            	and status not in
                <foreach item="item" collection="excludeStatus" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
示例: 指定多个状态
 <select id="selectUser" resultType="User">
        select *
        FROM user
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

新增

新增示例1:

参数: 多参数+数组

  public int setCargoBoxType(@Param("sourceId") Long sourceId, @Param("cargoBoxTypes") Integer[] cargoBoxTypes);
  <insert id="setCargoBoxType">
        insert into t_user(source_id,cargo_box_type) values
        <foreach item="cargoBoxType" index="index" collection="cargoBoxTypes" separator=",">
            (#{sourceId},#{cargoBoxType})
        </foreach>
    </insert>

注意java里的@param注解
可以看到 在循环的过程中参数:‘sourceId’ 是固定的
而参数:‘cargoBoxType’ 是每次循环而得到的

新增示例2:

参数: 集合

  public int setUserId(@Param("userIdList") List<User> userList);
 <insert id="setUserId">
        insert into t_user(user_id) values
        <foreach item="item" index="index" collection="userIdList" separator=",">
            (item.userId)
        </foreach>
    </insert>

循环过程中 item就代表是user对象了 所以item.userId就是获取这个对象的id

新增示例3:
<insert id="insertList">
    <foreach item="item" index="index" collection="auditList" separator=";">
        INSERT INTO glis_site_audit (illustrate, var_ext_call, var_own, type, carrier_id)
        VALUES
        (#{item.illustrate}, #{item.varExtCall}, #{item.varOwn}, #{item.type}, #{item.carrierId})
    </foreach>
</insert>

修改

修改示例1
<update id="updateList">
        <foreach separator=";" index="index" item="item" collection="list" close="" open="">
            update tb_activity
            set status=#{item.status}
            where id = #{item.id}
        </foreach>
    </update>
修改示例2
<update id="batchUpdate1" parameterType="java.util.List"> 
     
      update sys_group set level = null where level in
       <foreach separator="," index="index" item="item" collection="list" close=")" open="("> 
         #{item}
      </foreach> 
      
  </update>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值