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.
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;
}
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.