Mybatis batch inserts Oracle and MySQL_MySQL

Source: Internet
Author: User
Mybatis inserts data into Oracle and MySQL databases in batches. you must first understand the syntax supported by this database for batch inserts. The syntax for batch insert in each database is different. I will introduce two types.
MySQL: 1. insert into TABLE_NAME (ID, NAME) VALUES (1, 'Zhang San'), (2, 'Li si ')
2. insert into TABLE_NAME (ID, NAME) VALUES (1, 'Zhang San'); insert into TABLE_NAME (ID, NAME) VALUES (2, 'Li si ')
Oracle: 1. insert into TABLE_NAME (ID, NAME) (SELECT 1, 'Zhang San' from dual) union all (SELECT 2, 'Li Si' from dual)
2. insert into TABLE_NAME (ID, NAME) VALUES (1, 'Zhang San'); insert into TABLE_NAME (ID, NAME) VALUES (2, 'Li si ')
All four SQL statements are supported by the database. The second method is not recommended because it is far worse than the first one regardless of efficiency or readability.
Of course, more importantly, Mybatis does not support the second method, because one transaction of mybatis only supports one SQL statement.
Therefore, the first method in Mybatis can only be used. the code is as follows:
MySQL
INSERT INTO TABLE_NAME(ID,NAME) VALUES(#{itm.id},#{itm.name})
ORACLE:
INSERT INTO TABLE_NAME(ID,NAME) (SELECT #{itm.id},#{itm.name} FROM DUAL)
Note that the label must be , Cannot beOrOtherwise, an error is reported ..Don't ask your brother why he reported an error. he doesn't know either... (I am too lazy to check the information. if you are interested, you can check the information)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.