package com.ssh.domain;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static ServiceRegistry serviceRegistry;
static {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
整合SSH并实现登录注册分页功能
共48个文件
xml:9个
class:9个
java:9个
4星 · 超过85%的资源 需积分: 9 36 下载量 59 浏览量
2014-01-10
11:25:33
上传
评论
收藏 1.63MB RAR 举报
温馨提示
基于myeclipse2014整合了 Struts2.1+Spring3.1.1+Hibernate4.1.4,并实现登录,注册和分页功能 ,欢迎下载!并包含所需的Mysql数据库脚本文件
资源推荐
资源详情
资源评论
收起资源包目录
SSH.rar (48个子文件)
SSH
.springBeans 437B
.myhibernatedata 442B
.settings
com.genuitec.runtime.libraries.xml 108B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 563B
org.eclipse.wst.common.component 468B
org.eclipse.wst.common.project.facet.core.prefs.xml 647B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 522B
org.eclipse.jdt.core.prefs 364B
src
com
studentsystem
TestHibernateDaoSupport
TestHibernateDaoSupport.java 2KB
ssh
dao
UserDao.java 253B
impl
StudentDaoImpl.java 2KB
action
toOk.java 2KB
ProLogin.java 2KB
ProRegister.java 920B
domain
User.hbm.xml 800B
User.java 870B
HibernateSessionFactory.java 3KB
Page.java 492B
struts.xml 1015B
.project 2KB
WebRoot
err.jsp 260B
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 872B
classes
com
studentsystem
TestHibernateDaoSupport
TestHibernateDaoSupport.class 2KB
ssh
dao
UserDao.class 394B
impl
StudentDaoImpl.class 3KB
action
toOk.class 3KB
ProRegister.class 1KB
ProLogin.class 3KB
domain
User.hbm.xml 800B
User.class 1KB
Page.class 860B
HibernateSessionFactory.class 3KB
struts.xml 1015B
lib
hibernate-ehcache-4.1.0.Final.jar 134KB
mysql-connector-java-5.1.6-bin.jar 687KB
ehcache-core-2.4.3.jar 983KB
spring.tld 17KB
spring-form.tld 74KB
applicationContext.xml 4KB
ok.jsp 2KB
login.jsp 1KB
index.jsp 860B
register.jsp 1KB
user.sql 2KB
.classpath 2KB
共 48 条
- 1
资源评论
- tzh40352014-05-28还可以吧,就是熟悉下配置文件
- tanjian12342014-02-13代码比较简单 没我想要的
- gavial20122014-02-12变异的时候报错,但是也可能是因为我的包和没有配置对,希望其他下载的同学讲讲配置
赵侠客
- 粉丝: 339
- 资源: 57
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功