Spring 数据库处理Clob/Blob大对象

#概述

使用Spring的时候需求上难免说需要存储一下几种类型:

  • 文本
  • 图片
  • 二进制

处理对象

Spring 支持通过LobCreator/LobHandler进行处理大对象

  • BLOB
  • byte[] — getBlobAsBytes and setBlobAsBytes
  • InputStream — getBlobAsBinaryStream and setBlobAsBinaryStream
  • CLOB
  • String — getClobAsString and setClobAsString
  • InputStream — getClobAsAsciiStream and setClobAsAsciiStream
  • Reader — getClobAsCharacterStream and setClobAsCharacterStream

入口

图片标题

看到方法名就知道,在调用前会被执行一遍,刚好看看这个对象的实现,只有AbstractLobCreatingPreparedStatementCallback,接下来看看源码,看看有没有例子。 图片标题就是我想要的

使用例子

final File blobIn = new File("spring2004.jpg");
final InputStream blobIs = new FileInputStream(blobIn);
final File clobIn = new File("large.txt");
final InputStream clobIs = new FileInputStream(clobIn);
final InputStreamReader clobReader = new InputStreamReader(clobIs);
jdbcTemplate.execute(
    "INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
    //new DefaultLobHandler() or new OracleLobHandler()
    new AbstractLobCreatingPreparedStatementCallback(lobHandler) { 
        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
            ps.setLong(1, 1L);
            lobCreator.setClobAsCharacterStream(ps, 2, clobReader, (int)clobIn.length()); 
            lobCreator.setBlobAsBinaryStream(ps, 3, blobIs, (int)blobIn.length()); 
        }
    }
);
blobIs.close();
clobReader.close();

执行步骤:

    1. 获取 lobHandler 可以是 DefaultLobHandler
    1. 使用方法 setClobAsCharacterStream 设置CLOB内容
    1. 使用方法 setBlobAsBinaryStream 设置BLOB内容

其他方法

setBlobAsBinaryStream setClobAsAsciiStream setClobAsCharacterStream

转载于:https://my.oschina.net/u/1983041/blog/745196

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值