【Oracle】【35】BLOB字段和CLOB字段

前言:java

BLOB用來存儲大量二進制數據。如圖片、音樂等,轉爲二進制數再存儲web

CLOB用來存儲大量文本數據。如HTML頁面等,varchar2最大是4000,預計會超過4000的用Clobspring

正文:sql

1,我用的是java + mybatis,直接用String處理就能夠了。String最大能存4G數據庫

數據庫:建立表mybatis

-- Create table
create table CLOB_TEST
(
  id      VARCHAR2(32) default sys_guid(),
  content CLOB
)

實體類:app

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.xmlwebapp

<?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字段的值爲空ui

方法1:dbms_lob.substr()

注意:dbms_lob.substr(content),超過4000字符,會報錯

參考博客:

Oracle中將Clob字段轉換成字符串 - sqyNick - CSDN博客
https://blog.csdn.net/u010670151/article/details/52210333

相關文章
相關標籤/搜索