我为此找到了两个解决方案。
春天的方法
非Spring方法
在第一种情况下,只需注入 aJdbcTemplate并使用以下帮助程序代码:
public static LobHandler getAppropriateLobHandler(Factory factory) {
LobHandler lobHandler = null;
switch( factory.getDialect() ) {
case ORACLE: lobHandler = new OracleLobHandler(); break;
default: lobHandler = new DefaultLobHandler(); break;
}
return lobHandler;
}
Run Code Online (Sandbox Code Playgroud)
使用以下代码读取 BLOB:
return jdbcTemplate.queryForObject(
"select BLOB from TABLE where PK = ?",
args,
new RowMapper<InputStream>() {
@Override
public InputStream mapRow( ResultSet rs, int rowNum ) throws SQLException {
return lobHandler.getBlobAsBinaryStream( rs, 1 );
}
}
);
Run Code Online (Sandbox Code Playgroud)
并这样写:
jdbcTemplate.execute(
"update TABLE set BLOB = ? where PK = ?",
new AbstractLobCreatingPreparedStatementCallback( lobHandler ) {
@Override
protected void setValues( PreparedStatement ps, LobCreator lobCreator ) throws SQLException {
lobCreator.setBlobAsBinaryStream( ps, 1, stream, sizeInBytes );
ps.setLong( 2, pk );
}
}
);
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,除非您有 Oracle,否则请使用 JDBC 读取和写入 BLOB。当你有Oracle时,你需要使用他们特殊的C/BLOB定位器模式。
| 归档时间: |
|
| 查看次数: |
1432 次 |
| 最近记录: |