Mybatis中实现oracle的批量插入、更新
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的批量插入、更新的更多相关文章
- mybatis中sql语句的批量插入
<!-- 收件箱插入收件信息 --> <insert id="insertReceiveemail"> <!-- 生成一条U ...
- mybatis oracle mysql 批量插入时的坑爹问题--需谨记
mybatis oracle mysql 批量插入一.oracle的批量插入方式insert into db(id, zgbh, shbzh) select '1', '2', '3' from du ...
- Oracle 存储过程批量插入数据
oracle 存储过程批量插入大量数据 declare numCount number; userName varchar2(512); email varchar2(512); markCommen ...
- mybatis+oracle的批量插入
// 批量插入,手动控制事务 SqlSession batchSqlSession = null; try { batchSqlSession = sqlSessionTemplate.getSqlS ...
- oracle+ibatis 批量插入-支持序列自增
首先请先看我前面一篇帖子了解oracle批量插入的sql:[oracle 批量插入-支持序列自增] 我用的ibatis2.0,sqlMap文件引入的标签如下: <!DOCTYPE sqlMap ...
- Mybatis通过注解方式实现批量插入数据库 及 常见的坑
原文地址:http://f0rb.iteye.com/blog/1207384 MyBatis中通过xml文件配置数据库批量操作的文章很多,比如这篇http://www.cnblogs.com/xcc ...
- C#中使用SqlBulkCopy的批量插入和OracleBulkCopy的批量插入
1.首先我们做一下准备工作,在sql server和oracle分别建立一个Student表 oracle中 --创建Student表 -- create table Student( stuId n ...
- Mybatis通过注解方式实现批量插入数据库
原文地址:http://f0rb.iteye.com/blog/1207384 MyBatis中通过xml文件配置数据库批量操作的文章很多,比如这篇http://www.cnblogs.com/xcc ...
- Java批量插入更新操作
以前总是说批量插入和更新的效率比非批量的要高,但是一直没有使用过批量处理数据的功能,现在由于项目中需要处理的数据量比较大,所以使用了批量处理的功能,java代码如下: 1.java实现批量插入数据: ...
随机推荐
- layer.js:2 Uncaught TypeError: Cannot read property 'extend' of undefined
在引用layer.js插件进行前端编程的时候,如果报这个错,解决办法只需: 把layer的引用放在有冲突的js库前面就行了
- FFmpeg发送流媒体的命令(UDP,RTP,RTMP)
http://blog.csdn.net/leixiaohua1020/article/details/38283297
- Qt源码分析之QPointer
QPointer是一个指针封装类,其作用类似于智能指针,但是它最大的特点应该是在指针的控制上,它希望一个Qt的指针(当然是从QObject派生的)可以同时被多个类拥有,这在界面编程中当然是很常见的事情 ...
- 【踩坑记】从HybridApp到ReactNative
前言 随着移动互联网的兴起,Webapp开始大行其道.大概在15年下半年的时候我接触到了HybridApp.因为当时还没毕业嘛,所以并不清楚自己未来的方向,所以就投入了HybridApp的怀抱. Hy ...
- IPVS实现分析
IPVS实现分析 IPVS实现分析 根据LVS官方网站的介绍,LVS支持三种负载均衡模式:NAT,tunnel和direct routing(DR). NAT是通用模式,所有交互数据必须通过均衡器:后 ...
- WordPress Lazy SEO插件lazyseo.php脚本任意文件上传漏洞
漏洞名称: WordPress Lazy SEO插件lazyseo.php脚本任意文件上传漏洞 CNNVD编号: CNNVD-201309-446 发布时间: 2013-09-26 更新时间: 201 ...
- 我泡在GitHub上的177天 by Ryan Seys
我泡在GitHub上的177天 这是一个关于我如何连续177天(将近半年)泡在GitHub上不间断地贡献代码的故事.我会谈到我为什么要这么做,以及为什么你也应该效仿,或者至少做点类似的事情.这是一 ...
- centos 6.4 大容量磁盘分区步骤
首先安装分区工具 yum install parted 假设需要分区的设备为/dev/sdb 运行如下命令 parted /dev/sdb (parted) mklabel gpt #大于2TB的磁盘 ...
- [C# 网络编程系列]专题九:实现类似QQ的即时通信程序
转自:http://www.cnblogs.com/zhili/archive/2012/09/23/2666987.html 引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例 ...
- 你认为A和B所在方格颜色相同吗?
[你认为A和B所在方格颜色相同吗? ]据说全世界只有0.003%的人和photoshop能看出它们的颜色是相同的. 我属于那 99.9997% 的人...因为我不是神...