Sometimes, we need to insert a lot of data at one time or update, delete a lot of data, so in order to improve efficiency, we need to use batch processing to complete. The following discusses how Mybatis+spring uses batches.
First of all should know MyBatis is Ibatis upgrade version, better than Ibatis performance, this is the reason why I use MyBatis.
Ibatis is supported for batch processing:
Public void batchaddexamlog (list examloglist) throws SQLException{ sqlmapclient smc=this.getsqlmapclient (); try { smc.starttransaction (); smc.startbatch (); &NB Sp &NBS P &NB Sp   &NB Sp for (iterator iter = examloglist.iterator () iter.hasnext ();) { Examlog log = (Examlog) iter.next (); smc.update ("InsertExamlog", log); } smc.executebatch (); }finally{ &NBS P &NB Sp smc.committransaction (); smc.endtransaction (); } }
Both of the above, they can implement batch processing. However, when MyBatis + spring is integrated and the transaction is controlled by spring, mybatis This batch is not available. Because Sessionfactory.opensession (Executortypetype), the method creates a new database connection that is not spring-controlled and causes spring's transaction control to fail. It cannot take advantage of the session that is bound to the current thread.
Look at other ways:
1, MyBatis The main configuration file, add the following code: <configuration> <settings> <setting name= "Defaultexecutortype" value= "BATCH" /> </settings> </configuration>-This is a total configuration, all operations will take effect, changes made in any one transaction are committed at the end of the transaction, and the results of their operations cannot be read within the transaction. --it causes all method inserts to not return Id,update/delete cannot get the number of affected rows, so this global setting is not good.
2, with (1) The use of Sqlsessionutils.getsqlsession (sessionfactory). commit ();
--although data is committed within a transaction, it is still not possible to obtain the self-added ID
3, mapper file using the foreach tag assembly statement
-but the SQL statement has a length limit, and small amounts of data can (of course, set the maximum SQL limit from the database).
4, get the current connection, use JDBC for batch operation.
That's not good, SQL needs to be written somewhere else, not in the mapper file.
5, from the spring context, instantiate two sqlsessiontemplate, one using batch processing, one does not use batch processing, in the program timely and use.
--Cannot get the ID, and cannot use two sqlsessiontemplate in one transaction.
6, from the source code, expand their own batch processing implementation. 1. The Batchexecutor class's public list<batchresult> doflushstatements () throws SqlException method eventually performs a bulk operation, which returns impact information. 2. The Batchexecutor.doflushstatements () method is invoked in the Executor.commit () method. 3, Executor.commit () is called in sqlsession. 4, Sqlsessionfactory is responsible for creating sqlsession.
Extension Sqlsessionfactory Create our defined mysqlsession,mysqlsession can eventually invoke to batchexecutor.doflushstatements () get impact results
-This approach requires extending the source code.
Summed up several ways, everyone in the project to see the use of it.
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