Home > Database > Mysql Tutorial > body text

Mybatis批量插入Oracle、MySQL_MySQL

WBOY
Release: 2016-06-01 13:13:51
Original
1156 people have browsed it

要做批量插入数据库,首先得知道该数据库对批量插入所支持的语法。每个数据库批量插入的语法都不一样,我介绍两种。
MySQL:1、INSERT INTO TABLE_NAME(ID,NAME)VALUES(1,'张三'),(2,'李四')
              2、INSERT INTO TABLE_NAME(ID,NAME)VALUES(1,'张三');INSERT INTO TABLE_NAME(ID,NAME)VALUES(2,'李四')
Oracle:1、INSERT INTO TABLE_NAME (ID,NAME) (SELECT 1,'张三' from dual) union all (SELECT 2,'李四' from dual)
             2、INSERT INTO TABLE_NAME (ID,NAME) VALUES(1,'张三');INSERT INTO TABLE_NAME(ID,NAME)VALUES(2,'李四')
4条SQL都是数据库支持的写法。两者的第二种是不推荐的写法,因为不管效率还是可读性上都远差于第一种
当然,还有更重要的一点,Mybatis不支持第二种写法,因为mybatis的一个事务只支持一条SQL
所以,Mybatis里写法只能取两者的第一种,代码如下
MySQL

<select id="batchSave" parametertype="java.util.List">INSERT INTO TABLE_NAME(ID,NAME) VALUES<foreach collection="list" item="itm" separator=",">(#{itm.id},#{itm.name})</foreach></select>
Copy after login
ORACLE:
<select id="batchSave" parametertype="java.util.List">INSERT INTO TABLE_NAME(ID,NAME) <foreach collection="list" item="itm" separator="union all">(SELECT #{itm.id},#{itm.name} FROM DUAL)</foreach></select>
Copy after login
这里要注意了,标签一定得是
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template