MyBatis Bulk Operation-xml Mode

Source: Internet
Author: User

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:

    1. The properties of the Foreach element are mainly item,index,collection,open,separator,close.
    2. Item represents the alias at which each element in the collection iterates
    3. index specifies a name that is used to indicate where each iteration takes place during the iteration
    4. Open indicates what the statement starts with
    5. Separator indicates what symbol is used as a delimiter between each iteration
    6. Close indicates what ends with
    7. 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:

    1. Prepend the parts of an SQL statement that can be overwritten, added to the front of the statement (optional)
    2. The property type is java.util.List for the traversed element (required)
    3. Open the entire string starting with the content body to define parentheses (optional)
    4. Close the entire string that iterates over the body of the content, defining parentheses (optional)
    5. Conjunction a string between each iteration of the content, used to define and or or (optional)

Attention

    1. 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);
    2. 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

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.