Environment: MyBatis + oracle11g R2
1. Use Direct path insertion ("/*+append_values/" in the following SQL statement) 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 the number of inserts, the execution rate is suddenly slow, so should be inserted in batches:
public void Save (list<uidcodebean> uidcodelist) throws Exception {sqlsession = null; try {batchsqlsession = Sqlsessionfactory.opensession (Executortype.batch, false);//Get the sqlsession int Batchcoun of the batch method t = 1000;//Number of commits per batch int batchlastindex = batchcount-1;//subscript for the last of each batch (int index = 0; index < Uidcodelist.siz
E ()-1;) {if (Batchlastindex > 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 cycle}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 (); }
}