oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误

### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL 命令未正确结束
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL 命令未正确结束
  • 批量插入方式一
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.attence.attence.dao.AttenceDao">
<resultMap id="Attence" type="Attence">
<id column="ATTENCEKEY" property="attenceKey"/>
<result column="EPC" property="epc"/>
<result column="ANT" property="ant"/>
<result column="RSSI" property="rssi"/>
<result column="DEVICE" property="device"/>
<result column="CRC" property="crc"/>
<result column="BCC" property="bcc"/>
<result column="DATETIME" property="dateTime" />
</resultMap> <select id="insertAttence" parameterType="List">
INSERT ALL
<foreach collection="list" item="attence" index="index" separator="">
into Attence(
attenceKey,
epc,
ant,
rssi,
device,
crc,
bcc,
dateTime
)
values
(
#{attence.id},
#{attence.epc},
#{attence.ant},
#{attence.rssi},
#{attence.device},
#{attence.crc},
#{attence.bcc},
#{attence.dateTime}
)
</foreach>
SELECT *
FROM dual
</select>
</mapper>

以上方式不支持oracle的主键序列方式。

  • 批量插入方式二
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.attence.attence.dao.AttenceDao">
<resultMap id="Attence" type="Attence">
<id column="ATTENCEKEY" property="attenceKey"/>
<result column="EPC" property="epc"/>
<result column="ANT" property="ant"/>
<result column="RSSI" property="rssi"/>
<result column="DEVICE" property="device"/>
<result column="CRC" property="crc"/>
<result column="BCC" property="bcc"/>
<result column="DATETIME" property="dateTime" />
</resultMap> <insert id="insertAttence" parameterType="java.util.List">
insert into Attence(
attenceKey,
epc,
ant,
rssi,
device,
crc,
bcc,
dateTime
)
Select
SEQ_ATTENCE.NEXTVAL,a.*
From (
  <foreach collection="list" item="attence" index="index" separator="union all">
(
Select
#{attence.epc},
                                    #{attence.ant},
                                    #{attence.rssi},
                                    #{attence.device},
                                    #{attence.crc},
                                    #{attence.bcc},
                                    #{attence.dateTime}
                                    From dual
                            ) </foreach>
)
</insert>
</mapper>

同时由于使用oracle的自增加序列,无法联合union all使用,会提示错误,必须加上select语句进行进一步封装。

1、传入的参数只有一个list时,则Mybatis映射collection的键名list,若传入含有其他参数,需要使用HashMap,同时键名为HashMap对应的键名。

2、传入的参数只有一个array时,则Mybatis映射collection的键名array,若传入含有其他参数,需要使用HashMap,同时键名为HashMap对应的键名。

  • 传递包含有数组参数的多参数HashMap实现批量插入
	<select id="insertGuideVideoList" parameterType="HashMap">
insert into Guide_Video(
id,
guideId,
videoId
)
Select
SEQ_Guide_Video.NEXTVAL ,#{guideId}, video.*
From(
<foreach collection="arrGuideVideo" item="videoId" index="index" separator="union all">
Select
#{videoId} videoId
From dual
</foreach>
)video
</select>

arrGuideVideo是一个字符串数组,这里必须使用select标签,而不能使用insert标签。如果使用insert标签执行会提示语句未结束。

  • 实现批量更新
	<update id="updateTest" parameterType="Test">
Begin
Update TST_TEST
<set>
<if test="test.testName != null and test.testName!= ''">
TESTNAME = #{test.testName},
</if> <if test="test.gatherKey != null and test.gatherKey!= ''">
GATHERKEY = #{test.gatherKey},
</if> <if test="test.subjectKey != null and test.subjectKey!= ''">
SUBJECTKEY = #{test.subjectKey},
</if> <if test="test.gradeKey != null and test.gradeKey!= ''">
GRADEKEY = #{test.gradeKey},
</if> <if test="test.answerCount != null and test.answerCount!= ''">
answerCount = #{test.answerCount},
</if> <if test="test.answerEndTime != null and test.answerEndTime!= ''">
answerEndTime = #{test.answerEndTime},
</if> <if test="test.answerTimeLength != null and test.answerTimeLength!= ''">
answerTimeLength = #{test.answerTimeLength},
</if>
</set>
Where testKey = #{testKey}
End;
</update>

