Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/。目前想批量更新,如果update的值是相同的话,很简单,组织
update table set column='...' where id in (1,2,3)l
这样的sql就可以了。Mybatis中这样写就行
UPDATE STUDENT SET name = #{name} WHERE id IN
#{item}
但是这样的需求很少,一般是有个集合,每个元素中的值是不一样的,然后需要一次性更新。一般的处理方式是使用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也有效。
最开始的时候,想着写一系列并列的更新语句就可以了
open="" close="">
update REGION_CODE set
CODE=#{item.Code,jdbcType=VARCHAR},
NAME=#{item.Name,jdbcType=VARCHAR}
where ID = #{item.id,jdbcType=DECIMAL}
这样直接报错,因为Mybatis映射文件中的sql语句不允许 ; 符号。按照可行的case when处理方式,Mybatis映射文件书写方式如下:
update REGION_CODE set
CODE=
when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR}
,NAME=
when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR}
where ID in
#{item.id,jdbcType=DECIMAL}
至此,批量更新功能完成。
--------------------------------------------------------------------------------------------------------------------------------
原mybatis执行批量更新batch update 的方法(oracle,mysql)
oracle数据库:
update test
test=${item.test}+1
where id = ${item.id}
mysql数据库:
mysql数据库采用一下写法即可执行,但是数据库连接必须配置:&allowMultiQueries=true
例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
update test
test=${item.test}+1
where id = ${item.id}
------------------------------------------------------------------------------------------------------------------------------
https://my.oschina.net/zouqun/blog/405424
--------------------------------------------------------------------------------------------------------------------------------
2015-03-02 18:10 3993人阅读
版权声明:本文为博主原创文章,未经博主允许不得转载。
update tmi_licai_all t
set
t.est_amount=#{item.estAmount,jdbcType=NUMERIC}
where t.licai_id = #{item.licaiId,jdbcType=NUMERIC}