Mybatis批量更新数据库
来源:互联网 发布:解析的域名怎么弄 编辑:程序博客网 时间:2024/05/15 23:46
使用如下Mybatis map xml文件
出错信息:
### The error may involve com.hirain.testmanagement.mapper.TestcaseNodeMapper.updateTestcaseNodeBatch-Inline### The error occurred while setting parameters### SQL: UPDATE testcase_node SET name=?, version=?, description=?, last_modify_user=?, last_modify_time=? WHERE object_id=? AND root_id=? ; UPDATE testcase_node SET name=?, version=?, description=?, last_modify_user=?, last_modify_time=? WHERE object_id=? AND root_id=?### 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 testcase_node SET name='Türstatus', version=4, ' at line 8; 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 testcase_node SET name='Türstatus', version=4, ' at line 8 at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:233)
仔细检查 map文件 和数据库表字段没有错误
最终结果是因为 配置的 MySQL jdbc 链接字符串 默认不支持一次性执行多个sql 语句;
但是在我们的 update map中需要执行多个 update语句。
最后加上参数 "allowMultiQueries" 设置为true 如下:
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true" />
问题解决!
但是这样其实是一条记录update一次,性能比较差,容易造成阻塞。
MySQL没有提供直接的方法来实现批量更新,可以考虑使用case when语法来实现这个功能。
UPDATE course
SET name = CASE id
WHEN 1 THEN 'name1'
WHEN 2 THEN 'name2'
WHEN 3 THEN 'name3'
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
这条sql的意思是,如果id为1,则name的值为name1,title的值为New Title1;依此类推。
在Mybatis中的配置则如下:
<update id="updateBatch" parameterType="list">
update course
<trim prefix="set" suffixOverrides=",">
<trim prefix="peopleId =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.peopleId!=null">
when id=#{i.id} then #{i.peopleId}
</if>
</foreach>
</trim>
<trim prefix=" roadgridid =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.roadgridid!=null">
when id=#{i.id} then #{i.roadgridid}
</if>
</foreach>
</trim>
<trim prefix="type =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.type!=null">
when id=#{i.id} then #{i.type}
</if>
</foreach>
</trim>
<trim prefix="unitsid =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.unitsid!=null">
when id=#{i.id} then #{i.unitsid}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.id}
</foreach>
</update>
- Mybatis批量更新数据库
- Oracle数据库 Mybatis批量插入,更新
- MyBatis中进行批量更新(MySQL数据库)
- Mybatis 批量增加,批量更新
- Mybatis 批量插入、批量更新
- Mybatis批量更新
- Mybatis批量更新
- Mybatis批量更新
- mybatis 批量更新
- mybatis 批量更新
- mybatis 批量更新
- Mybatis批量更新
- Mybatis批量更新
- Mybatis批量更新数据
- Mybatis批量更新
- mybatis批量更新
- Mysql+Mybatis批量更新
- Mybatis批量更新数据
- iOS compare用法
- 数字前后加*号
- 深入解析String intern()
- spring与mybatis三种整合方法
- 对数运算基本公式
- Mybatis批量更新数据库
- 关于PHP CURL post数据时数据过大导致无法接收返回信息的处理方法
- Linux网络配置相关文件
- 【走过的弯路】UTF-8编码转GB2312
- Docker 私有仓库配置
- PIN, PIN2, PUK, PUK2
- ipad下的常见问题(持续更新)1
- 高效开发 Android App 的 10 个建议
- C# 对List<T>取交集、连集及差集