【问题标题】:Multiple Inserts Oracle Mybatis Spring using Insert All多个插入 Oracle Mybatis Spring 使用 Insert All
【发布时间】:2014-11-11 10:31:47
【问题描述】:

我正在尝试使用 Mybatis foreach 功能将对象列表插入到 Oracle 数据库中的单个表中。

例如,我有一个表“Test_A”,它有一个可为空的 VARCHAR 列“描述”。

我将一个字符串列表传递给 mybatis 方法,其映射器如下所述。

<insert id = "testMultipleInserts"  parameterType="java.util.List">
    Insert All 
    <foreach collection="list" item="element" index="index">
        Into Test_A (Description) values (#{element.description})
    </foreach>
    select * from dual
</insert>

当我调用上述方法时,它会将以下查询打印到日志中

      Insert All Into Test_A (Description) values (?) Into Test_A (Description) values (?) Into Test_A (Description) values (?) select * from dual 

我希望按照 Oracle 方言使用正确的语法(我从 SQL 浏览器执行,这有效)

但是从提供给方法的 List 参数中读取列表元素似乎存在一些问题。我得到了一个例外,如下所示。

      org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '__frch_element_0' not found. Available parameters are [list]

【问题讨论】:

  • 您确定没有任何不为空的 element.description 吗?而且,为什么要“从双重中选择 *”?
  • 我将列表中的所有非空值传递给 element.description。关于 select * from dual,我真的不知道为什么我们必须提供这个,但是在某处读到这必须遵循 Oracle 中的 Insert all 语句。它可以是 select * from dual 或任何 select query from dual。

标签: oracle mybatis


【解决方案1】:

我不知道您正在尝试的查询,但堆栈跟踪清楚地表明其参数问题将 parameterType 从 java.util.List 更改为 list

<insert id = "testMultipleInserts"  parameterType="list">
    Insert All 
    <foreach collection="list" item="element" index="index">
        Into Test_A (Description) values (#{element.description})
    </foreach>
    select * from dual
</insert>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2019-04-22
    相关资源
    最近更新 更多