欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis 实现多条update同时执行

程序员文章站 2022-07-09 17:43:12
想在mapper的一个更新节点进行多条update语句的操作:...

想在mapper的一个更新节点进行多条update语句的操作:

<update id="cleanuserbyphone" parametertype="java.lang.string">
 update user set valid_status = 1 where mobile_phone = #{mobilephone};
 update user_account set valid_status = 1 where mobile_phone = #{mobilephone} ;
</update>

mybatis是默认不支持的,需要在数据库配置中配置相关参数:

propertes 或者yml配置 文件中的jdbc后追加&allowmultiqueries=true

jdbc.jdbcurl=jdbc:mysql://127.0.0.1:3306/database?useunicode=true&characterencoding=utf8&allowmultiqueries=true

补充:mybatis批量更新update-设置多个字段值

mybatis由于简单易用性得到大家的认可和使用

但是在批量更新操作中,网上介绍的貌似不全,正好今天做个记录,大家一起进步

在实际项目开发过程中,常有这样的需求:根据ids更新表的某一个字段值,这时的sql语句是:

public interface istaffdao {
 void batchupdate(@param("list") list<long> list);
}
<select id="getstaffsbyids" resultmap="staff_mapper">
 update staff set status = 0 where id in
 <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
 #{item}
 </foreach>
 order by id
</select>

还有一种情况:根据ids更新表的多个值,并且每个id对应的值也不一样,这时上述语句已经满足不了需求,需要另一种批量更新sql语句

public interface istaffdao {
 void batchupdate(@param("list") list<staff> list);
}
<update id="batchupdate" parametertype="java.util.list" >
 <foreach collection="list" item="item" index="index" separator=";">
 update staff set count = #{item.count} , code = #{item.code} , invalid_time = #{item.time} where id = #{item.id}
 </foreach>
</update> 

由于这种批量更新是一次执行多个update语句,所以mybatis需要额外的配置:

spring.datasource.url后加上allowmultiqueries=true

如:jdbc:mysql://10.10.20.36:3306/test?allowmultiqueries=true

否则,在执行sql语句时,会报下面的错误

[org.apache.ibatis.session.defaults.defaultsqlsession@76a2f910]
org.springframework.jdbc.badsqlgrammarexception: 
### error updating database. cause: com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'update b_email_msg_remind 
set send_status = 1, send_email_code='abc@abc.abc'' at line 6
### the error may involve com.hhsoft.sectionservice.model.persistence.emailmapper.updateemailtasks-inline
### the error occurred while setting parameters
### cause: com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'update staff 
set status = 1, send_email_code='abc@abc.abc';<span style="font-family: helvetica, tahoma, arial, sans-serif;">update sta<span style="font-size:10px;">ff set status = 2,</span> send_email_code='test@qq.com' </span>' at line 6
; bad sql grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'update b_email_msg_remind 
set send_status = 1, send_email_code='abc@abc.abc'' at line 6

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

相关标签: mybatis update