Hibernate简单入门
2015年03月27日 15:14:20 Hibernate ⁄ 共 4980字 暂无评论 ⁄ 被围观 2,882次

Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,使得 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate 可以应用在任何使用 JDBC 的场合,既可以在 Java 的客户端程序使用,也可以在 Servlet/JSP 的 Web 应用中使用,最具革命意义的是,Hibernate 可以在应用 EJB 的 J2EE 架构中取代CMP,完成数据持久化的重任。

官网地址为:http://hibernate.org/

Hibernate 的运用也不难,主要是一些配置,在这里分享一个视频,一个对照视频实练的小程序,还有一些 jar 包之类的,供大家学习工作用。

Hibernate 主配置文件 hibernate.cfg.xml

Code   ViewPrint
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.   
  10.         <!-- Database connection settings -->  
  11.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  12.         <property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property>  
  13.         <property name="connection.username">root</property>  
  14.         <property name="connection.password">psw123456</property>  
  15.   
  16.         <!-- JDBC connection pool (use the built-in) -->  
  17. <!--        <property name="connection.pool_size">1</property>-->  
  18.   
  19.         <!-- SQL dialect -->  
  20.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  21.   
  22.         <!-- Enable Hibernate's automatic session context management -->  
  23. <!--        <property name="current_session_context_class">thread</property>-->  
  24.   
  25.         <!-- Disable the second-level cache  -->  
  26.         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>  
  27.   
  28.         <!-- Echo all executed SQL to stdout -->  
  29.         <property name="show_sql">true</property>  
  30.   
  31.         <!-- Drop and re-create the database schema on startup -->  
  32. <!--        <property name="hbm2ddl.auto">update</property>-->  
  33.   
  34.         <mapping resource="com/sinaapp/langtuteng/demo/hibernate/model/Student.hbm.xml"/>  
  35.   
  36.     </session-factory>  
  37.   
  38. </hibernate-configuration>  

 

对象的映射文件,和对象类文件放在一起,如 Student.hbm.xml

Code   ViewPrint
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="com.sinaapp.langtuteng.demo.hibernate.model">  
  7.     <class name="Student">  
  8.         <id name="id"></id>  
  9.         <property name="name"/>  
  10.         <property name="age"/>  
  11.     </class>  
  12. </hibernate-mapping>  

 

当然需要在 MySQL 数据库中创建 hibernate 数据库,然后再数据库中建立 student 表。

在测试类中,如果用下面的方法取得 session 对象,则会提示方法 buildSessionFactory() 已废弃。

Code   ViewPrint
  1. Configuration cfg = new Configuration();  
  2. SessionFactory sf = cfg.configure().buildSessionFactory();  
  3. Session session = sf.openSession();  
  4. // 开启事务  
  5. session.beginTransaction();  
  6. // 保存对象  
  7. Configuration cfg = new Configuration();  
  8. SessionFactory sf = cfg.configure().buildSessionFactory();  
  9. Session session = sf.openSession();  
  10. session.save(s);  
  11. // 事务提交  
  12. session.getTransaction().commit();  
  13. // 关闭资源  
  14. session.close();  
  15. sf.close();  

 

所以用改用下面的方式获取:

Code   ViewPrint
  1. Configuration cfg = new Configuration();  
  2. cfg.configure("hibernate.cfg.xml");  
  3. ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();  
  4. SessionFactory sf = cfg.buildSessionFactory(sr);  
  5. Session session = sf.openSession();  
  6. // 开启事务  
  7. session.beginTransaction();  
  8. // 保存对象  
  9. Configuration cfg = new Configuration();  
  10. SessionFactory sf = cfg.configure().buildSessionFactory();  
  11. Session session = sf.openSession();  
  12. session.save(s);  
  13. // 事务提交  
  14. session.getTransaction().commit();  
  15. // 关闭资源  
  16. session.close();  
  17. sf.close();  

 

另外,用 Annotation 来实现实体类的话,代码会更加简洁。比如再创建一个 Teacher 的实体类,用注解来标记类,如用 @Entity 来注解实体类,用 @Id 来注解表中的主键:

Code   ViewPrint
  1. package com.sinaapp.langtuteng.demo.hibernate.model;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.Id;  
  5.   
  6. @Entity  
  7. public class Teacher {  
  8.     private int id;  
  9.     private String name;  
  10.     private String title;  
  11.       
  12.     @Id  
  13.     public int getId() {  
  14.         return id;  
  15.     }  
  16.     public void setId(int id) {  
  17.         this.id = id;  
  18.     }  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     public String getTitle() {  
  26.         return title;  
  27.     }  
  28.     public void setTitle(String title) {  
  29.         this.title = title;  
  30.     }  
  31.       
  32. }  

 

需要在主配置文件中加入下面配置,来映射该实体类:
<mapping class="com.sinaapp.langtuteng.demo.hibernate.model.Teacher"/>

否则会报如下错误:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.sinaapp.langtuteng.demo.hibernate.model.Teacher

在测试代码中,原来实例化 Configuration 的话需要调用 AnnotationConfiguration() 方法,但在后来的版本中,该方法已废弃,可以用 Configuration() 方法。详细见 http://www.mkyong.com/hibernate/hibernate-the-type-annotationconfiguration-is-deprecated/ 的解释。

In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration“.

So , you can safely replace your “AnnotationConfiguration” with “Configuration” class.

 

其它部分如果想参阅,请下载 Demo 附件吧,下面提供一些下载链接:

代码小 Demo + hibernate包 + mysql驱动包下载:百度云共享下载 | 360云盘下载(提取码:4486)

给我留言

留言无头像?