• mybatis批量插入(mysql和oracle)


    Mybatis批量插入需要foreach元素。foreach元素有以下主要属性:

    (1)item:集合中每一个元素进行迭代时的别名。

    (2)index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置。

    (3)collection:根据传入的参数值确定。

    (4)open:表示该语句以什么开始。

    (5)separator:表示在每次进行迭代之间以什么符号作为分隔 符。

    (6)close:表示以什么结束。

    mybatis + mysql的写法:

     <insert id="insertBatch" parameterType="com.demo.User"
                useGeneratedKeys="true" keyProperty="id" keyColumn="id">
            INSERT INTO user
            (
            id,
            name,
            age
            )
            values
            <foreach collection="list" index="index" item="item" separator=",">
                (
                #{item.id},
                #{item.name},
                #{item.age}
                )
            </foreach>
        </insert>
    

    mybatis + oracle的写法:

    <insert id="save" parameterType="com.demo.User"
                useGeneratedKeys="true" keyProperty="id" keyColumn="id">
                INSERT INTO user
                (
                  id,
                  name,
                  age
                )
              <foreach collection="roleIdList" item="item" index="index" separator="UNION ALL" >
                SELECT
                    #{item.id},
                    #{item.name},
                    #{item.age}
                FROM dual
            </foreach>
    </insert>
    

    oracle的写成mysql的写法就会报错:SQL 命令未正确结束。

    mybatis+oracle需要注意三个地方:

    (1)需要取掉values

    (2)separator属性值改为UNION ALL。因为在oracle中用insert into xxx values (xxx,xxx),(xxx,xxx) 这种语法是通不过的

    (3)foreach标签中需要取掉括号,加入select ..from dual.

  • 相关阅读:
    时间日期date/cal
    chown命令
    su命令
    which命令和bin目录
    python基础之文件操作
    python之模块之shutil模块
    python基础之面向对象01
    python基础之面向对象02
    python基础之map/reduce/filter/sorted
    python基础之模块之序列化
  • 原文地址:https://www.cnblogs.com/snail-gao/p/14453036.html
Copyright © 2020-2023  润新知