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中每一个对象的名称

  • 相关阅读:
    使用Redis实现分布式锁
    SpringBoot 定时任务的使用
    HTTP请求调试软件 Postman
    ElasticSearch的安装
    全文搜索 简介
    SpringBoot整合Redis
    Git 操作远程仓库(Github)
    Git的使用
    Git 简介、下载安装、配置
    Vue 商城的一些小demo(后台添加商品、前台购物车、本地存储的使用)
  • 原文地址:https://www.cnblogs.com/yxth/p/6673802.html
Copyright © 2011-2022 走看看