mybatis 实现多条update同时执行
- 更新时间:2023-01-06 11:36:57
- 编辑:蒙浩涆
参考资料
- 《Spring+Spring MVC+MyBatis整合开发实战》源码 配套资源 / 42.4 MB / 陈学明 推荐度:
- Mybatis在Mapper.xml文件中的转义字符处理方式 / 82 KB / 码小辫 推荐度:
- MyBatis从入门到精通 PDF 电子书 / 116.8 MB / 刘增辉 推荐度:
正文内容
mybatis执行多条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相关教程
-
MyBatis insert操作插入数据之后返回插入记录的id
给网友朋友们带来一篇关于MyBatis的教程,今天小编就为大家分享一篇关于MyBatis插入数据之后返回插入记录的id,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
发布时间:2022-06-22
-
Spring Boot整合mybatis多数据源用法
这篇文章主要介绍了Spring Boot 整合mybatis 使用多数据源的实现方法,需要的朋友可以参考下
发布时间:2020-01-07
-
关于Mybatis基于注解形式的sql语句生成实例效果
这篇文章主要介绍了 Mybatis基于注解形式的sql语句生成实例代码,需要的朋友可以参考下
发布时间:2020-01-21
-
解决Mybatis逆向工程中使用Mysql8.0版本驱动遇到的问题
今天在使用 8.0.12 版的 mysql 驱动时遇到了各种各样的坑。这篇文章主要介绍了详解Mybatis逆向工程中使用Mysql8.0版本驱动遇到的问题,感兴趣的小伙伴们可以参考一下
发布时间:2019-06-18
-
详解使用Mybatis-plus + velocity模板生成自定义的代码
给大家整理了关于Mybatis的教程,这篇文章主要介绍了详解使用Mybatis-plus + velocity模板生成自定义的代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
发布时间:2022-09-07
-
实例详解mybatis的插件机制
这篇文章主要给大家介绍了关于mybatis插件机制的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mybatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
发布时间:2020-01-13
-
mybatis注解配置的学习笔记
本篇文章主要介绍了mybatis学习笔记之mybatis注解配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2019-06-21
-
mybatis 传入null值的解决方案
这篇文章主要介绍了mybatis 传入null值的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
发布时间:2022-04-25
-
Spring Boot + Mybatis多数据源和动态数据源配置方法
最近做项目遇到这样的应用场景,项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库。下面小编给大家带来了Spring Boot + Mybatis多数据源和动态数据源配置方
发布时间:2022-04-03