zoukankan      html  css  js  c++  java
  • Mybatis批量操作

    1、批量删除:

    <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表示元素之间的间隔。
    2、批量插入:

    <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属性。
    3、批量更新:

    <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类型。 
    4、批量查询:

    <?xml version="1.0" encoding="utf-8"?>
    <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>

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

    每天进步一步步,坚持坚持坚持
  • 相关阅读:
    js的深拷贝特别注意this的深拷贝
    快速的熟悉一个angular的项目从run看起
    关于angular路由中的#
    AngularJS的Provider, Value, Constant, Service, Factory, Decorator的区别与详解
    css页面缩放
    jquery自定义window事件
    js自定义事件
    git分支
    webpack知识小结--require.context方法
    Vue 创建组件的两种方法
  • 原文地址:https://www.cnblogs.com/zhizhao/p/7808819.html
Copyright © 2011-2022 走看看