MyBatis BULK INSERT, batch update

Source: Internet
Author: User
Tags bulk insert
The rational use of batch inserts and updates has a great effect on performance optimization, and the speed is obviously N times faster.

Pay attention to the new addition of the database connection string: & allowMultiQueries = true, which means that a SQL can be split into multiple independent SQLs by a semicolon.

The maximum limit of batch insertion is mainly based on the length of your entire SQL, so you can configure the number of each batch to be batched according to the size of your own SQL length.

Bulk insert

Dao

public Integer saveStudents (List <Student> students) {
Super.getSqlSession (). Insert ("StudentMapper.insertStudents", students);
}
Mapper
<insert ID = "insertStudents" parameterType = "java.util.List">
Insert_student (
City_id
)
Value
<Foreach collection = "list" item = "itm" index = "index" separator = ",">
(
# {Itm.city_id}
()
</ Foreach>
</ insert>

Bulk update

Dao
public Integer updateStudents (List <Student> students) {
Super.getSqlSession (). Update ("StudentMapper.updateStudents", students);
}
Mapper

<update ID = "updateStudents" parameterType = "java.util.List">
<Foreach collection = "list" itemite = "item" index = "index" open = ""
Close = "" separator = ";">
UPDATE t_studnet
<Set>
If <if test = "item.name! = Null"
Name = name # {item.name}
... if it is </ if>
... set </ set>
WHERE ID = # {item.id}
AND AND age = = # {item.age}
</ Foreach>
</ update>

List batched tools
/ **
* Batch list
*
@Param sourceList
* List to be batched
@Param @batchCount
* The number of lists in each batch
@Return List <List <Object >>
* /
public staticList <List <? >> batchList (List <?> sourceList, int batchCount) {
List <List <? >> returnList = newArrayList <> ();
Int startIndex = 0; // start from 0th subscript
... (startIndex, <sourceList.size ()) {
Int index = 0;
(SourceList.size ()-batchCount <startIndex) {
EndIndex = sourceList.size ();
... else {
EndIndex = startIndex + batchCount;
}}
ReturnList.add (sourceList.subList (startIndex, endIndex));
StartIndex = startIndex + batchCount; // next batch
}
Return list;
}

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.