Bulk Add, delete, modify in MyBatis

Source: Internet
Author: User
Tags foreach

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


Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.