使用hibernate 做应用持久层

在尝试了无数次的Entity bean 的配置失败后,我决定放弃ejb的繁琐配置,用hibernate来替代之。

学习hibernate 并不是很难,应该说比较容易就上手了,看了一下hibernate reference 以及hibernate in action 就动手写了一个可持久化的类。

要用hibernate 实现对象的持久化需要做下面几件事情:
1.  编写实体类 Person.java
     hibernate 的诱人之处就在于 对要持久化的类没有任何特殊的要求或者说是写法格式和类继承结构上    的要求。一般的javabean 风格的类就可以用hebernate 持久化了,甚至不用实现Seriazable 接口。

package hi;

public class Person
{
 private String id;
 private String name;
 private int age;
 private String addr;


 public String getAddr() {
  return addr;
 }

 public void setAddr(String addr) {
  this.addr = addr;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }
}

2.  编写该类对应的hibernate 配置文件 person.hbm.xml
     这个配置文件的作用是告诉hibernate 那个类需要持久化,以及这个类中哪些属性域对应到数据库中的字段是什么,类型又是什么。hibernate 的另一个强大的地方就是 在数据库中甚至不用自己手工创建table,hibernate 能通过person.hbm.xml 来自动创建(下文中红色的代码的工能就是自动create table)。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
 
 
<hibernate-mapping>
 <class name = "hi.Person" table="hiperson">
  <id name = "id" type = "string" unsaved-value = "null">
   <column name = "ID" sql-type = "char(16)" not-null = "true"></column>
   <generator class = "uuid.hex"></generator>
  </id>
  
  <property name = "name">
   <column name = "name"  length = "16" not-null = "true"></column>
   
  </property>
  
  <property name = "age" column="age"></property>
  <property name = "addr" column="addr"></property>
 </class>
 
</hibernate-mapping>

3. 编写hibernate 与使用数据库的配置文件  hibernate.cfg.xml
    这个配置文件的作用是告诉hibernate 怎么与数据库相连,以及哪些类持久化到这个数据库中。
    <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
 <session-factory name="personHi">
  <property name="show_sql">true</property>
  <property name="dialect">
   net.sf.hibernate.dialect.MySQLDialect
  </property>
  <property name="connection.driver_class">
   com.mysql.jdbc.Driver
  </property>
  <property name = "connection.url">
   jdbc:mysql://localhost:3306/hi
  </property>
  
  <property name="connection.username">
   root
  </property>
  
  <property name = "connection.password"></property>
  
  <mapping resource="person.hbm.xml"/>
  
 </session-factory>
</hibernate-configuration>

至此对于person 持久化的基础工作完成了,下面就是写使用person 这个类的test 来看看hibernate的功效了为了方便以后测试方便,我们抽象一个hibernate 的工具类 HibernateUtil.java 它的主要作用就是 维护一个Session ,并且有静态的方法来得到,关闭这个session,省得每个每次测试的时候到要写。

package hi;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.tool.hbm2ddl.SchemaUpdate;

public class HibernateUtil
{
 private static final SessionFactory sessionFactory;
 
 static
 {
  try
  {
   Configuration config = new Configuration();
   sessionFactory = config.configure().buildSessionFactory();
   new SchemaUpdate(config).execute(true, true);
   
  }catch(Throwable ex)
  {
   ex.printStackTrace();
   throw new ExceptionInInitializerError(ex);
  }
 }
 
 public static final ThreadLocal session = new ThreadLocal();
 
 public static Session currentSesion()throws HibernateException
 {
  Session s = (Session) session.get();
  
  if(s==null)
  {
   s = sessionFactory.openSession();
   session.set(s);
  }
  return s;
 }
 public static void closeSession()throws HibernateException
 {
  Session s = (Session) session.get();
  session.set(null);
  if(s!=null)
   s.close();
 }
 
}

下面就是test类了:

package hi;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

import hi.HibernateUtil;
import hi.Person;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;


public class Test 
{

 public static void main(String[] args)
 {
  try
  {
   Session session = HibernateUtil.currentSesion();

   Transaction tx = session.beginTransaction();
   Person person1 = new Person();
   person1.setAge(20);
   person1.setName("apple1");
   
   session.save(person1);
   tx.commit();
   
   HibernateUtil.closeSession();
  }catch (HibernateException e)
  {
   e.printStackTrace();
  }
 }
}

理论上到此就可以运行Test了,但是可能心中还会有疑问,使用hibernate的话需要什么jar 来配置环境能。
答案很简单,而且在hibernate reference 里都列的很清楚了,但是有一点就是除了reference 中列出的需要的jar之外,还要把j2ee.jar 放到classpath下。
hibernate reference 下载地址:www.hibernate.org/5.html



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=350861

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值