Now a lot of companies and individuals start to use the MyBatis framework, and the MyBatis framework is an ORM framework, so the database insert, UPDATE, delete that must be necessary, but there is a problem is the performance of the problem.
Let's just say this: seeing someone using MyBatis to bulk Delete bulk additions also uses a for loop in the program to invoke the method, which is true, but slightly lower performance, so here are a few examples of batch processing, relatively directly in the program for the loop efficiency is a little higher:
1, Batch add:
<insert id= "Batchinsert" parametertype= "java.util.List" >
insert INTO STUDENT (id,name,sex,tel,address)
VALUES
<foreach collection= "list" item= "item" index= "index" separator= "," >
(#{item.id},#{ Item.name},#{item.sex},#{item.tel},#{item.address})
</foreach>
</insert>
2, Batch modification:
<update id= "BatchUpdate" parametertype= "java.util.List" >
update STUDENT SET name = "All" WHERE ID in
< foreach collection= "list" item= "item" index= "Index" open= "(" separator= "," close= ")" >
#{item}
</ Foreach>
</update>
3, Bulk Delete:
<delete id= "Batchdelete" parametertype= "java.util.List" >
delete from STUDENT WHERE ID in
<foreach collection= "list" index= "index" item= "Item" open= "(" separator= "," close= ")" >
#{item}
</foreach>
</delete>
This batch inserts, modifies, deletes the way faster than the program in the For loop calls the reason as follows: (1), the network transmits the data quantity to be few, certainly passes the time to be much less. (2), the number of requests for database services is few, because the connection database service is very time-consuming (so out of the database connection pool). (3), MyBatis in the execution of the time will be acquired connection, statement objects so do not want to loop to create a lot of objects.
(4), mybatis of the implementation of three kinds namely: simple, reuse, BATCH three ways, if convenient to set up a bit, if you use easy every execution to create statement objects and execute, if you use reuse The PreparedStatement object is reused for execution and batch is executed in bulk, and MyBatis is executed by default, so we should be aware that the minimum is reuse execution.
There are three ways to do this before, and it's not repeated here. Three ways to do this article address: Click the Open link