菜鸟学习Hibernate——简单的一个例子

来源:互联网 发布:塔夫斯大学 知乎 编辑:程序博客网 时间:2024/04/28 08:46

一、Hibernate开发。

上篇博客已经为大家介绍了持久层框架的发展流程,持久层框架的种类。

为了能够使用Hibernate快速上手,我们先讲解一个简单的Hibernate应用实例hibernate_first

二、开发流程。

1.首先在MyEclipce中新建一个hibernate_first的项目,然后新建后的项目目录为:


2.配置Hibernate环境。

3.编写持久化类User.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.bjpowernode.hibernate;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class User {  
  6.   
  7.     private String id;  
  8.       
  9.     private String name;  
  10.       
  11.     private String password;  
  12.       
  13.     private Date createTime;  
  14.       
  15.     private Date expireTime;  
  16.   
  17.     public String getId() {  
  18.         return id;  
  19.     }  
  20.   
  21.     public void setId(String id) {  
  22.         this.id = id;  
  23.     }  
  24.   
  25.     public String getName() {  
  26.         return name;  
  27.     }  
  28.   
  29.     public void setName(String name) {  
  30.         this.name = name;  
  31.     }  
  32.   
  33.     public String getPassword() {  
  34.         return password;  
  35.     }  
  36.   
  37.     public void setPassword(String password) {  
  38.         this.password = password;  
  39.     }  
  40.   
  41.     public Date getCreateTime() {  
  42.         return createTime;  
  43.     }  
  44.   
  45.     public void setCreateTime(Date createTime) {  
  46.         this.createTime = createTime;  
  47.     }  
  48.   
  49.     public Date getExpireTime() {  
  50.         return expireTime;  
  51.     }  
  52.   
  53.     public void setExpireTime(Date expireTime) {  
  54.         this.expireTime = expireTime;  
  55.     }  
  56. }  


4.编写生成映射文件User.hbm.xml。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0"?>  
  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.bjpowernode.hibernate.User">  
  7.         <id name="id">  
  8.             <generator class="uuid"/>  
  9.         </id>  
  10.         <property name="name"/>  
  11.         <property name="password"/>  
  12.         <property name="createTime"/>  
  13.         <property name="expireTime"/>  
  14.     </class>  
  15. </hibernate-mapping>  


5.编写hibernate.cfg.xml文件。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE hibernate-configuration PUBLIC  
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4.   
  5. <hibernate-configuration>  
  6.     <session-factory >  
  7.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  8. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_frist</property>  
  9. <property name="hibernate.connection.username">root</property>  
  10. <property name="hibernate.connection.password">root</property>  
  11. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  12. <property name="hibernate.show_sql">true</property>  
  13. <property name="hibernate.format_sql">true</property>  
  14.     <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>  
  15.     </session-factory>  
  16. </hibernate-configuration>  

6.生成表的类ExportDB.java。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.bjpowernode.hibernate;  
  2.   
  3. import org.hibernate.cfg.Configuration;  
  4. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  5.   
  6. /** 
  7.  * 将hbm生成ddl 
  8.  * @author Administrator 
  9.  * 
  10.  */  
  11. public class ExportDB {  
  12.   
  13.     public static void main(String[] args) {  
  14.           
  15.         //默认读取hibernate.cfg.xml文件  
  16.         Configuration cfg = new Configuration().configure();  
  17.           
  18.         SchemaExport export = new SchemaExport(cfg);  
  19.         export.create(truetrue);  
  20.     }  
  21. }  


7.以上六个步骤已经把表建起来了下面我们就保存个数据,新建一个Client.java类来存入一个数据。代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.bjpowernode.hibernate;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.cfg.Configuration;  
  8.   
  9. public class Client {  
  10.   
  11.     public static void main(String[] args) {  
  12.           
  13.         //读取hibernate.cfg.xml文件  
  14.         Configuration cfg = new Configuration().configure();  
  15.           
  16.         //建立SessionFactory  
  17.         SessionFactory factory = cfg.buildSessionFactory();  
  18.           
  19.         //取得session  
  20.         Session session = null;  
  21.         try {  
  22.             session = factory.openSession();  
  23.             //开启事务  
  24.             session.beginTransaction();  
  25.             User user = new User();  
  26.             user.setName("张三");  
  27.             user.setPassword("123");  
  28.             user.setCreateTime(new Date());  
  29.             user.setExpireTime(new Date());  
  30.               
  31.             //保存User对象  
  32.             session.save(user);  
  33.               
  34.             //提交事务  
  35.             session.getTransaction().commit();  
  36.         }catch(Exception e) {  
  37.             e.printStackTrace();  
  38.             //回滚事务  
  39.             session.getTransaction().rollback();  
  40.         }finally {  
  41.             if (session != null) {  
  42.                 if (session.isOpen()) {  
  43.                     //关闭session  
  44.                     session.close();  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49. }  
0 0
原创粉丝点击