其实之前的例子中也涉及到了映射。Hibernate的特点就是利用Hibernate.cfg.xml配置数据库相关信息,xxx.hbm.xml配置对象与数据库表的映射信息。
这次写的例子涉及到大文本信息的存储和图片信息在数据库中的存储,比较有实用价值。
实体信息
package test.hibernate.domain; import java.util.Date; public class User { private int id; private String name; private int age; private Date birthday; private String desc;//说明 private byte[] photo;//头像图片 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public byte[] getPhoto() { return photo; } public void setPhoto(byte[] photo) { this.photo = photo; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { // TODO Auto-generated method stub return "[User:id="+id+",name:"+name+"]"; } }
测试类
package test.hibernate.domain; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; public class App { private static SessionFactory sessionFactory; static { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); sessionFactory = cfg.buildSessionFactory(); } @Test public void testSave() throws Exception { // 读取图片 InputStream in = new FileInputStream("E:/图标/my02.png"); byte[] photo = new byte[in.available()]; in.read(photo); in.close(); // 创建对象 User user = new User(); user.setName("张三"); user.setAge(21); user.setBirthday(new Date()); user.setDesc("一篇论文。。。。。。。。。。。。。。。。。。。。。。。。"); user.setPhoto(photo); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); session.save(user); transaction.commit(); session.close();// 关闭session,释放资源 } @Test public void testGet() throws Exception { Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = (User) session.get(User.class, 1); System.out.println(user.getId()); System.out.println(user.getName()); System.out.println(user.getBirthday()); System.out.println(user.getDesc()); // 将图片写到F盘的copy.png OutputStream outputStream = new FileOutputStream("F:/copy.png"); outputStream.write(user.getPhoto()); outputStream.close(); transaction.commit(); session.close(); } }
<img src="http://img.blog.csdn.net/20150714095500916?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" />
主配置文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="foo"> <!-- 配置数据库信息 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_2015</property> <!-- 可简写为<property name="connection.url">jdbc:mysql:///hibernate_2015</property> --> <property name="connection.username">root</property> <property name="connection.password">686175</property> <!-- 显示生成的sql语句,不写的话默认是false --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="test/hibernate/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
加了hbm2ddl.auto属性,用于更新表结构
在MySQL- Front里查看
版权声明:本文为博主原创文章,未经博主允许不得转载。http://blog.csdn.net/lindonglian
时间: 2024-10-10 12:43:38