Methods and Techniques for batch database insertion using different ORM frameworks in java

Source: Internet
Author: User

Methods and Techniques for batch database insertion using different ORM frameworks in java
1. Use executeBatch in jdbc Mode

In JDBC, you can use the executeBatch () method to reduce the number of database calls, such:

 

Statement pstmt = conn. createStatement (); pstmt. addBatch (insert into settings values (3, 'xiaomi ', 'female'); pstmt. addBatch (insert into settings values (4, 'lily', 'female); pstmt. addBatch (insert into settings values (5, 'John, Male'); pstmt. addBatch (insert into settings values(6, 'xiaoming ', 'male' too many pstmt.exe cuteBatch ();

2. Use hibernate framework session. save () to save data
To implement batch insert in hibernate, you must control the size of the first-level cache by calling flush () and calling clear () later.

 

 

Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); for (int I = 0; I <100000; I ++) {Student entity = new Student (.....); session. save (entity); if (I % 50 = 0) {// write 50 pieces of data into the database at a time and release the memory session. flush (); session. clear () ;}} tx. commit (); session. close ();

3. Batch add data using the MyBatis framework
In the insert tag of the corresponding xml in the mapping File of MyBatis, insert values cyclically. The following code is used:

 

 

 
       insert into STUDENT( ID,NAME,SEX)    values     
  
           ( #{item.id},#{item.name},#{item.sex})     
   
 


 

 

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.