Lob代表大对象数据,包括BLOB和CLOB两种类型数据,前者用于存储大块的二进制数据,如图片和视频数据等,而后者用于存储长文本数据,如论坛帖子内容,产品详细描述等。在不同的数据库中,大对象对应的字段类型往往不一样,如oracle对应的是BLOB/CLOB;Mysql对应的BLOB/LONGTEXT;SQLSERER对应IMAGE/TEXT,有些数据库对大对象类型可以像简单类型一样访问,如mysql的LONGTEXT的操作方式和VARCHAR类型一样,在一半情况下,LOB类型数据的访问不同于其他简单类型的数据,用户可能会以流的方式操作LOB类型的书。此外,LOB类型的数据访问并不是线程安全的,需要分配相应的数据库资源,并在操作后完成释放。最后oracle非常有个性地采用非jdbc标准的api操作lob数据,spring为此在org.springframework.jdbc.support.lob包中提供帮助类
LobCreator
虽然jdbc定义了两个操作LOB类型的接口:java.sql.BLOB,java.sql.CLOB,但是有些厂商的jdbc驱动程序并不支持这两个接口。为此,spring定义了一个独立于java.sql.blob/java.sql.clob接口,以统一的方式操作各种数据库LOB类型的lobCreator接口,因为LobCreator本身执有Lob所对应的数据库资源,所以它不是线程安全,一个LOBcREATOR只能操作一个Lob数据
LobCreator接口对应的方法
void close();关闭会话,并释放Lob资源
void setBlobAsBinaryStream(PreparedStatement ps,int paramIndex,InputStream,int contentLength)通过流填充Blob数据
void setBlobAsBytes(PreparedStatement ps,int paramIndex,byte[] content);通过二进制填充Blob数据
void setClobAsCharacterStream(PreparedStatement ps,int paramIndex,Reader characterStream,int contentLength);通过Unicode字符流填充clob数据
void setClobAsAsciiStream(PreparedStatement ps,int paramIndex,InputStream asciiStream,int contentLength);通过Ascii字符流填充clob数据
void setClobAsStream(PreparedStatement ps,int paramIndex,String content) 通过字符串填充clob数据
LobHandler
lobhandler接口为操作大二进制字段和大文本字段提供统一的访问接口访问,不管底层数据库是以大对象的方式还是一般数据类型进行操作,lobhandler还充当了lobcreator的工厂类
InputStream getBlobAsBinaryStream(ResultSet rs,int columnIndex)从结果集中返回InputStream,通过InputStream读取Blob数据
byte[] getBlobAsBytes(ResultSet rs,int columnIndex);以二进制数据的方式读取结果集中的Blobshuju
InputStream getClobAsAssciiStream(ResultSet rs,int columnIndex)从结果集中返回InputStream,通过InputStream 以Asscii字符流方式读取Blob数据
Reader getClobAsCharacterStream(ResultSet rs,int columnIndex);从结果集中获取Unicode字符流Reader,并通过Reader以Unicode字符流方式读取Clob数据
getClobAsStream(ResultSet rs,int columnIndex)会从结果集中以字符串的方式获取clob数据
xml配置文件
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/>
测试类
@Test
public void handle14() throws IOException{
File file = new File("F:\\e.png");
final byte[] mockImg = FileCopyUtils.copyToByteArray(file);
String sql = "insert into tb_content(image)values(?)";
jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
@Override
protected void setValues(PreparedStatement ps, LobCreator lobCreator)
throws SQLException, DataAccessException {
lobCreator.setBlobAsBytes(ps, 1, mockImg);
}
});
}
@Test
public void handle15() throws IOException{
final File file = new File("F:\\d.png");
String sql = "select * from tb_content where id=3";
jdbcTemplate.query(sql, new RowMapper(){
@Override
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
byte[] attach = lobHandler.getBlobAsBytes(rs, 1);
try {
FileOutputStream outputStream = new FileOutputStream(file);
try {
outputStream.write(attach, 0, attach.length);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
});
}
}
spring+hibernate处理Blob类型的数据
实体类
@Entity(name = "tb_news")
public class NewsContent {
private Integer id;
private byte[] photo;
public NewsContent(){}
public NewsContent(Integer id){
this.id = id;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name ="photo",columnDefinition="longblob")
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
}
测试:
@Test
public void handle2() throws IOException{
NewsContent content = new NewsContent();
File file = new File("F:\\e.png");
byte[] bytes = FileUtils.readFileToByteArray(file);
content.setPhoto(bytes);
Session session = sessionFactory.openSession();
session.save(content);
}
@Test
public void handle3() throws IOException{
File file = new File("F:\\e.png");
FileOutputStream out = new FileOutputStream(file);
Session session = sessionFactory.openSession();
NewsContent content = (NewsContent)session.get(NewsContent.class, 1);
byte[] bytes = content.getPhoto();
out.write(bytes, 0, bytes.length);
}