前言:
BLOB用来存储大量二进制数据。如图片、音乐等,转为二进制数再存储
CLOB用来存储大量文本数据。如HTML页面等,varchar2最大是4000,预计会超过4000的用Clob
正文:
1,我用的是java + mybatis,直接用String处理就可以了。String最大能存4G
数据库:创建表
-- Create table create table CLOB_TEST ( id VARCHAR2(32) default sys_guid(), content CLOB )
实体类:
package com.bf.test.entity; public class ClobTest { private String id; private String content; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
查询语句:sql.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.bf.labor.dao.IClobDao"> <resultMap id="BaseResultMap" type="com.bf.test.entity.ClobTest" > <result column="ID" property="id" jdbcType="VARCHAR" /> <result column="CONTENT" property="content" jdbcType="VARCHAR" /> </resultMap> <select id="getList" resultMap="BaseResultMap"> select * from CLOB_TEST </select> <insert id="insert" > insert into CLOB_TEST (CONTENT) values (#{content}) </insert> </mapper>
测试类:
package com.test; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import com.bf.labor.entity.ClobTest; import com.bf.labor.service.ClobService; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration("web") @ContextConfiguration(locations = "file:G:/WIM/Source/webapp/WEB-INF/spring-core-config.xml") public class ClobServiceTest { @Autowired private ClobService clobService; @Test public void test() { List<ClobTest> list = this.clobService.getList(); System.out.println("test=" + list); } @Test public void test2() { String str = ""; for (int i = 0; i < 5000; i++) { str = str + "a"; } System.out.println("str=" + str); ClobTest newInfo = new ClobTest(); newInfo.setContent(str); this.clobService.insert(newInfo); } }
2,Clob字段转换成字符串。数据库数据迁移的时候要格外注意这一点,不做处理的话,Clob字段的值为空
方法1:dbms_lob.substr()
注意:dbms_lob.substr(content),超过4000字符,会报错
参考博客:
Oracle中将Clob字段转换成字符串 - sqyNick - CSDN博客
https://blog.csdn.net/u010670151/article/details/52210333