; 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就死活执行不成功。

猜你喜欢

转载自blog.csdn.net/u010316188/article/details/84098926