In the actual project, we will generally use the batch insert、delete、update and other operations, because the use of frequency or pretty high, here is a simple record for later study and reference.
BULK INSERT
In a database, bulk inserts can be multipleinsert into tableName values(?,?,?...);
or a piece ofinsert into tableName values (?,?,?...),(?,?,?...),(?,?,?...)....
So when we use mybatis , XML can write bulk operations like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<insert ID ="Insert-code-batch" parametertype="Java.util.List" > InsertIntoRedeem_code (bach_id, code, type, Facevalue,create_user,create_time) <foreach collection = "list" Item= "Reddemcode" index= "index" separator =< Span class= "string" "," ( #{ Reddemcode.batchid}, #{reddemcode.code}, #{reddemcode.type}, #{reddemcode.facevalue}, #{reddemcode.createuser}, #{reddemcode.createtime} </foreach, </insert; |
Label Description:
- The properties of the Foreach element are mainly item,index,collection,open,separator,close.
- Item represents the alias at which each element in the collection iterates
- index specifies a name that is used to indicate where each iteration takes place during the iteration
- Open indicates what the statement starts with
- Separator indicates what symbol is used as a delimiter between each iteration
- Close indicates what ends with
- Collection property is required! It could be list,array,map.
Attention
The tag here is foreach not included insert into tableName , so the last actual execution is an SQL statement:
Insert into Redeem_code (batch_id, code, type, Facevalue,create_user,create_time)
Values
(?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? )
Recommended MyBatis Blog Column
Batch Update
MyBatis the bulk of the focus is how to connect SQL into a database can be executed directly in the SQL, here to record the bulk operation of Ibatis , as long as the one, the other is not a problem.
1 2 3 4 5 6 7 8 9 10 |
<update id="Wholesale-update-rebate-config-by-productcode" >
Rebate_config SET Rebate_type = ' By_rate ', rebate_config_value = #rebateConfigValue # WHERE <iterate property="list" conjunction=open=close=")" > Product_code =#list[]# </iterate> </update>
|
Label Description:
- Prepend the parts of an SQL statement that can be overwritten, added to the front of the statement (optional)
- The property type is java.util.List for the traversed element (required)
- Open the entire string starting with the content body to define parentheses (optional)
- Close the entire string that iterates over the body of the content, defining parentheses (optional)
- Conjunction a string between each iteration of the content, used to define and or or (optional)
Attention
The argument I'm passing here is a map<string,object>.
1 2 3
|
map<new hashmap<object> (); Conditions.put (//rebatevalue is a fixed value Conditions.put ("list", productcodelist);
|
When used, it is important to include square brackets [] After the list element name, square brackets [] to mark the object as list, in case the parser simply outputs the list to string
Ibatis Official Document Description
Ibatis Learning Website
MyBatis Bulk Operation-xml mode