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实现批量插入数据: ...
随机推荐
- kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
今天在写kafka生产者生成数据的程序并运行时,报如下错误: log4j:WARN No appenders could be found for logger (kafka.utils.Verifi ...
- [转贴] C++ 判断主机是否处于联网状态下
直接让本机访问一个网站,如果成功的话,就说明成功联网,没有访问成功,则说明没有联网!!! #include<iostream> #include <WINSOCK2.H> #p ...
- (转载)JS事件监听 JS:attachEvent和addEventListener使用方法
(转载)http://www.chhua.com/web-note146 attachEvent和addEventListener使用方法 Js代码 <html> <head> ...
- Devexpress 之gridControl
1.gridControl如何去掉主面板? 鼠标右键Run Designer=>OptionsView => ShowGroupPanel=False: 2.gridControl如何设置 ...
- Linux学习笔记32——select()函数分析【转】
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程序 ...
- JavaScript高级程序设计24.pdf
Element类型 Element类型用于表现XML或HTML元素,提供对元素标签名.子节点及特性的访问,它具有以下特征 nodeType的值为1: nodeName的值为元素的标签名: nodeVa ...
- [ZETCODE]wxWidgets教程七:对话框
本教程原文链接:http://zetcode.com/gui/wxwidgets/dialogs/ 翻译:瓶哥 日期:2013年12月9日星期一 邮箱:414236069@qq.com 主页:http ...
- [LeetCode] 86. Partition List 解题思路
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- Java--创建线程及常用方法
继承java.lang.Thread类--Thread类代表线程类 它的常用方法如下: static Thread currentThread():返回当前正在运行的线程对象的引用. static ...
- poj 2503 字符串hash
题目链接:http://poj.org/problem?id=2503 代码: #include<cstdio> #include<cstring> #include<i ...