oracle--clob
最近,收到这样的异常邮件:
Error updating database. Cause: java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
查了下,应该是数据库varchar2(4000)写入的数据超过4000字节导致的,换成clob吧。
mybatis 中相应修改
mapper.xml文件中
</resultMap>
<result column="MSG_CONTENT" property="msgContent" jdbcType="CLOB" />
</resultMap>
insert操作中
#{msgContent,jdbcType=CLOB}
顺便把一些大对象的操作也看了下,DBMS_LOB维护内部lob常用操作如下
- DBMS_LOB.write
PROCEDURE WRITE(lob_loc IN OUT BLOB,
amount IN BINARY_INTEGER,
offset IN INTEGER,
buffer IN RAW);
PROCEDURE WRITE(lob_loc IN OUT CLOB CHARACTER SET any_cs,
amount IN BINARY_INTEGER,
offset IN INTEGER,
buffer IN VARCHAR2 CHARACTER SET lob_loc%charset);
各参数的含义为:
lob_loc:要写入的LOB定位器。
amount:写入LOB中的字节数。
offset:指定开始操作的偏移量。
buffer:指定写操作的缓冲区。
示例代码:
DECLARE lobloc CLOB; buffer VARCHAR2(2000); amount NUMBER := 20; offset NUMBER := 1; BEGIN --初始化要写入的数据 buffer := 'This is a writing example'; amount := length(buffer); SELECT document INTO lobloc -- 获取定位器并锁定行 FROM view_sites_info WHERE site_id = 100 FOR UPDATE; dbms_lob.write(lobloc, amount, 1, buffer); COMMIT; END;
- DBMS_LOB.read
PROCEDURE READ(lob_loc IN BLOB,
amount IN OUT BINARY_INTEGER,
offset IN INTEGER,
buffer OUT RAW);
PROCEDURE READ(lob_loc IN CLOB CHARACTER SET any_cs,
amount IN OUT BINARY_INTEGER,
offset IN INTEGER,
buffer OUT VARCHAR2 CHARACTER SET lob_loc%charset);
各参数的含义为:
lob_loc:要读取的LOB定位器。
amount:要读取的字节数。
offset:开始读取操作的偏移量。
buffer:存储读操作结果的缓冲区。
示例代码:
DECLARE lobloc CLOB; buffer VARCHAR2(2000); amount NUMBER := 2; offset NUMBER := 6; BEGIN SELECT document INTO lobloc --获取定位器 FROM lob_store WHERE lob_id = 100; dbms_lob.read(lobloc, amount, offset, buffer); --读取数据到缓冲区 dbms_output.put_line(buffer); --显示缓冲区中的数据 COMMIT; END;
- DBMS_LOB.append
PROCEDURE append(dest_lob IN OUT NOCOPY BLOB,
src_lob IN BLOB);
PROCEDURE append(dest_lob IN OUT NOCOPY CLOB CHARACTER SET any_cs,
src_lob IN CLOB CHARACTER SET dest_lob%charset);
各个参数的含义如下:
dest_lob是被源lob添加到的目标lob的定位器。
src_lob是源lob的定位器。
any_cs用来指定字符集。
- DBMS_LOB.substr
dbms_lob.substr( lob_loc in blob, amount in integer := 32767, offset in integer := 1) return raw; dbms_lob.substr( lob_loc in clob character set any_cs, amount in integer := 32767, offset in integer := 1) return varchar2 character set lob_loc%charset;
各个参数的含义如下:
lob_loc是substr函数要操作的大型对象定位器
amount是要从大型对象中抽取的字节数
offset是指从大型对象的什么位置开始抽取数据
参考文章: