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. VC提交网页表单(一共八篇)

    VC提交网页表单-自动评论留言(1)http://blog.csdn.net/wangningyu/article/details/4526357VC提交网页表单-自动评论留言(2)http://bl ...

  2. [cocos2d demo]认字小游戏

    2013.9.5更新第二版 游戏分三个场景,分别为主场景,加载场景以及游戏场景,游戏场景分为背景层,逻辑层以及UI层 1.背景:旋转太阳,移动波浪,漂浮的云 2.UI层:随机生成字附带在帆船上移动,当 ...

  3. c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制

    using mshtml;using System;using System.Collections.Generic;using System.Linq;using System.Security.P ...

  4. 无法自动调试 未能调试远程过程。这通常说明未在服务器上启用调试 WCF 托管在IIS上

    解决方案,把新建的网站的app.config修改下配置 <system.web> <!-- 设置 compilation debug="true" 可将调试符号插 ...

  5. 【C++ in Qt5】一个简单的通讯录程序,支持文件存取

    https://github.com/shalliestera/AddressBook 使用Qt5完成了一些之前用DevCpp很难做到的事,比如更简单地实现“从文件读取”和“保存到文件”的功能.之前我 ...

  6. IE浏览器下读取客户端上传的文件大小

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. (转载)查看三种MySQL字符集的方法

    (转载)http://database.51cto.com/art/201010/229171.htm MySQL字符集多种多样,下面为您列举了其中三种最常见的MySQL字符集查看方法,该方法供您参考 ...

  8. C# partial 局部类型

    关键字partial是一个上下文关键字,只有和 class.struct.interface 放在一起时才有关键字的含义.因此partial的引入不会影响现有代码中名称为partial的变量.局部类型 ...

  9. poj magic number

    Problem H Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  10. [转]NHibernate之旅(8):巧用组件之依赖对象

    本节内容 引入 方案1:直接添加 方案2:巧用组件 实例分析 结语 引入 通过前面7篇的学习,有点乏味了~~~这篇来学习一个技巧,大家一起想想如果我要在Customer类中实现一个Fullname属性 ...