实现批量更新实际上就是 生成oracle语句的代码块,然后jdbc执行sql语句。

Mybatis中实现oracle的批量插入、更新的更多相关文章

  1. mybatis中sql语句的批量插入

    <!-- 收件箱插入收件信息 -->    <insert id="insertReceiveemail">           <!-- 生成一条U ...

  2. mybatis oracle mysql 批量插入时的坑爹问题--需谨记

    mybatis oracle mysql 批量插入一.oracle的批量插入方式insert into db(id, zgbh, shbzh) select '1', '2', '3' from du ...

  3. Oracle 存储过程批量插入数据

    oracle 存储过程批量插入大量数据 declare numCount number; userName varchar2(512); email varchar2(512); markCommen ...

  4. mybatis+oracle的批量插入

    // 批量插入,手动控制事务 SqlSession batchSqlSession = null; try { batchSqlSession = sqlSessionTemplate.getSqlS ...

  5. oracle+ibatis 批量插入-支持序列自增

    首先请先看我前面一篇帖子了解oracle批量插入的sql:[oracle 批量插入-支持序列自增] 我用的ibatis2.0,sqlMap文件引入的标签如下: <!DOCTYPE sqlMap ...

  6. Mybatis通过注解方式实现批量插入数据库 及 常见的坑

    原文地址:http://f0rb.iteye.com/blog/1207384 MyBatis中通过xml文件配置数据库批量操作的文章很多,比如这篇http://www.cnblogs.com/xcc ...

  7. C#中使用SqlBulkCopy的批量插入和OracleBulkCopy的批量插入

    1.首先我们做一下准备工作,在sql server和oracle分别建立一个Student表 oracle中 --创建Student表 -- create table Student( stuId n ...

  8. Mybatis通过注解方式实现批量插入数据库

    原文地址:http://f0rb.iteye.com/blog/1207384 MyBatis中通过xml文件配置数据库批量操作的文章很多,比如这篇http://www.cnblogs.com/xcc ...

  9. Java批量插入更新操作

    以前总是说批量插入和更新的效率比非批量的要高,但是一直没有使用过批量处理数据的功能,现在由于项目中需要处理的数据量比较大,所以使用了批量处理的功能,java代码如下: 1.java实现批量插入数据: ...

随机推荐

  1. 解决CAS单点登录出现PKIX path building failed的问题

    在一次调试中,出现了这个错误: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExceptio ...

  2. 李洪强iOS开发之-Swift_00_介绍

    SWIFT (计算机编程语言) Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序. ...

  3. BOM的来源是不可能出现的字符,GB2312双字节高位都是1,Unicode理论的根本缺陷导致UTF8的诞生

    Unicode字符编码规范   http://www.aoxiang.org 2006-4-2 10:48:02Unicode是一种字符编码规范 . 先从ASCII说起.ASCII是用来表示英文字符的 ...

  4. 关于v$sql_bind_capture 的问题

    ---先清空shared_pool SQL> alter system flush shared_pool; System altered. SQL> col value_STRING f ...

  5. 转:十九、Java的接口及实例

    http://blog.csdn.net/liujun13579/article/details/7736116 一.定义 Java接口(Interface),是一系列方法的声明,是一些方法特征的集合 ...

  6. 【CF】196 Div.2 D. Book of Evil

    显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...

  7. C#连接数据库的一些鲜为人知的方法

    用过VS2008和VS2010的开发人员肯定知道在安装这个IDE的时候会自动安装了一个精简版的SQL数据库服务SqlExpress,这个数据库系统少了最重要的企业管理器,也就是说不能用它来建数据表和一 ...

  8. android报错——findViewById报错

    通過ID找到Layout的 VIEW控件.,比如你的控件Button ID為"@+id/button01"   就可以通過這樣Button btn=(Button)findView ...

  9. 数学之美 zt

    数学是美丽的,哪里有数哪里就有美. 数学的定义是研究数量关系和空间形式的一门科学.但有句名言说:数学比科学大得多,因为它是科学的语言.数学不仅用来写科学,而且可用来写人生.所以说数学是一切学科的基础, ...

  10. win8 mysqlzip install

    1. 下载MySQL Community Server 5.6.142. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下.3. 添加环境变量 变量名:MYSQL_HOME 变量值: ...