使用Spring JdbcTemplate实现CLOB和BLOB的存取

概述

本文讲述通过Spring的JdbcTemplate来读写数据库大字段的实现方案,在一位网友的一篇博客的基础上,查看api文档整理而成。

写实现

 1 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  // reusable object
 2  LobHandler lobHandler = new DefaultLobHandler();  // reusable object
 3 
 4  jdbcTemplate.execute(
 5      "INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)",
 6      new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
 7        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
 8          ps.setString(1, name);
 9          lobCreator.setBlobAsBinaryStream(ps, 2, contentStream, contentLength);
10          lobCreator.setClobAsString(ps, 3, description);
11        }
12      }
13  );

类介绍

以下内容不求精准,加入了自己的理解和猜测,勿作学术用。

  • JdbcTemplate
    负责翻译,向JDBC接口提供可执行的指令
  • AbstractLobCreatingPreparedStatementCallBack
    负责提供参数设置的方法模板,并提供LobCreator实例
  • LobHandler
    负责提供LobCreator - AbstractLobCreatingPreparedStatementCallBack借助于它来提供LobCreator
  • LobCreator
    负责简化大字段的写入库

LobCreator API

LobCreator提供了多个接口来简化大字段的写入:对于BLOB,内容来源可以是InputStream、byte[];对于CLOB,内容来源可以是InputStream(自行确保都是ascii字符)、Reader、String。

使用Spring JdbcTemplate实现CLOB和BLOB的存取

读实现

 1 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  // reusable object
 2  final LobHandler lobHandler = new DefaultLobHandler();  // reusable object
 3 
 4  jdbcTemplate.query(
 5                  "SELECT content FROM imagedb WHERE image_name=?", new Object[] {name},
 6                  new AbstractLobStreamingResultSetExtractor() {
 7                          public void streamData(ResultSet rs) throws SQLException, IOException {
 8                                  FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs, 1), contentStream);
 9                          }
10                  }
11  );

批量读取

上述实现仅仅针对一条记录的读取,如果要读取多条记录,需要注意ResultSet实例已经指向了第一条记录(上述代码也没有调用rs.next()),可参考实现:

 1 public List<DFCL> batchRead(String sql, final String colNameFileName,
 2         final String colNameFileContent) {
 3     JdbcTemplate jt = JdbcService.getInstance().getJdbcTemplate();
 4      
 5     final LobHandler lobHandler = new DefaultLobHandler();
 6     final List<DFCL> dfclList = new ArrayList<DFCL>();
 7     jt.query(sql, new AbstractLobStreamingResultSetExtractor() {
 8         protected void streamData(ResultSet rs) throws SQLException,
 9                 IOException, DataAccessException {
10             do{   //SINOBEST 文件下载 ,此处的rs初始化时已经指向第一条记录  
11                 String name = rs.getString(colNameFileName);
12                 String content = lobHandler.getClobAsString(rs, colNameFileContent);
13                 
14                 DFCL cl = new DFCL(name, content);
15                 dfclList.add(cl);
16             }while(rs.next());  
17         }
18     });
19     return dfclList;
20 }

类介绍

以下内容不求精准,加入了自己的理解和猜测,勿作学术用。

  • JdbcTemplate
    同上
  • AbstractLobStreamingResultSetExtractor
    提供解析数据的方法模板,并提供ResultSet对象实例
  • LobHandler
    简化大字段的读取
  • FileCopyUtils
    spring提供的工具类,简化文件内容的传输

LobHandler API

LobHandler提供了多个接口来简化大字段的读入:对于BLOB,可以读为InputStream、byte[];对于CLOB,可以读为InputStream、Reader、String。和LobCreator的写入简化是对应的。

使用Spring JdbcTemplate实现CLOB和BLOB的存取

FileCopyUtils API

使用Spring JdbcTemplate实现CLOB和BLOB的存取