国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Mybatis中實現oracle的批量插入、更新

2019-11-08 20:55:39
字體:
來源:轉載
供稿:網友
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>

 

<select id="insertWorksheet" parameterType="java.util.List">        insert into TDB_WORKSHEET(WORKSHEET_ID,SIM_WORKSHEET_ID)  (        <foreach item="item" index="index" collection="list"  separator="union all">        (select          #{item.worksheetId,jdbcType=VARCHAR},#{item.simWorksheetId,jdbcType=VARCHAR}          from dual)        </foreach>        )    </select>

      以上方式不支持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語句。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江都市| 伊宁市| 成都市| 四平市| 锦屏县| 含山县| 南木林县| 额敏县| 东乡| 高平市| 南平市| 曲周县| 福州市| 南雄市| 丰县| 太谷县| 南充市| 秦皇岛市| 阜平县| 永州市| 宜兰县| 诸暨市| 汉阴县| 盐边县| 龙门县| 扬州市| 南投县| 灯塔市| 海阳市| 澜沧| 康马县| 师宗县| 镇安县| 阿克苏市| 广昌县| 平泉县| 郎溪县| 长乐市| 山阴县| 得荣县| 鹰潭市|