Mybatis Query插入java对象列表失败

时间:2015-04-09 16:24:51

标签: java oracle mybatis

我尝试使用Mybatis插入对象列表并获取以下错误。

我做了一些关于插入列表的研究,很少有例子用于插入所有查询,我认为这不是正确的方法。

我为每个人使用“插入”并且无法使其工作。如果有人可以指出它为什么不起作用以及什么是正确的方法,我将非常感激.Below是我的代码和堆栈跟踪:

1)域对象: -

public class TestName {
  String first;
  String last;

  public String getFirst() {
    return first;
  }

  public void setFirst(String first) {
    this.first = first;
  }

  public String getLast() {
    return last;
  }

  public void setLast(String last) {
    this.last = last;
  }
}

2)调用myBatis的方法: -

 public void insertSplyDmndList(List<SplyDmndDo> list) throws DppException {
    String methodName = "insertSplyDmndList";
    LogHelper.begin(logger, methodName);

    try {
      if (list.size() > 0) {

        List<TestName> testNameList = new ArrayList<TestName>();
        for (int i = 0; i < list.size(); i++) {
          TestName testName = new TestName();
          testName.setFirst("First");
          testName.setLast("Last");
          testNameList.add(testName);
        }

        System.out.println("SIZE Test Name" + testNameList.size());

        session.insert("com.pepsico.dpp.webservices.rest.drpload.DrpLoadMapper.insertName",
            testNameList);


      }
    } catch (Exception e) {
      throw new DppException(e);
    }
    LogHelper.end(logger, methodName);
  }

3)Mybatis映射器: -

<insert id="insertName" parameterType="java.util.List">
  <foreach item="name" collection="list"  separator=",">
  INSERT INTO NAME (LAST, First) VALUES ( #{name.last}, #{name.first} )</foreach> 
  </insert>

4)Pom.xml的依赖: -

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

5)堆栈追踪: -

### The error may involve com.pepsico.dpp.webservices.rest.drpload.DrpLoadMapper.insertName-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? ) ,        INSERT INTO NAME (LAST, First) VALUES ( ?, ? )         
### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
    ... 43 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

4 个答案:

答案 0 :(得分:1)

对于Oracle,INSERT ALL语法是使用单个语句插入多行的正确方法,即使它看起来与其他RDBMS中的多行插入不同。 要使用MyBatis构造INSERT ALL语句,请尝试以下操作:

<insert id="insertName" parameterType="java.util.List">
INSERT ALL
  <foreach item="name" collection="list"  separator=" ">
    INTO NAME (LAST, First) VALUES ( #{name.last}, #{name.first} )
  </foreach> 
SELECT * FROM DUAL;
</insert>

如果您运行的是旧版本的Oracle,则替代方案是使用this answer中的INSERT INTO'd“UNION语句SELECT语法,你可以试试这个:

<insert id="insertName" parameterType="java.util.List">
INSERT INTO NAME (LAST, First)
  <foreach item="name" collection="list"  separator=" UNION ALL ">
    SELECT #{name.last}, #{name.first} FROM DUAL
  </foreach> 
</insert>

正如@pf_miles指出的那样,如果您选择从Java循环遍历名称列表,请务必在打开MyBatis会话时使用ExecutorType.BATCH

[免责声明:我没有尝试过任何上述代码段,因此请将它们视为优化的起点]

答案 1 :(得分:0)

请注意:

  

SQL:INSERT INTO NAME(最后,第一个)VALUES(?,?),INSERT INTO NAME(最后,第一个)VALUES(?,?),INSERT INTO NAME(最后,第一个)VALUES(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后) ,首先)值(?,?),插入名称(最后,第一)值(?,?),插入名称(最后,第一)值(?,?),插入名称(最后,第一)值( ?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后) ,首先)VALUES(?,?),插入名称(最后,第一)值( ?,? ),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入名称(最后,第一个)值(?,?),插入.. ....

绝对是一个非法的sql语句。

看起来您想要将列表中的每个元素插入表'NAME'的单独行中 您可以编写一个insert语句,其中包含一个list元素作为参数,如下所示:

<insert id="insertName" parameterType="your list element type">
    INSERT INTO NAME (LAST, First) VALUES ( #{last}, #{first} )
</insert>

并循环遍历java代码中的列表,在批处理操作中执行所有插入操作。

答案 2 :(得分:0)

一个提示

在Mybatis中,当 ORA 00933 ERROR 出现时,有几个原因。

其中一个是由于分号(;)相关。

您需要从查询结尾删除分号。

答案 3 :(得分:0)

以格式插入多个值更有效:

INSERT INTO NAME (LAST , FIRST)
VALUES (
    (L1 , F1),
    (L2 , F2),
    (L3 , F3),
    ..
)

不确定此XML代码的确切格式,但应该是这样的:

<insert id="insertName" parameterType="java.util.List">
    INSERT INTO NAME (LAST, First) VALUES (
    <foreach item="name" collection="list"  separator=",">
        (#{name.last} , #{name.first})
    </foreach>
    )
</insert>