一、什么是SessionFactory
SessionFactory接口负责初始化Hibernate。它充当数据存储源的代理,并负责创建Session对象。这里用到了工厂模式。需要注意的是SessionFactory并不是轻量级的,因为一般情况下,一个项目通常只需要一个SessionFactory就够,当需要操作多个数据库时,可以为每个数据库指定一个SessionFactory。----百度百科
一般来讲,一个web工程(系统),如果涉及到的是一个数据库(你在学校做项目,一般就一个吧。。。哈哈哈),那么本工程就只需要一个sessionfactory就够了。这个工程所涉及到的所有的session,都交由这个sessionfactory来管理。
二、什么是单例模式
单例模式(Singleton),也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息。这种方式简化了在复杂环境下的配置管理。
三、代码实现
之前的hibernate的配置、实体类、映射文件和数据库;已经给我直接上代码
1.单例模式类
package com.util;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static final ThreadLocal threadLocal = new ThreadLocal();
public static Session currentSession() throws HibernateException{
Session session = (Session)threadLocal.get();
if(session == null){
if(sessionFactory == null){
try{
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}catch(HibernateException e){
System.out.println("Error Creating SessionFacotry.");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void cloaseSession() throws HibernateException{
Session session = (Session)threadLocal.get();
threadLocal.set(null);
if(session != null){
session.close();
}
}
}
2.测试类
package com.test;import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.util.HibernateSessionFactory;
import com.util.HibernateUtil;
import com.vo.Person;
public class TestSessionFactory {
private static Transaction transaction;
@Test
public void insertTest() {
Session session = HibernateSessionFactory.currentSession();
transaction = session.beginTransaction();
String person_id = "1514010624";
String person_name = "谢星";
Person person = new Person(person_id, person_name);
session.save(person);
transaction.commit();
HibernateSessionFactory.cloaseSession();
}
@Test
public void selectTest() {
Session session = HibernateSessionFactory.currentSession();
Person person = (Person) session.get(Person.class, "1514010624");
System.out.println("id:" + person.getPerson_id() + ",name:" + person.getPerson_name());
HibernateSessionFactory.cloaseSession();
}
@Test
public void updateTest() {
Session session = HibernateSessionFactory.currentSession();
transaction = session.beginTransaction();
String person_id = "1514010624";
String person_name = "谢魁星";
Person person = new Person(person_id, person_name);
session.update(person);
transaction.commit();
HibernateSessionFactory.cloaseSession();
}
@Test
public void deleteTest() {
Session session = HibernateSessionFactory.currentSession();
transaction = session.beginTransaction();
String person_id = "1514010624";
String person_name = "谢魁星";
Person person = new Person(person_id, person_name);
session.delete(person);
transaction.commit();
HibernateSessionFactory.cloaseSession();
}
}