Mybatis批量添加、批量修改



Mybatias中批量添加功能实现


提示:以下是本篇文章正文内容,下面案例可供参考

  1. Mapper层
    /**
    * @Description: 新增人员信息
    * @Param:
    * @return:
    * @Author: 杨永卓
    * @Date: 2022/7/7 20:21
    */
    boolean insertUser(@Param("users") List<User> users);
  1. Mapper.xml中
    <insert id="insertUser" parameterType="List">
        insert into t_user
        (
        id,
		user_name,
		password,
		age
		)
        values
        <foreach collection="users" item="user" separator=",">
            (
            #{user.id},
            #{user.userName},
            #{user.password},
            #{user.age}
            )
        </foreach>
    </insert>

Mybatias中批量修改功能实现

批量修改分为两种,一种需要在application.yml的jdbc配置后面拼接allowMultiQueries=true,第二种不需要。
下面是示例

在这里插入图片描述

批量处理的时候需要在jdbc连接后面加上:allowMultiQueries=true,因为默认是不支持批处理的。
第一种

  1. Mapper层
    /**
     * 修改人员信息
     * 
     * @param updateList
     *            更新集合
     * @return {@code Integer}
     */
    boolean updateUsers(@Param("list") List<User> updateList);
  1. Mapper.xml中
<update id="updateUsers" parameterType="java.util.List"> 
      <foreach collection="list" item="item" index="index" open="" close="" separator=";"> 
       update t_user
          <set> 
	 			<if test="item.userName!= null and item.userName!= ''">user_name=#{item.userName},</if>
	 			<if test="item.password!= null and item.password!= ''">password=#{item.password},</if>
	 			<if test="item.age!= null and item.age!= ''">age=#{item.age}</if>
	 			create_date= sysdate()			
          </set> 
          where id= #{item.id} 
      </foreach> 
</update>

第二种

  1. Mapper层
    /**
     * 批量更新
     * @param updateList
     * @return
     */
    Boolean updateBatch(List<VisualizeDsVideoInfo> updateList);
  1. Mapper.xml中 通过when then来修改
	<!--批量修改-->
	<update id="updateBatch" parameterType="java.util.List">
		update ds_video
			    <trim prefix="set" suffixOverrides=",">
					<trim prefix = "status=case" suffix="end,">
						<foreach collection="list" item="info" index="index">
							when id = #{info.id} then  #{info.status}
						</foreach>
					</trim>
					<trim prefix = "code=case" suffix="end,">
						<foreach collection="list" item="info" index="index">
							when id = #{info.id} then #{info.code}
						</foreach>
					</trim>
					<trim prefix = "camera_name=case" suffix="end,">
						<foreach collection="list" item="info" index="index">
						when id = #{info.id} then #{info.cameraName}
						</foreach>
					</trim>
					<trim prefix = "update_by=case" suffix="end,">
					<foreach collection="list" item="info" index="index">
						when id = #{info.id} then #{info.updateBy}
					</foreach>
					</trim>
					<trim prefix = "update_time=case" suffix="end,">
					<foreach collection="list" item="info" index="index">
						when id = #{info.id} then #{info.updateTime}
					</foreach>
					</trim>
			</trim>
			where
			    <foreach collection="list" separator="or" item="info" index="index">
					id = #{info.id}
				</foreach>
	</update>

总结:仰天大笑出门去,我辈岂是蓬蒿人

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT行业小趴菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值