论坛首页 Java企业应用论坛

MyBatis 批量操作 ArrayIndexOutOfBoundsException

浏览 7339 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2016-04-19  

问题描述:

mybatis 在进行批量插入数据的时候报如下异常:

org.apache.ibatis.exceptions.PersistenceException: ### Error flushing statements. Cause: java.lang.ArrayIndexOutOfBoundsException ### Cause: java.lang.ArrayIndexOutOfBoundsException at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26) at org.apache.ibatis.session.defaults.DefaultSqlSession.flushStatements(DefaultSqlSession.java:202) at com.chinaums.uis.web.dao.BaseDao.batchInsert(BaseDao.java:90) at com.chinaums.uis.web.service.refund.RefundToBankTest.test3(RefundToBankTest.java:93) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

同样的代码运行一直没有问题,经过排查是表的主键生成方式不一样导致的异常,之前的主键是这样生成的:

  <insert id="insert" parameterType="com.test.User"    useGeneratedKeys="true" >
  <selectKey keyProperty="id" order="BEFORE" resultType="java.math.BigDecimal">
            SELECT USER.NEXTVAL FROM DUAL
    </selectKey>

产生异常的,批量操作的表的主键 是在外面生成的,所以导致报错

问题的解决:

<insert id="insert" parameterType="com.xxx.xxx" useGeneratedKeys="false" >

 只要加入useGeneratedKeys="false" 就可以了。

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics