问题:在做批量新增时其中有一个字段需要将对象集合转换成json字符串进行存储,数据库使用的时Oracle,这个字段使用的是CLOB类型,之前没有发现有什么问题,后来业务在调用这个地方的功能时有其中一条数据在执行批量插入的时候报错了

起初是在找这里的类型是否和数据库中字段类型匹配,正常情况数据库字段为CLOB类型在封装实体时使用String是可以保存或是读取成功的,因为之前也没有问题。后来百度查询说是CLOB的上限是4GB,默认为4000字节,存储时需要将字符串转换为字节存储,试了一下,不行。
中午睡了一觉继续搜,发现了问题所在,在一篇来自CSDN @Orient_大佬的文章中找出了原因。
在批量插入的时候使用了虚表,虚表中的字段类型为long类型,LONG 数据类型中存储的是可变长字符串,最大长度限制是2GB,超过最大限制时这里报的错就是上面的这个can bind a LONG value only for insert into a LONG column的报错。

所以这里给出的解决方案就是不使用虚表,通过单条插入是没有问题的。
单条插入的写法
<insert id="insertUser" parameterType="com.orient.user">
INSERT INTO table_user (
id, user_code, user_name
) values(
#{item.id,jdbcType=VARCHAR}, #{item.userCode,jdbcType=VARCHAR},
#{item.userName,jdbcType=VARCHAR}
)
</insert>由上可得知可以使用 begin 和 end 标签围绕插入语句,以实现批量操作
<insert id="insertUserBatch" parameterType="java.util.List">
begin
<foreach collection="userList" item="item" index="index" separator=";">
INSERT INTO table_user (
id, user_code, user_name
) values(
#{item.id,jdbcType=VARCHAR}, #{item.userCode,jdbcType=VARCHAR},
#{item.userName,jdbcType=VARCHAR}
)
</foreach>
;end;
</insert>也可以写成
<insert id="insertUserBatch" parameterType="java.util.List">
<begin />
INSERT INTO table_user (id, user_code, user_name)
values
<foreach collection="userList" item="item" index="index" separator=",">
(#{item.id,jdbcType=VARCHAR}, #{item.userCode,jdbcType=VARCHAR},
#{item.userName,jdbcType=VARCHAR})
</foreach>
<end />
</insert>通过以上方式可以解决mybatis批量插入报ORA-01461: can bind a LONG value only for insert into a LONG column的错误。
参考文章链接:mybatis批量插入报 ORA-01461-仅能绑定要插入LONG列的LONG值_mybatis ora-01461-CSDN博客
