首页 > 技术文章 > 使用mybatis向oracle数据库插入数据异常

widget90 2018-05-23 10:47 原文

遇到了使用mybatis向oracle数据库插入数据异常的问题,

具体的报错如下:org.springframework.jdbc.UncategorizedSQLException:
### Error updating database.  Cause: java.sql.SQLException: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值


mybatis的脚本如下:

     <insert id="insertInfos" parameterType="java.util.List">
        INSERT INTO
        INFO(
        RECORD_ID,
        CONTENT
        )
        <foreach close=")" collection="list" item="item" index="index"
            open="(" separator="union">
            select
            #{item.recordId,jdbcType=VARCHAR},
            #{item.content,jdbcType=VARCHAR}
            from dual
        </foreach>
    </insert>

 

后面做了很多测试分析,确认是因为参数List过长,导致此问题,后面修改mybatis的脚本,

在最外层使用BEGIN、END包裹,然后使用foreach循环并在循环完成具体的数据库字段映射;

如下:

    <insert id="insertInfos" parameterType="java.util.List">
        BEGIN
        <foreach close="" collection="list" item="item" index="index"
            open="" separator=";">
            INSERT INTO
            INFO(
            RECORD_ID,
            REQ_CONTENT
            )
            VALUES(
            #{item.recordId,jdbcType=VARCHAR},
            #{item.rspContent,jdbcType=VARCHAR}
            )
        </foreach>
        ;END;
    </insert>

 

注意在后面可以正确使用的脚本中,separator=";"  这里的分号不可以去掉的!

推荐阅读