首页 > 代码库 > Hibernate系列(二):简单编写HibernateUtil类来优化性能

Hibernate系列(二):简单编写HibernateUtil类来优化性能

相对于Hibernate系列(一)中的代码编写HibernateUtil类以提高程序的运行速度

首先,仍然要写一个javabean(User.java):

package cn.itcast.hibernate.domain;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	
}


然后,在当前包下添加一个映射文件:User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
	package="cn.itcast.hibernate.domain">

	<class name="User" table="tb_User">
		<id name="id">
			<generator class="native"/> 
		</id>
		<property name="name" />
		<property name="birthday" />
	</class>
	
</hibernate-mapping>

之后,我们要配置Hibernate.cfg.cml,此文件直接放在src目录下边:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/test
		</property>
		<property name="connection.username">sa</property>
		<property name="connection.password">123456</property>
		
		<property name="hbm2ddl.auto">create</property>
		<property name="show_sql">true</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>

接下来,我们就要编写HibernateUtil.java类了,这个类主要是为了获得Session。

因为,在我们每次在代码中获得Session是非常耗时的,我们希望它仅仅做一次,因此我们要做一个工具类去初始化Hibernate。而且因为工具类一般不希望能够被继承,使得使用者能够改变工具类的东西,所以一般我们会采用final关键字。

package cn.itcast.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public final class HibernateUtil {
	private static SessionFactory sessionFactory;
	
	private HibernateUtil(){
	}
	
	static{
		Configuration cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	public static Session getSession(){
		return sessionFactory.openSession();
	}
}

然后,我们编写一个简单的测试类,添加一条数据Add.java:

package cn.itcast.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.itcast.hibernate.domain.User;

public class Add {
	public static void main(String[] args){
		Session s = HibernateUtil.getSession(); //这里直接调用HibernateUtil工具类中的getSession()方法获得Session
		Transaction tx = s.beginTransaction(); //开启事务
		User user = new User();
		user.setName("zhangsan");
		user.setBirthday(new Date());
		
		s.save(user); //保存对象
		tx.commit();//提交事务
		s.close();//关闭Session
	}
}
如此,一个简单的获取Session来提高程序的性能的HibernateUtil类就编写完毕了。

Hibernate系列(二):简单编写HibernateUtil类来优化性能