mybatis批量插入数据到oracle

mybatis 批量插入数据到oracle报 ”java.sql.SQLException: ORA-00933: SQL 命令未正确结束“  错误解决方法

oracle批量插入使用 insert all into table(...) values(...) into table(...) values(...) select * from dual; 语句来解决,但一直报如下错误

### The error may involve ApplaudDaoImpl.addList-Inline
### The error occurred while setting parameters
### SQL: INSERT ALL        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )        INTO T_APPLAUD   (    ID,    USER_ID,    BUSINESS_TYPE,    PRODUCT_ID,    CREATE_TIME   ) VALUES   (    ?,    ?,    ?,    ?,    ?    )       SELECT 1 FROM DUAL
### Cause: java.sql.SQLException: ORA-00933: SQL 命令未正确结束

; bad SQL grammar []; nested exception is java.sql.SQLException: ORA-00933: SQL 命令未正确结束

    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:97)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:368)
    at com.sun.proxy.$Proxy12.insert(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:240)
    at com.teshehui.applaud.dao.impl.ApplaudDaoImpl.addList(ApplaudDaoImpl.java:33)
    ... 52 more
Caused by: java.sql.SQLException: ORA-00933: SQL 命令未正确结束

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:966)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1170)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3339)
    at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3445)
    at com.jolbox.bonecp.PreparedStatementHandle.execute(PreparedStatementHandle.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:55)
    at com.sun.proxy.$Proxy17.execute(Unknown Source)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:59)
    at com.sun.proxy.$Proxy15.update(Unknown Source)
    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:100)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:148)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
    ... 55 more

谷歌无数次,自己跟踪调试也没结果,最后在stackoverflow mybatis问答区偶然看到了网友分析(链接见末尾),测试发现解决了,原因竟是mybatis批量插入oracle时需要显式指定为 useGeneratedKeys="false" 不然报错~~~

正确的mybatis的mapper的sql配置如下:

 1 <insert id="addList" parameterType="java.util.List" useGeneratedKeys="false">
 2         INSERT ALL
 3         <foreach item="item" index="index" collection="list">
 4         INTO T_APPLAUD
 5         (
 6             ID,
 7             USER_ID,
 8             BUSINESS_TYPE,
 9             PRODUCT_ID,
10             CREATE_TIME
11         ) VALUES
12         (
13             #{item.id, jdbcType=NUMERIC},
14             #{item.userId, jdbcType=VARCHAR},
15             #{item.businessType, jdbcType=VARCHAR},
16             #{item.productId, jdbcType=VARCHAR},
17             #{item.createdTime, jdbcType=NUMERIC}
18         )
19         </foreach>
20         SELECT 1 FROM DUAL
21     </insert>

另外一种方法是 insert into table(...) (select ... from dual) union all (select ... from dual)

 1     <insert id="addList" parameterType="java.util.List" useGeneratedKeys="false">
 2         INSERT INTO T_APPLAUD
 3         (
 4             ID,
 5             USER_ID,
 6             BUSINESS_TYPE,
 7             PRODUCT_ID,
 8             CREATE_TIME
 9         )
10         <foreach item="item" index="index" collection="list" separator="union all">
11         (
12             SELECT
13                 #{item.id},
14                 #{item.userId},
15                 #{item.businessType},
16                 #{item.productId},
17                 #{item.createdTime}
18             FROM DUAL
19         )
20         </foreach>
21     </insert>

参考:

http://stackoverflow.com/questions/24956269/camel-2-13-1-mybatis-3-2-7-batch-insert-to-oracle-11g-table-ora-00933-sql-comma

时间: 2024-10-14 05:23:58

mybatis批量插入数据到oracle的相关文章

mybatis foreach批量插入数据:Oracle与MySQL区别

mybatis foreach批量插入数据:Oracle与MySQL不同点: 主要不同点在于foreach标签内separator属性的设置问题: separator设置为","分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) ,(v4,v5,v6) ,... separator设置为"union all"分割时,最终拼接的代码形式为:insert into table_name (a,b

Mybatis 批量插入数据

--mybatis 批量插入数据 --1.Oracle(需要测试下是否支持MySQL) < insert id ="insertBatch" parameterType="List" > insert into REAL_DATA_HW( M_LINE_NO,M_TIME,HW_NUM, VOL_A,VOL_B,VOL_C ) < foreach collection ="list" item ="item"

Java通过Mybatis实现批量插入数据到Oracle中

最近项目中遇到一个问题:导入数据到后台并将数据插入到数据库中,导入的数据量有上万条数据,考虑采用批量插入数据的方式: 结合网上资料,写了个小demo,文章末尾附上demo下载地址 1.新建项目:项目目录结构如下图所示,添加相应的jar包 2.新建数据库表:ACCOUNT_INFO 1 CREATE TABLE ACCOUNT_INFO ( 2 "ID" NUMBER(12) NOT NULL , 3 "USERNAME" VARCHAR2(64 BYTE) NULL

MyBatis批量插入数据(MySql)

由于项目需要生成多条数据,并保存到数据库当中,在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,项目使用了Spring+MyBatis,所以打算使用MyBatis批量插入,应该要比循环插入的效果更好,由于之前没用过批量插入,在网上找了一些资料后最终实现了,把详细过程贴出来.供以后查阅和学习. java代码: 注意:这里循环的时候需new 出来新的对象,而不能通过循环改变属性的值就认为这是一个新的对象了,通俗的说就是new ReddemCode()要放在for循环的里面.

MyBatis批量插入数据

在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,由于项目使用了Spring+MyBatis的配置,所以打算使用MyBatis批量插入,由于之前没用过批量插入,在网上找了一些资料后最终实现了,把详细过程贴出来. 实体类TrainRecord结构如下: public class TrainRecord implements Serializable { private static final long serialVersionUID = -12069604621179

160421、MyBatis批量插入数据

在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,由于项目使用了Spring+MyBatis的配置,所以打算使用MyBatis批量插入,由于之前没用过批量插入,在网上找了一些资料后最终实现了,把详细过程贴出来. 实体类TrainRecord结构如下: Java代码   public class TrainRecord implements Serializable { private static final long serialVersionUID = -12069

Java 批量插入数据(Oracle)

//批量添加20000条数据用时8秒. try { String url = "jdbc:oracle:thin:@IP:1521:orcl"; // orcl为数据库的SID String user = "oracle"; String password = "oracle"; StringBuffer sql = new StringBuffer(); sql.append("insert into ex_log (EX_LOG_I

1. mybatis批量插入数据

通过list <insert id="saveByList" useGeneratedKeys="true" parameterType="java.util.List"> insert into T_App_Default_User(UserID,AppType,CreateTime) values <foreach collection="list" item="item" index

批量插入数据(基于Mybatis的实现-Oracle)

前言:做一个数据同步项目,要求:同步数据不丢失的情况下,提高插入性能. 项目DB框架:Mybatis.DataBase:Oracle. ---------------------------------------------------------------------------- 批量插入数据方式: 一.Mybatis 全局设置批处理: 二.Mybatis 局部设置批处理: 三.Mybatis foreach批量插入: ①SELECT UNION ALL: ②BEGIN INSERT I