Mybatis的批量更新 bug
来源:互联网 发布:数字滤波算法快速滤波 编辑:程序博客网 时间:2024/06/09 02:11
Mybatis的批量更新
Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/。目前想批量更新,如果update的值是相同的话,很简单,组织
update table set column='...' where id in (1,2,3)l
这样的sql就可以了。Mybatis中这样写就行
<update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
UPDATE STUDENT SET name = #{name} WHERE id IN
<foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</update>
但是这样的需求很少,一般是有个集合,每个元素中的值是不一样的,然后需要一次性更新。一般的处理方式是使用for循环。这样的效率较低,当数据量大时,期望有种一次性插入的操作。如果使用的是mysql,有
insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo) on duplicate key update
和
replace into table (aa,bb,cc) values(xxx,xxx,xxx),(ooo,ooo,ooo),(ccc,ccc,ccc)
两种方式可以处理。
当前数据库是oracle,可以使用case when来拼成一长串sql处理
UPDATE mytable
SET myfield = CASE id
WHEN 1 THEN 'value'
WHEN 2 THEN 'value'
WHEN 3 THEN 'value'
END
WHERE id IN (1,2,3)
实际上这种方式对于mysql也有效。
最开始的时候,想着写一系列并列的更新语句就可以了
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";"
open="" close="">
update REGION_CODE set
CODE=#{item.Code,jdbcType=VARCHAR},
NAME=#{item.Name,jdbcType=VARCHAR}
where ID = #{item.id,jdbcType=DECIMAL}
</foreach>
</update>
这样直接报错,因为Mybatis映射文件中的sql语句不允许 ; 符号。按照可行的case when处理方式,Mybatis映射文件书写方式如下:
<update id="updateBatch" parameterType="java.util.List">
update REGION_CODE set
CODE=
<foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR}
</foreach>
,NAME=
<foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR}
</foreach>
where ID in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=DECIMAL}
</foreach>
</update>
至此,批量更新功能完成。
总结:批量更新的时候,用;号结束不行。
http://ljhzzyx.blog.163.com/blog/static/38380312201482434852742/
0 0
- Mybatis的批量更新 bug
- Mybatis的批量更新
- Mybatis的批量更新
- mybatis 批量的更新
- mybatis 的批量更新
- MyBatis的批量更新
- MyBatis的批量更新对象
- mybatis+mysql的批量更新
- mybatis批量新增,更新的一些问题
- mybatis批量更新的一些问题
- mybatis的批量插入与更新
- 关于mybatis的批量更新完整版
- 基于MyBatis的批量插入更新实现
- mybatis 修改不同内容的批量更新
- Mybatis的批量插入与更新
- mybatis批量更新的几种方式
- MyBatis(mysql) 批量更新的坑
- mybatis批量更新不同值的字段
- Redis 3.0 cluster 集群环境搭建
- Druid连接池 一个设置 removeAbandonedTimeout
- ListView的简单使用
- 第一章 Java基础之JDK动态代理
- mybatis 批量增加 Parameter '__frch_item_0' not found. Available parameters are [lis
- Mybatis的批量更新 bug
- mybatis批量操作
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in
- word 文档中嵌入和链接OLE 对象|Packager-对象包装程序
- 崩溃统计
- Jenkins入门系列之——02第二章 Jenkins安装与配置
- ListenableFuture demo
- java第二十一节-字节流FileOutputStream-写
- Elasticsearch的Mapping之元数据类型