相关介绍-
Spring 让 LOB 数据操作变得简单易行(http://www.ibm.com/developerworks/cn/java/j-lo-spring-lob/)
具体代码:
Blob操作也类似Clob,也是通过LobHandler 类的方法进行操作。
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.stereotype.Repository;
import com.commom.model.PageBean;
/**
* 类:ReportTemplateDaoImpl.java
* 时间:2016-1-8
* 说明:模板
*/
@Repository
public class ReportTemplateDaoImpl extends BaseDao
{
/**
* 根据条件获取模板的信息
* @param queryMap:查询条件
* @return
*/
public List
相关查考文章:
利用spring的jdbcTemplate处理blob、clob
spring定义了一个以统一的方式操作各种数据库的Lob类型数据的LobCreator(保存的时候用),同时提供了一个LobHandler为操作二进制字段和大文本字段提供统一接口访问。
举例,例子里面的t_post表中post_text字段是CLOB类型,而post_attach是BLOG类型:
public class PostJdbcDao extends JdbcDaoSupport implements PostDao {
private LobHandler lobHandler;
private DataFieldMaxValueIncrementer incre;
public LobHandler getLobHandler() {
return lobHandler;
}
public void setLobHandler(LobHandler lobHandler) {
this.lobHandler = lobHandler;
}
public void addPost(final Post post) {
String sql = " INSERT INTO t_post(post_id,user_id,post_text,post_attach)"
+ " VALUES(?,?,?,?)";
getJdbcTemplate().execute(
sql,
new AbstractLobCreatingPreparedStatementCallback(
this.lobHandler) {
protected void setValues(PreparedStatement ps,
LobCreator lobCreator) throws SQLException {
ps.setInt(1, incre.nextIntValue());
ps.setInt(2, post.getUserId());
lobCreator.setClobAsString(ps, 3, post.getPostText());
lobCreator.setBlobAsBytes(ps, 4, post.getPostAttach());
}
});
}
}
设置相对应的配置文件(Oracle 9i版本),Oracle的数据库最喜欢搞搞特别的东西啦:
Oracle 10g或其他数据库如下设置:
读取BLOB/CLOB块,举例:
public List getAttachs(final int userId){
String sql = "SELECT post_id,post_attach FROM t_post where user_id =? and post_attach is not null";
return getJdbcTemplate().query(
sql,new Object[] {userId},
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Post post = new Post();
int postId = rs.getInt(1);
byte[] attach = lobHandler.getBlobAsBytes(rs, 2);
post.setPostId(postId);
post.setPostAttach(attach);
return post;
}
});
}