知识点

【 


下载地址http://www.hibernate.org,本教程使用3.2.5。 

将下载目录/hibernate3.jar和/lib下的hibernate运行时必须的包加入classpath中: 

 antlr.jar,cglib.jar,asm.jar,commons-collections.jar,commons-logging.jar,jta.jar,dom4j.jar 



配置文件hibernate.cfg.xml和hibernate.properties,XML和properties两种,这两个文件的作用一样,提供一个即可,推荐XML格式,下载目录/etc下是示例配置文件。 

 可以在配置文件指定: 

 数据库的URL、用户名、密码、JDBC驱动类、方言等。 

 启动时Hibernate会在CLASSPATH里找这个配置文件。 

映射文件(hbm.xml,对象模型和关系模型的映射)。在/eg目录下有完整的hibernate示例。 



Domain Object限制 

 1.默认的构造方法(必须的)。 

 2有无意义的标示符id(主键)(可选) 

 3非final的,对懒加载有影响(可选) 

 Domain Java Object(User) 

public class User { 

 private int id; 

 private String name; 

 private Date birthDay; 


 //getter setter… 

} 


类映射文件 

类名.hbm.xml 

<?xml version="1.0"?> 

<hibernate-mapping package=“cn.itcast.domain"> 

<class name="User" table="user"> 

 <id name="id"> 

 <generator class="native"/> 

 </id> 

 <property name="name"/> 

 <property name="birthday”/> 

</class> 

</hibernate-mapping> 


Java代码: 


 1.初始化代码(只做一次) 

 Configuration cfg = new Configuration(); 

 cfg.configure(“config.cfg.xml”); 

 也可以通过cfg.setProperty设置属性。 

 SessionFactory sessionFactory = cfg.buildSessionFactory(); 

 2.模板代码 

Session session = null;Transaction tx = null; 

try{ 

 session = sessionFactory.openSession(); 

 tx = session.beginTransaction(); 

 //…你的代码save,delete,update,get… 

 tx.commit(); 

}catch(Exception e){ 

 if(tx !=null)tx.rollback();throw e; 

}finally{ 

 if(session != null)session.close(); 

} 


】 


第一搭建环境: 


<1、导入hibernate相关*.jar包和数据库驱动 


<2、配置hibernate配置文件hibernate.cfg.xml模版可从hibernate例子得到 

<!DOCTYPE hibernate-configuration PUBLIC 

 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 

 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 


<hibernate-configuration> 

 <session-factory> 

 <property name="show_sql">true</property> 


 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 

 <!-- jdbc:mysql:///test 以下表示localhost 3306 test:数据库--> 

 <property name="hibernate.connection.url">jdbc:mysql:///student</property> 

 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 

 <property name="hibernate.connection.username">root</property> 

 <property name="hibernate.connection.password">liyong</property> 


 <!-- 下面指定方言 --> 

 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

 <!-- 下面指定是否让hibernate产生ddl语句 DDL:数据定义语言--> 

 <property name="hbm2ddl.auto">update</property> 


 <mapping resource="com/liyong/domain/User.hbm.xml"/> 

 </session-factory> 

</hibernate-configuration> 



这里环境搭建完成 


第二步:编写bean User.java 普通的java bean 


public class User { 


 private int id; 

 private String name; 

 private Date birthday; 


 public User(){} 

 public User(String name,Date birthday){ 

 this.name=name; 

 this.birthday=birthday; 

 } 


 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 Date getBirthday() { 

 return birthday; 

 } 

 public void setBirthday(Date birthday) { 

 this.birthday = birthday; 

 } 



} 


第三步:编写这个类的对象关系映射文件User.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"> 

<hibernate-mapping package="com.liyong.domain"> 


 <class name="User"> 

 <!-- 生成主键 以natvie --> 

 <id name="id"> 

 <generator class="native" /> 

 </id> 

 <property name="name" unique="true"/> 

 <property name="birthday" /> 

 </class> 


</hibernate-mapping> 


完成这个配置过后到hibernate.cfg.xml中指明这个关系映射,因为User.hbm.xml默认是不会被加载 所有在hibernate.cfg.xml中添加如下映射 当hibernate.cfg.xml被加载的时候去加载 对象关系映射文件 

 <mapping resource="com/liyong/domain/User.hbm.xml"/> 



第四步:编写单元测试 


 @Test 

 public void save() 

 { 

 Configuration cfg=new Configuration(); 

 cfg.configure(); 

// cfg.setProperties(properties) 

 // 

 SessionFactory factory=cfg.buildSessionFactory(); 

 Session sessin=factory.openSession(); 

 Transaction ts=sessin.beginTransaction(); 

 User user=new User("李勇",new Date()); 

 sessin.save(user); 

 ts.commit(); 

 sessin.close(); 

 factory.close(); 

 }