`
icarusliu
  • 浏览: 232999 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

在spring当中启用hibernate

阅读更多

有两种方式:

1. 在springContext.xml当中配置sessionFactory,然后就可以将这个SessionFactory注入到其它要使用hibernate的DAO当中去。

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
	
	<bean id="test" class="TestDAO">
		<property name="sessionFactory">
			<ref local="sessionFactory"/>
		</property>
	</bean>

  这样,在TestDAO这个类当中就可以直接用SessionFactory来操作数据库了。

 

2. 不在xml文件当中进行配置,生成单独的一个类来通过hibernate.cfg.xml文件的位置来取得sessionFactory。这种方式其实就是上一种方式自己来实现。

下面是MyEclipse自动生成的一个sessionFactory的管理类,用户也可以自行实现。

package com.unews.hibernate.data;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * 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 String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

	static {
    	try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} 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(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} 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 session factory
     *
     *	session factory will be rebuilded in the next call
     */
	public static void setConfigFile(String configFile) {
		HibernateSessionFactory.configFile = configFile;
		sessionFactory = null;
	}

	/**
     *  return hibernate configuration
     *
     */
	public static Configuration getConfiguration() {
		return configuration;
	}

}

 

分享到:
评论

相关推荐

    Spring 整合 Hibernate 时启用二级缓存实例详解

    Spring 整合 Hibernate 时启用二级缓存实例详解 写在前面:  1. 本例使用 Hibernate3 + Spring3;  2. 本例的查询使用了 HibernateTemplate; 1. 导入 ehcache-x.x.x.jar 包; 2. 在 applicationContext.xml ...

    Struts+spring+hibernatejar包

    整合了Struts2+spring3.1+hibernate+mysql+dom4j+jstl的jar包 能自己放入项目启用 具体操作右键项目名-builtpath-libraries-addjars即可

    maven3.05+springmvc3.2+spring3.2+hibernate3.6重写传智播客OA源代码(包含数据库)

    maven3.05+springmvc3.2+spring3.2+hibernate3.6重写传智播客OA源代码。菜单部分采用ztree.界面有一部分使用bootstrap3.11。听了汤阳光大神的视频收获很大,也学习springmvc,期间有一个想法用springmvc来改写汤大神...

    spring3.2+strut2+hibernate4

    -- 启用spring注解支持 --&gt; &lt;!-- &lt;!-- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;--&gt; &lt;!-- &lt;!-- value="classpath:hibernate.cfg.xml"&gt;--&gt; &lt;!-- &lt;/property&gt;--&gt; &lt;!-- ...

    Customer-Relationship-Management:Spring MVC,Hibernate

    #依赖关系Spring Hibernate C3P0 #Spring和Hibernate ## 1的配置。 使用c3p0 ## 2定义数据库数据源和连接池。 设置Hibernate会话工厂## 3。 设置Hibernate事务管理器## 4。 启用事务注释的配置

    spring-boot-rest-commons:该库包含开箱即用的模块,用于启用典型 spring-boot REST 服务器应用程序所需的一些通用跨功能特性

    Spring Boot REST 共享该库包含开箱即用的模块,用于启用典型 spring-boot REST 服务器应用程序所需的一些通用跨功能特性:审计、请求/响应日志记录、安全性和 API 错误映射。 这些是为每个主题提供的单独文档: :...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring攻略(第二版 中文高清版).part1

    3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案 115 3.2.3 工作原理 116 3.3 访问...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring攻略(第二版 中文高清版).part2

    3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案 115 3.2.3 工作原理 116 3.3 访问...

    hibernate-envers-demo:使用Hibernate Envers,Spring Boot和AngularJS的简短演示

    Hibernate启用演示问题陈述这是一个使用Hibernate Envers,Spring boot和AngularJS的演示项目。 它旨在作为称为“四眼原理”( )的实际用例的解决方案。 此用例的主要参与者是“制造者”和“检查者”。 第一个将对...

    company-structure-spring-security-oauth2-authorities:示例Spring Boot + Spring Security + OAuth2项目用于演示

    示例Spring Boot + Hibernate + Spring Security + OAuth2项目用于演示。 入门 先决条件: Java 8 玛文 H2 / PostgreSQL 可以在两个配置文件之一中运行应用程序: 22 Postgres 取决于选择进行测试的数据库引擎...

    Java+ssh网上银行交易系统.zip

    netBank-System ...SSH2(Struts2-Spring3-Hibernate4) 系统分析 需求分析 普通用户 普通用户可以执行存款、取款、转账、查看交易记录、查看个人信息、修改个人信息、修改密码和注销等功能。用例图如下图所示。

    springboot-multiple-datasource

    已启用,以与使用Spring Boot设置RDB相同的设置运行。由于我实际上使用了Spring Boot的AutoConfigure设置类的机制,因此我能够使用HikariCP启动连接池,并通过像标准设置一样编写属性文件来进行连接。 (在示例中,...

    SSH的jar包.rar

    SSH(struts+spring+hibernate)的jar包 SSH 通常指的是 Struts2 做前端控制器,Spring 管理各层的组件,Hibernate 负责持久化层。 一个请求在Struts2框架中的处理大概分为以下几个步骤: 1、客户端初始化一个指向...

    springboot参考指南

    在Spring环境中使用YAML暴露属性 iii. 23.6.3. Multi-profile YAML文档 iv. 23.6.4. YAML缺点 vii. 23.7. 类型安全的配置属性 i. 23.7.1. 第三方配置 ii. 23.7.2. 松散的绑定(Relaxed binding) iii. 23.7.3. @...

Global site tag (gtag.js) - Google Analytics