zoukankan      html  css  js  c++  java
  • mybatis 使用批量的方式进行 insert插入和update 修改

    在Java代码种频繁调用sql进行处理数据是比较费时间的。

    那么对于插入这种我们可用mybatis的批量插入进行insert数据 而不是循环一次调一次insert

    写法:

    mapper:

    Integer  createPlanByListEntity(List<Entity> list);
    xml:parameterType就是传入的list<实体>
    <insert id="自己定义的id名" parameterType="java.util.List" >
        insert into 表名 (
            id, STATUS,CREATE_USER_NAME,CREATE_USER_ID,CREATE_TIME
        ) values 
        <foreach item="item" collection="list" index="index" separator=",">
            (#{item.id},#{item.status},#{item.whsCompanyId},#{item.whsCode},#{item.whsName},#{item.appointmentType},#{item.startTime},#{item.endTime},
            #{item.intervalTimeUnit},#{item.intervalTime},#{item.appointmentDay},#{item.appointmentStockType},#{item.appointmentUnit},
            #{item.appointmentWeight},#{item.createUserName},#{item.createUserId},#{item.createTime} 
            )
        </foreach>
       </insert>

    批量修改写成

    update 表名 set status =1 where id =1;

    update 表名 set status =2 where id =2;

    这种格式就是分号隔开的多个update

    代码  只贴xml 了  

    <update id="自己起个"  parameterType="java.util.List">
        <foreach collection="list" item="item"  separator=";">
            update 表名
            <set>
                 
                <if test="item.updateUserName != null and item.updateUserName != ''">
                    UPDATE_USER_NAME = #{item.updateUserName},
                </if>
                <if test="item.updateUserId != null and item.updateUserId != ''">
                    UPDATE_USER_ID = #{item.updateUserId},
                </if>
                <if test="item.updateTime != null  ">
                    UPDATE_TIME = #{item.updateTime}
                </if>
            </set>
            where  id  = #{item.id}
        </foreach>
    </update>

    注意批量update需要设置数据库的连接加上这个:&allowMultiQueries=true  否则会报错

  • 相关阅读:
    一个案例
    Python Qt 简介
    【第二】用QtDesigner设计第一个界面
    【第一节】QtDesigner安装
    【第十二节】PyQt5俄罗斯方块
    【第十一节】PyQt5自定义控件
    【第十节】PyQt5绘图
    【第九节】PyQt 拖拽
    【第八节】PyQt5控件(II)
    【第七节】PyQt5控件
  • 原文地址:https://www.cnblogs.com/liglacier/p/14973390.html
Copyright © 2011-2022 走看看