• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
山高我为峰
博客园    首页    新随笔    联系   管理    订阅  订阅
Mybatis foreach

批量删除:

<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>

注意标签的用法和上面的大同小异,都是通过传入一个集合对象来进行值得批量查询

posted on 2016-06-12 20:39  山高我为峰  阅读(824)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3