mybatis对CLOB类型的处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qincidong/article/details/82621541

mybatis插入时,当插入clob类型时报错,ORA-01461:仅能绑定要插入LONG列的LONG值。
mapper文件SQL如下

<insert id="addDutyPost" parameterType="dp">
    insert into sds_duty_post(id,title,content,creator,create_date)
    select SEQ_SDS_DUTY_POST.NEXTVAL,#{title,jdbcType=VARCHAR},#{content,jdbcType=CLOB},#{creator,jdbcType=VARCHAR},sysdate
    from dual where not exists(select 'x' from sds_duty_post where title=#{title})
</insert>

这个SQL在长度<4000时,工作正常;大于4000,会提示ORA-01461.
在插入时指定了jdbcType=CLOB,但仍然不奏效。


在网上查了很久,有可能问题是出现在当从dual中取数据时,会将clob对象的字段转为Long型。
所以改写一下SQL

<select id="selPostByTitle" parameterType="string" resultType="dp">
    select id as postid,title,content,creator,create_date,updator,update_date from sds_duty_post where title=#{value}
</select>
<insert id="addDutyPost" parameterType="dp">
    insert into sds_duty_post(id,title,content,creator,create_date)
    values (SEQ_SDS_DUTY_POST.NEXTVAL,#{title,jdbcType=VARCHAR},#{content,jdbcType=CLOB},#{creator,jdbcType=VARCHAR},sysdate)
</insert>

我这里分成了2个SQL,selPostByTitle用来实现根据title查询记录,addDutyPost用来添加记录。只是最开始的SQL是合二为一。
拆分后问题即解决,查询不需要指定jdbcType=CLOB。

猜你喜欢

转载自blog.csdn.net/qincidong/article/details/82621541