Spring3 整合 Hibernate4 - 注入SessionFactory

版本:

spring-framework-3.2.4.RELEASE
hibernate-release-4.2.5.Final
jdk1.7

要使用Spring3整合Hibernate4需要再添加以下包

1.----  spring-orm-3.2.4.RELEASE.jar
2.----  spring-dao-2.0.7.jar(Spring里没有提供,需要到网上下载)

在要用到hibernate的类中添加SessionFactory属性和相应的set方法

public class StudentDaoImpl implements IStudentDao {
//SessionFactory 在包org.hibernate内
private SessionFactory sessionFactory; public void save(Student student) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit(); System.out.println("save:"+student.getName());
} //为SessionFactory添加set方法,Spring才能为其注入
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} }

在配置文件中添加SessionFactory 的baen

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入Hibernate 配置文件路径,前面要加上 classpath:-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean> <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
<!-- 把sessionFactory 注入给studentDao -->
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean> <bean id="start" class="com.startspring.Start">
<property name="studentService" ref="studentService" />
</bean>
</beans>

------------------------------------------------------------------------------------------------------------

使用数据源(DataSource)

如果要使用数据源:

1.先把数据源相关的包导入到项目。

2.在Spring配置文件中添加数据源的bean。

3.把数据源 bean 注入到sessionFactory的dataSource属性。

(关于 Spring 注入数据源:http://www.cnblogs.com/likailan/p/3459776.html

Spring配置文件 如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 数据源 -->
<bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<property name="user" value="hib" />
<property name="password" value="hib" />
</bean> <!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入Hibernate 配置文件路径,前面要加上 classpath:-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<!-- 把数据源 bean 注入到sessionFactory的dataSource属性 -->
<property name="dataSource" ref="dataSource"/>
</bean> <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
<!-- 把sessionFactory 注入给studentDao -->
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean> <bean id="start" class="com.startspring.Start">
<property name="studentService" ref="studentService" />
</bean>
</beans>

现在数据库连接交由数据源来管理。可以把 hibernate.cfg.xml 文件中的connection.driver_class、connection.username等属性去掉了。

--------------------------------------------------------------------------------------------------------------

去掉Hibernate配置文件

通过注入的方式为SessionFactory注入hibernate的配置信息,从而省去hibernate配置文件

Spring配置文件 如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 数据源 -->
<bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<property name="user" value="hib" />
<property name="password" value="hib" />
</bean> <!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 把数据源 bean 注入到sessionFactory的dataSource属性 -->
<property name="dataSource" ref="dataSource"/> <!-- hibernate的基本配置,如:方言,是否显示sql等 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property> <!-- 引入映射文件,如果有多个可以继续添加 <value> -->
<property name="mappingLocations">
<list>
<value>com/startspring/entrty/Student.hbm.xml</value>
</list>
</property>
</bean> <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
<!-- 把sessionFactory 注入给studentDao -->
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean> <bean id="start" class="com.startspring.Start">
<property name="studentService" ref="studentService" />
</bean>
</beans>

现在可以把 hibernate.cfg.xml 文件删除了。

原来Spring的配置文件中<property name="configLocation" value="classpath:hibernate.cfg.xml"/>这行代码也可以删去了。

----------------------------------------------------------------------------------------------------------

Spring2.X版本整合Hibernate

Spring2.x整合Hibernate和Spring3.x版基本一致,但要注意以下几点:

1.  Spring2.x只支持Hibernate3 ,并不支持Hibernate4

2.  整合 Hibernate3 使用的SessionFactory Bean 是:org.springframework.orm.hibernate3包下的LocalSessionFactoryBean而不是org.springframework.orm.hibernate4.LocalSessionFactoryBean(Spring3整合hibernate3也是)

3.  导入包时注意删除相同的包!(优先删除版本低的)

Spring整合Hibernate 一 - 注入SessionFactory的更多相关文章

  1. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  2. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  3. Spring整合Hibernate详细步骤

    阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...

  4. SSH整合之spring整合hibernate

    SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...

  5. Spring第13篇—–Spring整合Hibernate之声明式事务管理

    不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...

  6. Spring整合Hibernate笔记

    1. Spring 指定datasource a) 参考文档,找dbcp.BasicDataSource i. c3p0 ii. dbcp <bean id="dataSource&q ...

  7. 【SSH框架】系列之 Spring 整合 Hibernate 框架

    1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...

  8. java框架之Spring(4)-Spring整合Hibernate和Struts2

    准备 导包 Struts2 导入 Struts2 zip 包解压目录下 'apps/struts-blank.war' 中所有 jar 包,如下: asm-3.3.jar asm-commons-3. ...

  9. Spring整合Hibernate(转)

    概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 整合步骤 整合前准 ...

随机推荐

  1. Button简单实例1

    1.XML按钮定义 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" an ...

  2. MVC自我学起之MVCMusic开发中遇到问题:musicstore edit方法出错的原因和解决方法

    错误提示: 存储区更新.插入或删除语句影响到了意外的行数(0).实体在加载后可能被修改或删除.刷新 ObjectStateManager 项. 解决案: 1.在view中或model中增加隐藏id 1 ...

  3. xcode 7 添加空模板

    文件下载链接: http://pan.baidu.com/s/1pKbyf4R 密码: ppi7 下载完成后将模板放入以下路径应用程序->Xcode->右键显示包内容->/Conte ...

  4. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  5. asp.net js 获取服务器控件值

    最近在弄js 但是设计到获取服务器控件的值,有点模糊.百度查了资料都达不到效果.现在看看这个,希望朋友们能用上!! <asp:Label ID="lbDepartName" ...

  6. 优化DOM交互

    DOM操作与交互要消耗大量时间,所以优化DOM交互有重大意义. 1.最小化现场更新 如果需要访问的DOM部分是已经显示的页面的一部分,那么这就是在进行一个现场更新.现场更新得越多,代码完成执行所需要的 ...

  7. rub、sass和compass的安装

    长话短说,直接进入正题. [ruby安装] ruby下载网址:http://rubyinstaller.org/downloads/ 选择适合自己电脑的版本: 下载完成后直接双击安装,记得勾选加入环境 ...

  8. Stripies(POJ 1862 贪心)

    Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14151   Accepted: 6628 Descrip ...

  9. 使用ARM模板部署自动扩展的Linux VMSS(1)

    在Azure之前的ASM版本或者经典模式中,用户使用Azure Website,Azure Cloud Service等PAAS服务,可以实现一定程度上的自动扩展(auto scaling),但有着诸 ...

  10. ckeditor详解

    源网页编辑软件FCKEditor在09年发布更新到3.0,并改名为CKEditor.改进后的ckeditor更加模块话,配置更加灵活,和以前的fckeditor使用方式上也有所不同.在我的mvc项目中 ...