Environment: MyBatis + oracle11g R2
1. Use Direct path insertion ("/*+append_values */" in the SQL statement below) and use the keyword "union ALL":
<insert id= "Adduidcodebatch" parametertype= "java.util.List" > INSERT INTO/*+append_values */ T_ Uid_code (C_uid_code, C_serail_len, c_batch_code, c_type, c_create_time, c_super_code , C_security_code, c_serial_code ) <foreach collection= "list" item= "item" index= "index" separator= " UNION ALL "> select #{item.uidcode}, #{item.kcode}, #{item.batchcode}, #{item.type}, Sysdate, #{item.supercode}, #{item.securitycode}, #{item.serialcode} from dual </ Foreach> </insert>
2.dao layer implementation: Before a one-time commit, this will increase with the number of inserts, the execution speed is very slow, so should be inserted in batches:
public void Save (list<uidcodebean> uidcodelist) throws Exception {sqlsession batchsqlsession = null;try { Batchsqlsession = Sqlsessionfactory.opensession (Executortype.batch, false);//Get batch mode Sqlsessionint BatchCount = 1000 ;//The number of commits per batch int batchlastindex = batchcount-1;//The last subscript for each batch for (int index = 0; index < uidcodelist.size ()-1;) {if (BATC Hlastindex > Uidcodelist.size ()-1) {Batchlastindex = Uidcodelist.size ()-1;batchsqlsession.insert (NAMESPACE+). Adduidcodebatch ", Uidcodelist.sublist (Index, Batchlastindex)); Batchsqlsession.commit (); System.out.println ("Index:" +index+ "Batchlastindex:" +batchlastindex); break;//data is inserted, exit loop}else{ Batchsqlsession.insert (namespace+ ". Adduidcodebatch", Uidcodelist.sublist (Index, batchlastindex)); Batchsqlsession.commit (); System.out.println ("Index:" +index+ "Batchlastindex:" +batchlastindex); index = Batchlastindex + 1;// Set Next batch Subscript Batchlastindex = index + (batchCount-1);}}} Finally{batchsqlsession.close ();}}
MyBatis BULK INSERT Oracle Massive data logging performance problem Resolution