Hibernate的一个开发工具(OpenSession()和getCurrentSession())
来源:互联网 发布:驾驭大数据 百度云 编辑:程序博客网 时间:2024/06/05 23:50
在开发Hibernate项目时用到的一个帮助类来获取链接数据库的Session
HibernateUtil.java
package com.hsp.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
final public class HibernateUtil {
private static SessionFactory sessionFactory=null;
//使用线程局部模式
//线程和变量相关联
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
private HibernateUtil(){};
static {
sessionFactory=new Configuration().configure().buildSessionFactory();
}
//获取全新的全新的sesession
public static Session openSession(){
return sessionFactory.openSession();
}
//获取和线程关联的session
public static Session getCurrentSession(){
Session session=threadLocal.get();
//判断是否得到
if(session==null){
session=sessionFactory.openSession();
//把session对象设置到 threadLocal,相当于该session已经和线程绑定
threadLocal.set(session);
}
return session;
}
}
- Hibernate的一个开发工具(OpenSession()和getCurrentSession())
- hibernate openSession和getCurrentSession
- hibernate opensession()和getCurrentSession()
- hibernate 的openSession和getCurrentSession的区别
- hibernate 的 openSession 和 getCurrentSession 的实现
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- hibernate的session判断-openSession和getCurrentSession
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate: openSession() 和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别 .
- Hibernate的openSession和getCurrentSession区别
- hibernate中 openSession和getCurrentSession的区别
- Struts2 文件上传 之 文件类型 allowedTypes
- 排序——快速排序
- SELECT语句逻辑执行顺序,你知道吗?
- 黑马程序员_C语言的一些总结
- 协同OA2014的狂奔与驻守
- Hibernate的一个开发工具(OpenSession()和getCurrentSession())
- c++ string类的一些注意
- Windows窗口相关的一些概念解释
- freeman链码
- LAMP(Linux+Apache+Mysql+PHP)环境搭建
- 如何增加自己简历的亮点
- [LeetCode] 4Sum
- git常用命令
- win7 自带的端口转发功能开启