关键代码:
// 大文本 private String content; // 二进制类型 private Blob image; // 时间类型 private Date postTime;
<!-- 映射大文本对象 可以在property节点设置type="java.sql.Blob"(相对来说不这么精确) 也可以在property.column直接设置数据库类型(够精确) --> <property name="content" > <column name="CONTENT" sql-type="mediumtext"/> </property> <!-- 映射时间 --> <property name="postTime" type="java.util.Date"> <column name="POSTTIME" ></column> </property> <!-- 映射二进制对象 --> <property name="image" > <column name="IMAGE" sql-type="mediumblob"></column> </property>Q
详细:
News.java:
package com.shuoeasy.test; import java.sql.Blob; import java.util.Date; public class News { private int id; private String title; // 大文本 private String content; // 二进制类型 private Blob image; // 时间类型 private Date postTime; public News(){} public News(String title, String content) { super(); this.title = title; this.content = content; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getPostTime() { return postTime; } public void setPostTime(Date postTime) { this.postTime = postTime; } public Blob getImage() { return image; } public void setImage(Blob image) { this.image = image; } @Override public String toString() { return "News [id=" + id + ", title=" + title + ", content=" + content + ", image=" + image + ", postTime=" + postTime + "]"; } }
News.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.shuoeasy.test.News" table="NEWS"> <id name="id" type="int"> <column name="ID" /> <!-- 指定主键的生成方式,native:使用数据库本地方式 --> <generator class="native" /> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <!-- 映射大文本对象 可以在property节点设置type="java.sql.Blob"(相对来说不这么精确) 也可以在property.column直接设置数据库类型(够精确) --> <property name="content" > <column name="CONTENT" sql-type="mediumtext"/> </property> <!-- 映射时间 --> <property name="postTime" type="java.util.Date"> <column name="POSTTIME" ></column> </property> <!-- 映射二进制对象 --> <property name="image" > <column name="IMAGE" sql-type="mediumblob"></column> </property> </class> </hibernate-mapping>
AppTest.java
package com.shuoeasy.test; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.SQLException; import java.util.Date; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { Session session; SessionFactory sf; @Before public void init() { Configuration conf = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr); session = sf.openSession(); session.beginTransaction(); System.out.println("init"); } @After public void destory() { session.getTransaction().commit(); session.close(); sf.close(); System.out.println("dectory"); } @Test public void testInsert() throws IOException{ News news = new News("标题","内容"); // 保存时间数据 news.setPostTime(new Date()); // 保存二进制数据 InputStream stream = new FileInputStream("data/1.jpg"); Blob image = Hibernate.getLobCreator(session).createBlob(stream, stream.available()); news.setImage(image); session.save(news); } @Test public void testGet() throws SQLException { News news = (News) session.get(News.class, 1); // 读取二进制文件 Blob image = news.getImage(); InputStream stream = image.getBinaryStream(); // 保存文件代码略 System.out.println(stream); System.out.println(news); } }