Mybatis batch inserts a large number of oracle data records to solve the performance problem. mybatisoracle
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: Previously, it was a one-time commit. As the number of inserts increases, the execution speed suddenly slows down. Therefore, insert data in batches:
Public void save (List <UidCodeBean> uidCodeList) throws Exception {SqlSession batchSqlSession = null; try {batchSqlSession = sqlSessionFactory. openSession (ExecutorType. BATCH, false); // obtain sqlsessionint batchCount = 1000; // The number of commit entries in each BATCH int batchLastIndex = batchCount-1; // The subscript for (int index = 0; index <uidCodeList. size ()-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; // after data is inserted, exit the loop} else {batchSqlSession. insert (NAMESPACE + ". addUidCodeBatch ", uidCodeList. subList (index, batchLastIndex); batchSqlSession. commit (); System. out. println ("index:" + index + "batchLastIndex:" + batchLastIndex); index = batchLastIndex + 1; // set the next batch of subscripts batchLastIndex = index + (batchCount-1) ;}} finally {batchSqlSession. close ();}}
Mybatis uses oracle to insert data and returns the primary key.
Which of the following rows should be placed after SQL?
<SelectKey resultType = "java. lang. Integer" keyProperty = "ID" order = "BEFORE">
SELECT IBOKEE_COMM_TAG_LIBRARY_SEQ.nextval AS Id FROM DUAL
</SelectKey>
Mybatis implements oracle batch insert java method
When inserted in java code, it is cyclically placed into batch. Then, you only need to process the batch once ..