mybatis 批处理

批量删除:
<delete id= "deleteBatchByXXX" parameterType= "list">
       delete from 表名 where groupon_id in
       <foreach collection="list" item= "item" index ="index"
            open= "(" close =")" separator=",">
            #{item}
       </foreach >
       </delete >
注意,foreach是循环,用来读取传入的list参数。批量处理是parameterType的类型必须要注意。foreach标签中的collection属性表示传入的是什么集合类型。item表示的是集合中的一个量类似于
List<String>list;
for(String str:list){
     ……
}
item就相当于str的作用,用来遍历collection。index就是集合的索引。open表示标签以什么开始,close表示标签以什么结束。seprator表示元素之间的间隔。

批量插入:
<insert id="insertBatch" >
       insert into 表名 ( 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>
用法基本同批量删除,这里需要注意item.XXX表示获取该对象的XXX属性。

批量更新:
<update id= "updateSubmitTimeByUids" parameterType= "map">
    update 表名
    set submit_time = #{submitTime,jdbcType=BIGINT} where uid in
    <foreach collection="list" item= "uid" index ="index"
            open= "(" close =")" separator=",">
            #{ uid}
     </foreach >
    </update >
用法和之前的基本相同,但是需要注意传入的参数是map类型。

批量查询:
<select id="selectBySomePoiIds"resultType="list"parameterType="java.util.Map"> 
     SELECT <include refid="Base_Column_List" /> FROM 表名   
      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>
注意标签的用法和上面的大同小异,都是通过传入一个集合对象来进行值得批量查询。

批量操作核心就是一次传入多个数据然后进行相关操作

猜你喜欢

转载自guanjiall.iteye.com/blog/2292859