zoukankan      html  css  js  c++  java
  • Mybatis批量插入

    批量插入在oracle和mysql中配置几乎一致,但是sql语句不同。

    oracle配置如下

        <insert id="setRoles" parameterType="java.util.List">
            insert into role  
            <foreach collection="list" item="role" index="index" separator="union all">
                select #{role.roleID},#{role.moduleID},#{role.roleName},#{role.grade},#{role.roleID} from dual
            </foreach>
        </insert>

    mysql配置如下

        <insert id="insertByBatch" parameterType="java.util.List">
            insert into attachment_table (name, logID,url)
            values
            <foreach collection="list" item="item" index="index" separator=",">
                (#{item.name,jdbcType=VARCHAR}, #{item.logid,jdbcType=INTEGER},#{item.url,jdbcType=LONGVARCHAR})
            </foreach>
        </insert>

    注意其中

    1、oracle的插入语句里没有values

    2、oracle的foreach循环中使用的是select **** from dual

    这两种不同的方式,输入的日志格式也不同。

    oracle :

     insert into role select ?,?,?,?,? from dual union all select ?,?,?,?,? from dual union all select ?,?,?,?,? from dual 

    mysql :

     insert into role values(?,?,?,?,?),(?,?,?,?,?)

    由于oracle中没有mysql的这种插入方式,才导致两种不同数据库需要两种不同的批量插入方式。

    具体的配置当中,foreach是一个循环标签,由于我们的传入参数是list,所以collection的值为list;item代表list中每一个对象的名称

  • 相关阅读:
    [HDU 3038] How Many Answers Are Wrong
    [BZOJ 4977][Lydsy1708月赛]跳伞求生
    [BZOJ4974] 字符串大师
    总结-exCRT
    [luogu 4777] exCRT
    [AHOI 2009] 中国象棋
    JavaScript MVC框架PK:Angular、Backbone、CanJS与Ember
    十一黄金周 加班加点随笔
    从两个设计模式到前端MVC-洪宇
    Todo&Rocket
  • 原文地址:https://www.cnblogs.com/yxth/p/6673802.html
Copyright © 2011-2022 走看看