如何在MyBatis的mapper.xml文件编写批量的修改的语句?

如何在MyBatis的mapper.xml文件编写批量的修改的语句?

已知,dao 层提供的接口如下:

/**
 * 批量修改用户
 * @param userList
 * @return
 */
public int updateUserBatch(List<User> userList);

批量修改的语句如下:

<!-- 
	批量修改用户
	当传入的是一个List,collection="list"
	当传入的是一个数组,collection="array"
	当传入的是包装类或者map,collection为包装类的属性或者map的key
	注意:在mysql中,用多条语句运行的方式,需要在url连接中开启allowMultiQueries设置为true
-->
<update id="updateUserBatch" parameterType="list">
	<foreach collection="list" item="user" separator=";">
		UPDATE user
		<set>
			<if test="user.userName != null">
				user_name = #{user.userName},
			</if>
			<if test="user.loginName != null">
				login_name = #{user.loginName},
			</if>
			<if test="user.password != null">
				password = #{user.password},
			</if>
			<if test="user.age != null">
				age = #{user.age},
			</if>
			<if test="user.sex != null">
				sex = #{user.sex},
			</if>
			<if test="user.deptId != null">
				dept_id = #{user.deptId},
			</if>
			<if test="user.birthday != null">
				birthday = #{user.birthday},
			</if>
			<if test="user.tvUpdate != null">
				tv_update = #{user.tvUpdate},
			</if>
		</set>
		WHERE user_id = #{user.userId}
	</foreach>
</update>

最终拼接的语句类似如下的形式:

UPDATE user set xxx=xxx WHERE user_id = 1;
UPDATE user set xxx=xxx WHERE user_id = 2;
...
UPDATE user set xxx=xxx WHERE user_id = 144;
UPDATE user set xxx=xxx WHERE user_id = 145;

因为是要批量提交多条语句,所以需要在 mysql 的连接里面开启 allowMultiQueries=true

jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&allowMultiQueries
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值