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. Linq to Entities中无法构造实体或复杂类型

    EF中在使用linq就行查询select时不能直接使用自动映射生成的类,需要在单独声明一个类或者使用匿名类在查询完成后再转为对应的对象. public partial class WebForm1 : ...

  2. SQL Server数据库的操作流程和连接的简单介绍

    学习ADO,免不了要跟数据库打交道,对于初学者来说,如果不整理一下整个流程,那么可能会出现很多的问题,下面简单的介绍数据库的操作流程. 1.     我们最终操作的对像是数据表,在操作数据表之前我们先 ...

  3. jquery动态删除、复制、包裹DOM节点

    1.remove()方法 移除满足条件的元素 <html> <head> <meta http-equiv="Content-Type" conten ...

  4. poj1111 DFS

    J - 搜索 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit I ...

  5. (转) Data structures

      Data structures A data structure is a group of data elements grouped together under one name. Thes ...

  6. 读《疯狂Ajax讲义》重点

    1.XMLHttpRequest() 请求的写法(p62) 一个类XMLHttp 因该包含的接口:[1]  XMLHttp.sendRequest("POST",URL,data, ...

  7. (原)mkl用到的函数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5585301.html 计算 $C=\alpha *A*B+\beta *C$: void cblas_ ...

  8. git rm

    git rm可以执行删除的条件(前提是要被删除的文件还没有从workspace tree中移除) git rm可以执行删除的条件:被删除文件在index当中的blob内容必须与HEAD commit指 ...

  9. 【mysql】1206 SQLSTATE: HY000 (ER_LOCK_TABLE_FULL) 问题

    最近在做一个项目,其中一需求是:部分数据库中的数据需要定时删除掉(满足一定条件,比如7天前的数据都不保留) 最初的执行方法: 使用Quartz定时执行数据库操作,进行数据删除,数据库操作使用delet ...

  10. iOS延时执行的四种方法

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...