开发流程:
1.创建一个项目,项目结构如图所示:

2.引入hibernate开发包,所需要的jar包如图黑色框框所示,这是精简过后的jar包。

3.在数据库中创建表:employee,这里以oralce数据库为例。

  1. 创建employe 表.
  2. create table employee(
  3. id number primary key,
  4. name varchar2(64) not null,
  5. email varchar2(64) not null,
  6. hiredate date not null)
创建一个序列,将来用于主键的自动增长.

  1. --创建一个序列
  2. create sequence emp_seq
  3. start with 1
  4. increment by 1
  5. minvalue 1
  6. nomaxvalue
  7. nocycle
  8. nocache

4.开发domain对象和对象关系映射文件

对象关系映射文件: 作用是用于指定 domain对象和表的映射关系. ,该文件的取名有规范:domain对象.hbm.xml,一般我们放在 和domain对象同一个文件夹下(包下)

点击(此处)折叠或打开

  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. <!-- package : 表示该类在哪个包下 -->
  6. <hibernate-mapping package="com.shizongger.domain">
  7.     <!-- name : 表示类名 table 表示 该类和哪个表映射 -->
  8.     <class name="Employee" table="employee">
  9.         <id name="id" type="java.lang.Integer">
  10.             <!-- 主键生成策略 -->
  11.             <generator class="sequence">
  12.                 <param name="sequence">emp_sq</param>
  13.             </generator>
  14.         </id>
  15.         <property name="name" type="java.lang.String">
  16.             <column name="name" not-null="true" />
  17.         </property>
  18.         <property name="email" type="java.lang.String">
  19.             <column name="email" not-null="true"/>
  20.         </property>
  21.         <property name="hiredate" type="java.util.Date">
  22.             <column name="hiredate" not-null="true"></column>
  23.         </property>
  24.     </class>
  25. </hibernate-mapping>

5.手动配置我们的hibernate.cfg.xml文件,该文件用于配置 连接的数据库的类型,driver,,用户名,密码 ,url ....同时管理 对象关系映射文件 ,该文件的名称,我们一般不修改.

点击(此处)折叠或打开

  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. <!-- 该文件用于配置连接数据的种类,用户名,密码,ul ,驱动.. 连接池,二级缓存.. 有点类似strus struts-config.xml -->
  6. <hibernate-configuration>
  7.     <session-factory>
  8.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  9.         <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
  10.         <property name="connection.username">scott</property>
  11.         <property name="connection.password">zhang5476499</property>
  12.         <property name="dialect">org.hibernate.dialect.OracleDialect</property>
  13.         <property name="show_sql">true</property>
  14.         <mapping resource="com/shizongger/domain/Employee.hbm.xml" />
  15.     </session-factory>
  16. </hibernate-configuration>

pojo类如下:

点击(此处)折叠或打开

  1. package com.shizongger.domain;

  2. import java.io.Serializable;
  3. import java.util.Date;

  4. public class Employee implements Serializable {
  5.     private static final long serialVersionUID = -1L;

  6.     private Integer id;
  7.     private String name;
  8.     private String email;
  9.     private Date hiredate;

  10.     public Integer getId() {
  11.         return id;
  12.     }
  13.     public void setId(Integer id) {
  14.         this.id = id;
  15.     }
  16.     public String getName() {
  17.         return name;
  18.     }
  19.     public void setName(String name) {
  20.         this.name = name;
  21.     }
  22.     public String getEmail() {
  23.         return email;
  24.     }
  25.     public void setEmail(String email) {
  26.         this.email = email;
  27.     }
  28.     public Date getHiredate() {
  29.         return hiredate;
  30.     }
  31.     public void setHiredate(Date hiredate) {
  32.         this.hiredate = hiredate;
  33.     }
  34. }
这里pojo类要实现序列化.

测试类,这是一个main方法,相当于mvc中的view层。

点击(此处)折叠或打开

  1. package com.shizongger.view;

  2. import java.util.Date;

  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.Transaction;
  6. import org.hibernate.cfg.Configuration;
  7. import com.shizongger.domain.Employee;

  8. public class TestHibernate {

  9.     public static void main(String[] args) {
  10. // addEmployee();
  11. // updateEmp();
  12.         delEmp();
  13.     }

  14.     //添加一个雇员
  15.     private static void addEmployee() {
  16.         //1.得到Configuration
  17.         Configuration configuration = new Configuration().configure();
  18.         //2.得到SessionFactory(会话工厂,这是一个重量级的类,因此要保证在一个应用程序中只能有一个)
  19.         SessionFactory sessionFactory = configuration.buildSessionFactory();
  20.         //3.从sessionFactory中取出一个Session对象(它表示和数据库的一次会话)
  21.         Session session = sessionFactory.openSession();
  22.         //4.开始一个事务
  23.         Transaction transaction = session.beginTransaction();
  24.         //保存一个对象到数据库(持久化一个对象)
  25.         Employee emp = new Employee();
  26.         emp.setEmail("[email protected]");
  27.         emp.setHiredate(new Date());
  28.         emp.setName("lily");
  29.         //这里不必给出id值,因为它是自增的
  30.         session.save(emp);
  31.         transaction.commit();
  32.         session.close();
  33.     }

  34.     //更新用户
  35.     private static void updateEmp() {
  36.         Configuration configuration = new Configuration().configure();
  37.         SessionFactory sessionFactory = configuration.buildSessionFactory();
  38.         Session session = sessionFactory.openSession();
  39.         Transaction transaction = session.beginTransaction();
  40.         Employee emp = (Employee)session.load(Employee.class, 6);
  41.         emp.setName("shizongger");
  42.         emp.setEmail("[email protected]");
  43.         transaction.commit();
  44.         session.close();
  45.     }

  46.     //删除用户
  47.     private static void delEmp() {
  48.         Configuration configuration = new Configuration().configure();
  49.         SessionFactory sessionFactory = configuration.buildSessionFactory();
  50.         Session session = sessionFactory.openSession();
  51.         Transaction ts = session.beginTransaction();
  52.         Employee emp = (Employee)session.load(Employee.class, 6);
  53.         session.delete(emp);
  54.         ts.commit();
  55.         session.close();
  56.     }
  57. }
对于重量级的类SessionFactroy,我们只确保只能有一个,所以在工具类中获取一次sessionFacotrory.

点击(此处)折叠或打开

  1. package com.shizongger.utils;

  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.cfg.Configuration;

  4. final public class MySessionFactory {
  5.     private static SessionFactory sessionFactory = null;
  6.     private static Configuration configuration = null;

  7.     static{
  8.         configuration = new Configuration().configure();
  9.         sessionFactory = configuration.buildSessionFactory();
  10.     }

  11.     private MySessionFactory() {
  12.     }

  13.     public static SessionFactory getSessionFactory() {
  14.         return sessionFactory;
  15.     }
  16. }

注:本博文同时在我的csdn博客更新,博文链接在: http://blog.csdn.net/zhang5476499/article/details/52420347
并在原博客中已经著名在我的chinaunix博客同时更新的说明,希望能够推挤到chinaunix首页。


09-03 12:08