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.