mybatis batch

Batch delete:
<delete id= "deleteBatchByXXX" parameterType= "list">
       delete from table name where groupon_id in
       <foreach collection="list" item= "item" index ="index"
            open= "(" close =")" separator=",">
            #{item}
       </foreach >
       </delete >
Note that foreach is a loop, used to read the incoming list parameter. Batch processing is the type of parameterType that must be noted. The collection attribute in the foreach tag indicates what type of collection is passed in. item represents a quantity in the collection similar to
List<String>list;
for(String str:list){
     ...
}
item is equivalent to the function of str, which is used to traverse the collection. index is the index of the collection. open indicates where the label starts, and close indicates where the label ends. seprator represents the interval between elements.

Bulk insert:
<insert id="
       insert into table name( uid, groupon_id, create_time, receive_time) values
    ​​<foreach collection="list" item="item" index="index" separator=",">
       (#{item.uid,jdbcType=BIGINT}, # {item.grouponId,jdbcType=BIGINT},
      #{item.createTime,jdbcType=INTEGER}, #{item.receiveTime,jdbcType=INTEGER})
    </foreach >
</insert>
The usage is basically the same as batch deletion, here you need to pay attention to item .XXX means to get the XXX property of the object.

Batch update:
<update id= "updateSubmitTimeByUids" parameterType= "map">
    update table name
    set submit_time = #{submitTime,jdbcType=BIGINT} where uid in
    <foreach collection="list" item= "uid" index="index"


     </foreach >
    </update >
The usage is basically the same as before, but it should be noted that the parameter passed in is of type map.

Batch query:
<select id="selectBySomePoiIds"resultType="list"parameterType="java.util.Map"> 
     SELECT <include refid="Base_Column_List" /> FROM table name   
      WHERE poi_id in           
      <foreach collection="poiIds" item= "poiId" index="index" open="(" close=")" separator=","> #{poiId}</foreach> AND pass_uid = #{passUid}
     <if test="status != null"> 
      AND status = #{status,jdbcType=BIGINT}         
     </if> </select>
Note that the usage of tags is similar to the above, all of which are worthy of batch query by passing in a collection object.

The core of batch operation is to pass in multiple data at a time and then perform related operations

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327027224&siteId=291194637