大家好,又见面了,我是全栈君
基础知识:
怎样映射?
若希望精确映射sql类型,能够使用sql-type属性。比如:
<!– 映射大对象 –> <!– 若希望精确映射 SQL 类型, 能够使用 sql-type 属性. –> <property name=”content”> <column name=”CONTENT” sql-type=”mediumtext”></column> </property>
<property name=”image”> <column name=”IMAGE” sql-type=”mediumblob”></column> </property>
保存二进制blob:
@Test
public void testBlob() throws Exception{
News news = new News();
news.setAuthor("cc");
news.setContent("CONTENT");
news.setDate(new Date());
news.setDesc("DESC");
news.setTitle("CC");
InputStream stream = new FileInputStream("Hydrangeas.jpg");
Blob image = Hibernate.getLobCreator(session)
.createBlob(stream, stream.available());
news.setImage(image);
session.save(news);
}
读取二进制blob:
@Test
public void testBlob() throws Exception{
News news = (News) session.get(News.class, 1);
Blob image = news.getImage();
InputStream in = image.getBinaryStream();
System.out.println(in.available());
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117872.html原文链接:https://javaforall.cn