Mybatis向oracle数据库插入clob字段,长度大于4000时报错

重点

  1. 先用empty_clob()代替要插入的clob数据
  2. 用真正的数据去替换原先插进去的空clob
  3. 一定要加上对clob字段的非空判断,不然当clob为空时会报错

创建表

create table test(
      id varchar2(200),
      clob_filed clob
)

mybatis对应sql

<!-- 
    插入Clob字段长度超过4000测试
        NO.1 先用empty_clob()代替要插入的clob数据
        No.2 用真正的数据去替换原先插进去的空clob
        No.3 一定要加上对clob字段的非空判断,不然当clob为空时会报错
 -->
<update id="insertClobTest">
    <foreach collection="items" item="item" index="index" open="begin" close="end;">
        merge into test a using (
            select #{item.id} as id,empty_clob() as clob_filed from dual
        ) temp on (temp.id = a.id)
        when matched THEN update set a.clob_filed = temp.clob_filed
        when not matched then insert(id,clob_filed) values(temp.id, temp.clob_filed);

      <if test="item.clob_filed != null">
            update test set clob_filed = #{item.clob_filed} where id = #{item.id};
      </if> 
    </foreach>
</update>

后台调用

List<Map<String,Object>> newsList = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String, Object>();
String str = "124aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa123";
map.put("id","a");
map.put("clob_filed",str);
newsList.add(map);

Map<String,Object> map1 = new HashMap<String, Object>();
map1.put("id","b");
map1.put("clob_filed",str);
newsList.add(map1);

HashMap<String, Object> param = new HashMap<String, Object>();
param.put("items", newsList);

//执行插入语句
insertSql("insertClobTest", param);
展开阅读全文

没有更多推荐了,返回首页