1. 程式人生 > >; uncategorized SQLException; SQL state [99999]; error code [17090]; 不允許的操作; nested exception is jav

; uncategorized SQLException; SQL state [99999]; error code [17090]; 不允許的操作; nested exception is jav

今天在做mybatis批量插入oracle的時候報了這個錯誤; uncategorized SQLException; SQL state [99999]; error code [17090]; 不允許的操作; nested exception is java.sql.SQLException: 不允許的操作(String),搞了一晚上頭大,記錄下解決方法。

專案裡面用的mybatis版本是mybatis-3.4.6
不同的版本寫法還不一樣,我們原來的專案也是這樣寫沒啥問題。

之前的寫法

	<insert id="batchUserRole" >
		BEGIN
		<foreach item="item" index="index" collection="list" separator=";">
			insert into sys_user_role(user_id, role_id) values	(#{item.userId},#{item.roleId})
		</foreach>
		;END ;
	</insert>

改過之後的寫法

	<insert id="batchUserRole" useGeneratedKeys="false">
		BEGIN
		<foreach item="item" index="index" collection="list" separator=";">
			insert into sys_user_role(user_id, role_id) values	(#{item.userId},#{item.roleId})
		</foreach>
		;END ;
	</insert>

這裡我沒有新增parameterType="java.util.List"

也是可以的,預設還是新增一下比較好。

完整的寫法

	<insert id="batchUserRole" useGeneratedKeys="false" parameterType="java.util.List">
		BEGIN
		<foreach item="item" index="index" collection="list" separator=";">
			insert into sys_user_role(user_id, role_id) values	(#{item.userId},#{item.roleId})
		</foreach>
		;END ;
	</insert>

為什麼需要新增 useGeneratedKeys="false"呢,是因為mybatis在批量插入的時候如果沒有指定useGeneratedKeys="false"為false,會自動認為主鍵是自增型別,在mysql裡面貌似沒問題,但是在oracle就死活執行不成功。