怎样做视频直播网站,电商网名大全,房产类网站建设费用,企业网站源码 可去版权最近看一个老项目#xff0c;使用的Oracle数据库#xff0c;发现要使用MyBatis执行批量操作还是不太一样。
下面我们来看一下#xff0c;Oracle数据库#xff0c;如何使用MyBatis来批量插入和更新。
批量插入
因为Oracle和MySQL的insert还不太一样#xff0c;Oracle不能…最近看一个老项目使用的Oracle数据库发现要使用MyBatis执行批量操作还是不太一样。
下面我们来看一下Oracle数据库如何使用MyBatis来批量插入和更新。
批量插入
因为Oracle和MySQL的insert还不太一样Oracle不能像MySQL直接语法插入多条
INSERT INTO table_name(column1, column2, column3)
VALUES
(value1, value2, value3),
(value4, value5, value6),
(value7, value8, value9);所以需要使用insert all来变通一下
insert allinto table_name values(1,张三,49)into table_name values(2,李四,39)into table_name values(3,王五,29)into table_name values(4,赵六,19)into table_name values(5,孙七,59)
select 1 from dual;上面的table_name可以是不同的表。
简单来说就是把插入语句放在insert all和select 1 from dual;之间
注意中间的插入语句没有insert
在MyBatis中我们使用foreach拼装一下open就是insert all中间是into 插入语句close是select 1 from dual
注意最后没有分号除非你url中设置了allowMultiQueriestrue才可以带分号结尾否则MyBatis执行会出错。
下面是具体的示例
insert idinsertSelectiveList parameterTypevip.meet.BusinessDateTypeforeach collectionlist itemitem indexindex separator openinsert all closeselect 1 from dualinto business_date_typetrim prefix( suffix) suffixOverrides,if testitem.pkId ! nullPK_ID,/ifif testitem.fid ! nullFID,/ifif testitem.rowIndex ! nullROW_INDEX,/ifif testitem.dataType ! nullDATA_TYPE,/ifif testitem.lastModifyTime ! nullLAST_MODIFY_TIME,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testitem.pkId ! null#{item.pkId,jdbcTypeNUMERIC},/ifif testitem.fid ! null#{item.fid,jdbcTypeNUMERIC},/ifif testitem.rowIndex ! null#{item.rowIndex,jdbcTypeNUMERIC},/ifif testitem.dataType ! null#{item.dataType,jdbcTypeNUMERIC},/ifif testitem.lastModifyTime ! null#{item.lastModifyTime},/if/trim/foreach
/insert批量更新
Oracle没有批量更新的语句怎么批量更新呢
可以使用begin,end语句块可以把update语句放在begin和end之间执行。
BEGINUPDATE business_date_type SET price 2.99 WHERE FID 27210303266880UPDATE business_date_type SET price 4197276.99 WHERE FID 27210303266880;
END;在MyBatis中我们使用foreach拼装一下open就是begin中间是update语句close是;end;
close为啥是;end;呢因为foreach的separator最后一个语句不会添加所以少一个需要补一个。 end后面的分号(;)是因为begin end;语法这个分号不能省略必须添加。
为什么有的不能有分号有分号出错begin end又必须有分号没有分号出错呢
我想大概是因为其他都是一个完整语句begin end是一个语句块可能有多条语句必须通过分号(;)来判断结束吧。
update idupdateList parameterTypejava.util.Listforeach collectionlist itemitem indexindex openbegin close;end; separator;update business_date_typesetif testitem.dataType ! nullDATA_TYPE #{item.dataType,jdbcTypeVARCHAR},/ifif testitem.lastModifyTime ! nullLAST_MODIFY_TIME #{item.lastModifyTime},/if/setwhere FID #{item.fid,jdbcTypeNUMERIC}/foreach
/update