zoukankan      html  css  js  c++  java
  • 解决Oracle+Mybatis批量插入报错:SQL 命令未正确结束

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

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

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

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

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

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

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

    首先,错误的xml配置文件如下:

    <insert id="save" databaseId="oracle">
    insert into "sys_user_role"
    (
    "user_id",
    "role_id"
    )values
    <foreach collection="roleIdList" item="item" index="index" separator="," >
    (
    #{userId},
    #{item}
    )
    </foreach>
    </insert>

    如果如上这样写就会报错:SQL 命令未正确结束。

    经过修改后正确的xml配置文件如下:

    <insert id="save" databaseId="oracle">
    insert into "sys_user_role"
    (
    "user_id",
    "role_id"
    )
    <foreach collection="roleIdList" item="item" index="index" separator="UNION ALL" >
    SELECT
    #{userId},
    #{item}
    FROM dual
    </foreach>
    </insert>

    根据上下配置文件,需要注意三个地方:

    (1)需要取掉values

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

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

  • 相关阅读:
    Kafka 生产者 自定义分区策略
    同步互斥
    poj 1562 Oil Deposits(dfs)
    poj 2386 Lake Counting(dfs)
    poj 1915 KnightMoves(bfs)
    poj 1664 放苹果(dfs)
    poj 1543 Perfect Cubes (暴搜)
    poj 1166 The Clocks (暴搜)
    poj 3126 Prime Path(bfs)
    处理机调度
  • 原文地址:https://www.cnblogs.com/ninicwang/p/7470745.html
Copyright © 2011-2022 走看看