ibatis 框架 批量添加的写法

insert_viann insert_viann 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在ibatis中写一个批量添加的sql,结构报 ORA-00911: invalid character 错误,请指点


我在sqlMap中的写法如下

	<!--批量增加对象-->
<insert id="batchInsert" parameterClass="java.util.Map">

<iterate property="templateId" conjunction=";">
insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER)
values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,#templateId[]#, #templateGroupId#,#createDate#, #createUser#)
</iterate>

</insert>



java测试类中调用的方法如下:
	@Test
public void batchInsertTemplateRelate() {
Map<String, Object> map=new HashMap<String, Object>();
List<Integer> list=new ArrayList<Integer>();
list.add(111);
list.add(222);
list.add(333);
list.add(444);
map.put("templateId", list);
map.put("createDate", new Date(new Date().getTime()));
map.put("templateGroupId", 555);
map.put("createUser", "ooo");



System.out.println(templateGroupDAO.batchInsertTemplateRelate(map));
}



ibatis 执行的sql代码如下:
2013-06-05 19:02:44,814 DEBUG java.sql.Connection - {conn-100000} Connection
2013-06-05 19:02:44,823 DEBUG java.sql.Connection - {conn-100000} Preparing Statement: insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?) ; insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?) ; insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?) ; insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?)
2013-06-05 19:02:44,915 DEBUG java.sql.PreparedStatement - {pstm-100001} Executing Statement: insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?) ; insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?) ; insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?) ; insert into SYSTEM_TEMPLATE_RELATE (ID,TEMPLATE_ID, TEMPLATE_GROUP_ID, CREATE_DATE, CREATE_USER) values (SEQ_SYSTEM_TEMPLATE_RELATE.nextval,?, ?,?, ?)
2013-06-05 19:02:44,915 DEBUG java.sql.PreparedStatement - {pstm-100001} Parameters: [111, 555, 2013-06-05 19:02:44.805, ooo, 222, 555, 2013-06-05 19:02:44.805, ooo, 333, 555, 2013-06-05 19:02:44.805, ooo, 444, 555, 2013-06-05 19:02:44.805, ooo]
2013-06-05 19:02:44,915 DEBUG java.sql.PreparedStatement - {pstm-100001} Types: [java.lang.Integer, java.lang.Integer, java.sql.Timestamp, java.lang.String, java.lang.Integer, java.lang.Integer, java.sql.Timestamp, java.lang.String, java.lang.Integer, java.lang.Integer, java.sql.Timestamp, java.lang.String, java.lang.Integer, java.lang.Integer, java.sql.Timestamp, java.lang.String]



下面是报错的信息:


org.springframework.dao.DataIntegrityViolationException: SqlMapClient operation; SQL [];   
--- The error occurred while applying a parameter map.
--- Check the SystemTemplateRelateDAO.batchInsert-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the SystemTemplateRelateDAO.batchInsert-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character

at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:100)
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.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:212)
at com.alibaba.cobar.client.CobarSqlMapClientTemplate.execute(CobarSqlMapClientTemplate.java:195)
at org.springframework.orm.ibatis.SqlMapClientTemplate.delete(SqlMapClientTemplate.java:436)
at com.alibaba.cobar.client.CobarSqlMapClientTemplate.delete(CobarSqlMapClientTemplate.java:265)
at com.uuzz.lop.sp.dal.impl.SystemTemplateGroupDAOImpl.batchInsertTemplateRelate(SystemTemplateGroupDAOImpl.java:142)
at test.sp.dal.SystemTemplateDAOTest.batchInsertTemplateRelate(SystemTemplateDAOTest.java:121)
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:601)
at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the SystemTemplateRelateDAO.batchInsert-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character

at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:107)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.update(SqlMapExecutorDelegate.java:457)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.delete(SqlMapExecutorDelegate.java:480)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.delete(SqlMapSessionImpl.java:98)
at org.springframework.orm.ibatis.SqlMapClientTemplate$11.doInSqlMapClient(SqlMapClientTemplate.java:438)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
... 28 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1010)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3677)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:437)
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:601)
at com.ibatis.common.jdbc.logging.PreparedStatementLogProxy.invoke(PreparedStatementLogProxy.java:62)
at com.sun.proxy.$Proxy11.execute(Unknown Source)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)
... 33 more
false
...全文

相关推荐

ibatis应对批量update

最近遇到需要批量update数据的问题,一开始用了一个for循环去update,数据量大的时候效率很低。原因是for循环每次update一条语句,都是一次连接过程。遇到大批数据更新的时候,效率就可想而知了。在google上找了一遍,发现ibatis里有对批量update的支持,挺好的东西。

FBIKings

掌握iBATIS框架中的<iterate>标签

iBATIS框架,作为早期流行的ORM(对象关系映射)解决方案之一,为Java开发者提供了一种简便的方式来操作数据库。它的核心是通过XML文件或注解来映射SQL语句和Java对象,从而简化了传统JDBC操作的复杂性。动态SQL是指在执行时,SQL语句可以根据实际需要进行拼装和修改的SQL。它与静态SQL相对,后者是在编译期就已经确定,不会在运行时改变。动态SQL能够根据不同的条件和参数生成不同的SQL语句,使得代码更加灵活,适用于复杂的查询和更新操作。

韦臻

ibatis3批量插入

通过Ibatis3xml文件配置+Mysql实现数据批量插入,原先在网上找很多,都是关于批量更新,批量插入的很少,而且其写法又完全不同,所以自己研究了一下。可供参考。另外插入时如出现内容报错,请参照my.cnf修改书库配置文件。

wangzj218

ibatis批量处理

ibatis批量处理操作实现,以及sping中需要注意点

极客小桑

mysql,jdbc详解,与ibatis对比。批量查询,分页处理。

mysq增删改查,jdbc 驱动加载 各个类和接口详解,与ibatis对比。批量查询,分页处理。

qq_22992429
评论 2
收藏 已收藏
分享
返回顶部
insert_viann
Java EE 67,535 成员 225,843 内容