1:建立maven project

 

2:pom中添加hibernate支持包

 1 <dependency>
 2     <groupId>mysql</groupId>
 3     <artifactId>mysql-connector-java</artifactId>
 4     <version>5.1.31</version>
 5 </dependency>
 6 
 7 <dependency>
 8     <groupId>org.hibernate</groupId>
 9     <artifactId>hibernate-core</artifactId>
10     <version>4.3.0.Final</version>
11 </dependency>

 

 

3:创建实体类User

1 public class User {
2     private Long id;
3     private String title;
4     private Date date;
5 }

 

4:配置User.hbm.xml(文件放在工程根目录下)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6     <class name="com.zlt.hibernatedemo.User" table="user">
 7         <id name="id" column="id">
 8             <generator class="increment"></generator>
 9         </id>
10         
11         <property name="date" column="data" type="timestamp"></property>
12         <property name="title"></property>
13     </class>
14 
15 </hibernate-mapping>

 

5:配置hibernate.cfg.xml(文件放在工程根目录下)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 8         <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
 9         <property name="connection.username">root</property>
10         <property name="connection.password">root</property>
11         
12         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
13         <property name="show_sql">true</property>
14         <property name="hbm2ddl.auto">create</property>
15         
16         <mapping resource="User.hbm.xml"/>
17     </session-factory>
18 
19 </hibernate-configuration>

 

 

6:创建session工具类 HibernateFactory.java

 1 public class HibernateFactory {
 2     //sessionFactory是一个线程安全的全局对象,只需要创建一次
 3     public static final SessionFactory sessionFactory;
 4     
 5     static {
 6         try {
 7             sessionFactory = new Configuration().configure().buildSessionFactory();
 8         } catch (Throwable ex) {
 9             System.err.println("Initial SessionFactory creation failed." + ex);
10             throw new ExceptionInInitializerError(ex);
11         }
12     }
13     
14     public static final ThreadLocal session = new ThreadLocal();
15 
16     public static Session currentSession() throws HibernateException {
17         Session s = (Session) session.get();
18         // Open a new Session, if this thread has none yet
19         if (s == null) {
20             s = sessionFactory.openSession();
21             // Store it in the ThreadLocal variable
22             session.set(s);
23         }
24         return s;
25     }
26 
27     public static void closeSession() throws HibernateException {
28         Session s = (Session) session.get();
29         if (s != null)
30             s.close();
31         session.set(null);
32     }
33         
34 }

Configuration接口负责加载配置文件得到内部数据
Configuration config = new Configuration().configure("/hibernate.cfg.mxl"); //前面带"/"表示在src根目录下开始寻找
Configuration config = new Configuration().configure(); //自动搜寻hibernate.cfg.mxl

 

SessionFctory接口负责创建Session实例,Configuration实例config会根据配置信息构造SessionFactory实例并返回,因此config变更也不会影响到SessionFactory的实例,同样如果应用需要访问多个数据库,针对每个数据库,应分别对其创建相应的SessionFactory实例,SessionFactory保存了当前数据库配置的所有映射关系,同时也负责维护当前的二级数据缓存和Statement Pool,SessionFactory创建过程非常复杂而且线程安全,应用中针对一个数据库共享一个SessionFactory实例即可

SessionFactory sessionFactory = config.buildSessionFactory();

 

Session是hibernate持久化操作的基础类,提供了save,update,delete等方法
Session非线程安全,即一个Session实例同时只可由一个线程使用,一个Session实例的多线程并发调用会导致难以预知的错误
Session session = sessionFactory.openSession();

 

7:测试程序

 1 public class HibernateTest {
 2     public static void main(String[] args) {
 3         Session session = HibernateFactory.currentSession();
 4         Transaction tx = session.beginTransaction();
 5         User user = new User();
 6         user.setDate(new Date());
 7         user.setTitle("title");
 8         session.save(user);
 9         
10         tx.commit();
11         session.close();
12     }
13 }

 

 

8:结果

 

 

posted on 2014-06-30 21:39  幸福小弥  阅读(679)  评论(0编辑  收藏  举报