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. 【Python Network】使用DOM生成XML

    单纯的为DOM树添加结点. #!/usr/bin/env python # Generating XML with DOM - Chapter 8 - domgensample.py from xml ...

  2. .net 与 javascript脚本的几种交互方法

    1.asp.net呼叫js Response.Write("<script language=javascript>"); Response.Write("a ...

  3. 使用jQuery实现简单的拖动效果

    转自:http://www.muzilei.com/archives/136 如何实现拖动效果? 浏览DEMO 首先分析下拖动效果原理: 1.当鼠标在被拖动对象上按下鼠标(触发onmousedown事 ...

  4. NHibernate加载DLL错误

    这几天在开发关于Rest的服务,其中用到了NHibernate来进行数据库交互,突然有一天发现了一个错误,如下: Could not load file or assembly 'NHibernate ...

  5. 洛谷1440 求m区间内的最小值

    洛谷1440 求m区间内的最小值 本题地址:http://www.luogu.org/problem/show?pid=1440 题目描述 一个含有n项的数列(n<=2000000),求出每一项 ...

  6. uva 10047 the monocyle (四维bfs)

    算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...

  7. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

  8. window7电脑设置好了,却无法远程?

    设置远程连接: 步骤:右键[我的电脑]-->[属性] 点击[远程设置],然后设置如下: 在 cmd 中 通过 [ipconfig]命令查看IP: 以上设置好了,发现仍无法远程?解决办法如下: 电 ...

  9. 获得URl信息

      public class GetUrlInfo {     public static void printfInfo(URL url)throws Exception     {         ...

  10. 1 weekend110的复习 + hadoop中的序列化机制 + 流量求和mr程序开发

    以上是,weekend110的yarn的job提交流程源码分析的复习总结 下面呢,来讲weekend110的hadoop中的序列化机制 1363157985066      13726230503  ...