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

    
    
    批量插入
    void batchInsertBackup(List<UserRights> list);
    <insert id ="batchInsertBackup" parameterType="java.util.List" >
    insert into user_rights_backup
    (id,supplier_id,user_id,rights_id,rules_id,
    start_time,end_time,goods_no,has_num,type,
    status,ctime,mtime,biz_type,first_type,
    is_limit_time)
    values
    <foreach collection ="list" item="entityRecord" index= "index" separator =",">
    (
    #{entityRecord.id}, #{entityRecord.supplierId},#{entityRecord.userId},#{entityRecord.rightsId},#{entityRecord.rulesId},
    #{entityRecord.startTime}, #{entityRecord.endTime}, #{entityRecord.goodsNo},#{entityRecord.hasNum},#{entityRecord.type},
    #{entityRecord.status},#{entityRecord.ctime},#{entityRecord.mtime},#{entityRecord.bizType},#{entityRecord.firstType},
    #{entityRecord.isLimitTime}
    )
    </foreach >
    </insert >

    批量删除源数据
    void batchDelete(List<UserRights> list);
    <delete id="batchDelete" parameterType="java.util.List" >
    delete from user_rights where id in (
    <foreach collection="list" item="entity" index= "index" separator=",">
    #{entity.id}
    </foreach>
    )
    </delete>


    根据id,批量查询
    List<UserRights> batchQuery(List<Long> idList);
    <select id="batchQuery" resultMap="BaseResultMap" parameterType="java.util.List">
    select
    <include refid="base_column_list"/>
    from user_rights
    where id in
    <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item,jdbcType=BIGINT}
    </foreach>
    </select>


    根据id,批量更新时间和状态
    <update id="updateBatch" parameterType="java.util.List">
    update user_rights
    <trim prefix="set" suffixOverrides=",">
    <trim prefix="status = case" suffix="end,">
    <foreach collection="list" item="item" index="index">
    when id=#{item.id} then #{item.status}
    </foreach>
    </trim>
    <trim prefix=" start_time = case" suffix="end,">
    <foreach collection="list" item="item" index="index">
    when id=#{item.id} then #{item.startTime}
    </foreach>
    </trim>
    </trim>
    where id in
    <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item.id,jdbcType=BIGINT}
    </foreach>
    </update>

    首先,mysql需要数据库连接配置&allowMultiQueries=true

    jdbc:mysql://127.0.0.1:3306/mybank?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

    oracle下支持执行多条语句,下面3个相同

    复制代码
    <update id="batchUpdate" parameterType="java.util.List">
            <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";" > 
                update T_EMP_1 
                <set>       
                    age = #{item.age}+1,name=#{item.name}
                </set>
                where id = #{item.id}
            </foreach>
        </update>
    复制代码
    复制代码
    <update id="batchUpdate" parameterType="java.util.List">
            <foreach collection="list" item="item" index="index" open="begin" close="end;" separator="" > 
                update T_EMP_1 
                <set>       
                    age = #{item.age}+1,name=#{item.name}
                </set>
                where id = #{item.id};
            </foreach>
        </update>
    复制代码
    复制代码
    <update id="batchUpdate" parameterType="java.util.List">
            begin
            <foreach collection="list" item="item" index="index" separator="" > 
                update T_EMP_1 
                <set>       
                    age = #{item.age}+1,name=#{item.name}
                </set>
                where id = #{item.id};
            </foreach>
            end;
        </update>
    复制代码

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况: 
    1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list 
    2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array 
    3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key

    批量删除

    <delete id="batchDeleteStudent" parameterType="List">  
        DELETE FROM STUDENT WHERE id IN  
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">   
            #{item}   
        </foreach>  
    </delete>

    批量更新 注意:oracle中 形如 update *** set *** where ** in(....) 这种语句 in所在的集合有条数限制 为1000条

    <update id="batchUpdateStudent" parameterType="List">  
        UPDATE STUDENT SET name = "5566" WHERE id IN  
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >  
            #{item}  
        </foreach>  
    </update>
    <update id="batchUpdateStudentWithMap" parameterType="Map" >  
        UPDATE STUDENT SET name = #{name} WHERE id IN   
        <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">   
            #{item}   
        </foreach>  
    </update>

    批量插入  mysql和oracle不一样

    复制代码
    mysql:
    <insert id="batchInsertStudent" parameterType="java.util.List">  
        INSERT INTO STUDENT (id,name,sex,tel,address)  
        VALUES   
        <foreach collection="list" item="item" index="index" separator="," >  
            (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})  
        </foreach>  
    </insert> 
    复制代码
    复制代码
    oracle:
    <insert id="insertBatch4Oracle" parameterType="List">
            insert into aa(a,b)
            <foreach collection="list" item="item" index="index" separator="union all" >
               select  #{item.a},#{item.b} from dual
            </foreach>
     </insert>
  • 相关阅读:
    read-uncommited 下脏读的实现
    MySQL 加锁处理分析【重点】
    Next-key locking是如何解决幻读(当前读)问题的
    spring ioc
    讨论 update A set number=number+ ? where id=?的原子性 (含数据库原理)
    PESSIMISTIC_READ & PESSIMISTIC_WRITE 与 共享锁 & 排它锁
    innodb当前读 与 快照读 and rr级别是否真正避免了幻读
    java finalize及实践
    uva 539 The Settlers of Catan
    zoj 1016 Parencodings
  • 原文地址:https://www.cnblogs.com/ctaixw/p/6037054.html
Copyright © 2011-2022 走看看