Insert into select usage in mybaties, mybaties BULK Insert

Source: Internet
Author: User
Tags foreach bulk insert table name

In recent projects used in the station letter function, the demand is the station letter to all users and designated users, in order to optimize the performance of the database, greatly reduce the number of access to the database, so in the mybaties used in bulk Delete, directly on the code bar.

1. Send to some users, BULK Insert function

Messagedaoimpl.java

[Java] View plain copy public void Sendpartmessage (list<usermessage> list) throws Exception {//list is stored to send in-station credit   The user's ID getsqlsession (). Insert ("Message.sendpartmessage", list); }


Message-mapper.xml

[java]  View plain  copy <insert id= "Sendpartmessage"  usegeneratedkeys= "true"   Parametertype= "Java.util.List" >    insert into usermessage (   Sender, CONTENT,MSGDATE,READSTATUS,USERID,PARENTID,MSGID,ORDERID,TYPEITEM,SUBNAME,TYPE,NAME,CONTENTM)  VALUES         <foreach collection= "list"  index= "index"  item= " Item " separator=", " >    <--  loops through the IDs of all specified users for BULK insert-->                    (#{item.sender },#{item.content},#{item.msgdate},#{item.readstatus},#{item.userid},   [java]  View Plain   Copy                 0,#{ ITEM.MSGID},0,#{ITEM.TYPEITEM},#{ITEM.SUBNAME},#{ITEM.TYPE},#{ITEM.NAME},#{ITEM.CONTENTM})            <span style= "White-space:pre" >      </span></foreach>     </insert>  
2. Send the In-station letter insert into Select for the user you are using

All user IDs are queried from the user table and inserted to access only one database at a time

Messagedaoimpl.java

[Java] View plain copy <span style= "White-space:pre" > </span>public void sendallmessage (Usermessage userm essage) {<span style= "White-space:pre" > </span>getsqlsession (). Insert ("Message.sendallmessage", UserM   Essage); <span style= "White-space:pre" > </span>} message-mapper.xml

[java]  View plain  copy <insert id= "sendallmessage"  parametertype= "UserMessage" >     insert into usermessage (Sender,content,msgdate,readstatus,userid,parentid,msgid, ORDERID,TYPEITEM,SUBNAME,TYPE,NAME,CONTENTM)         select #{sender},#{ CONTENT},#{MSGDATE},#{READSTATUS},USERINFO.ID,0,#{MSGID},0,#{TYPEITEM},#{SUBNAME},#{TYPE},#{NAME},#{CONTENTM}         from UserInfo    </insert>     INSERT into   table name (Field 1, Field 2, Field 3, ...)  select attributes (that is, variables) in usermessage Userinfo.id for all user IDs that are isolated from the UserInfo table, so that users who are not in the table that is in the station's letter are inserted into a station letter corresponding to their ID.

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.