Hibernate是当前最流行的ORM框架,ORM即Object/Relation Mapping, 对象关系数据库映射。
ORM是一种规范:完成面向对象语言到关系数据库的映射。
本文将演示一个最简单的demo,完成往数据库中添加记录的功能。
准备工作:
本文采用Hibernate 3.6.0版本,用到的jar包如下图:
一个典型的Hibernate Demo应该包含一下三个文件:
1. PO,持久化对象。利用它可完成持久化操作。Hibearnate是低侵入式的设计,只需一个POJO(普通java对象)即可,无需继承任何父类和接口。
2. *.hbm.xml。POJO与数据库表之间的映射配置文件。
3. *.cfg.xml。Hibernate的配置文件,包括数据库驱动、URL、用户名、密码等信息。
数据库表:
use hibernateDemo; drop table if exists TMP_EMP; create table TMP_EMP ( ID int(4) primary key, NAME VARCHAR(50), BIRTHDAY DATE, SEX CHAR(1), SALARY float(8,2) );
POJO:
1 // default package 2 // Generated 2013-4-29 17:04:49 by Hibernate Tools 3.4.0.CR1 3 4 import java.util.Date; 5 6 /** 7 * TmpEmp generated by hbm2java 8 */ 9 public class TmpEmp implements java.io.Serializable { 10 11 private int id; 12 private String name; 13 private Date birthday; 14 private Character sex; 15 private Float salary; 16 17 public TmpEmp() { 18 } 19 20 public TmpEmp(int id) { 21 this.id = id; 22 } 23 24 public TmpEmp(int id, String name, Date birthday, Character sex, 25 Float salary) { 26 this.id = id; 27 this.name = name; 28 this.birthday = birthday; 29 this.sex = sex; 30 this.salary = salary; 31 } 32 33 public int getId() { 34 return this.id; 35 } 36 37 public void setId(int id) { 38 this.id = id; 39 } 40 41 public String getName() { 42 return this.name; 43 } 44 45 public void setName(String name) { 46 this.name = name; 47 } 48 49 public Date getBirthday() { 50 return this.birthday; 51 } 52 53 public void setBirthday(Date birthday) { 54 this.birthday = birthday; 55 } 56 57 public Character getSex() { 58 return this.sex; 59 } 60 61 public void setSex(Character sex) { 62 this.sex = sex; 63 } 64 65 public Float getSalary() { 66 return this.salary; 67 } 68 69 public void setSalary(Float salary) { 70 this.salary = salary; 71 } 72 73 }
hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 4 <hibernate-configuration> 5 <session-factory name=""> 6 <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> 7 <property name="hibernate.connection.password">wailiee</property> 8 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/HibernateDemo</property> 9 <property name="hibernate.connection.username">root</property> 10 <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> 11 <mapping resource="TmpEmp.hbm.xml"/> 12 </session-factory> 13 </hibernate-configuration>
TmpEmp.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2013-4-29 17:07:22 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="TmpEmp" table="TMP_EMP"> 7 <id name="id" type="int"> 8 <column name="ID" /> 9 <!-- 指定主键生成策略 --> 10 <generator class="assigned" /> 11 </id> 12 <property name="name" type="java.lang.String"> 13 <column name="NAME" /> 14 </property> 15 <property name="birthday" type="java.util.Date"> 16 <column name="BIRTHDAY" /> 17 </property> 18 <property name="sex" type="java.lang.Character"> 19 <column name="SEX" /> 20 </property> 21 <property name="salary" type="java.lang.Float"> 22 <column name="SALARY" /> 23 </property> 24 </class> 25 </hibernate-mapping>
Client
1 import org.hibernate.Session; 2 import org.hibernate.SessionFactory; 3 import org.hibernate.Transaction; 4 import org.hibernate.cfg.Configuration; 5 6 7 public class Client0 { 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Configuration conf = new Configuration(); 15 // 下面的方法默认加载hibernate.cfg.xml文件 16 conf.configure(); 17 18 // 创建sessionFactory 19 SessionFactory sf = conf.buildSessionFactory(); 20 // 创建session 21 Session sess = sf.openSession(); 22 // 开始事务,Hibernate必须显式的手动提交事务 23 Transaction tx = sess.beginTransaction(); 24 25 TmpEmp tmpEmp = new TmpEmp(); 26 tmpEmp.setId(5); 27 tmpEmp.setName("yz"); 28 29 // 保存 30 sess.save(tmpEmp); 31 32 // 提交事务 33 tx.commit(); 34 35 // 关闭session 36 sess.flush(); 37 sess.close(); 38 sf.close(); 39 } 40 41 }
运行结果: