Spring整合Hibernate 一 - 注入SessionFactory
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的更多相关文章
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- Spring整合Hibernate详细步骤
阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...
- SSH整合之spring整合hibernate
SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...
- Spring第13篇—–Spring整合Hibernate之声明式事务管理
不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...
- Spring整合Hibernate笔记
1. Spring 指定datasource a) 参考文档,找dbcp.BasicDataSource i. c3p0 ii. dbcp <bean id="dataSource&q ...
- 【SSH框架】系列之 Spring 整合 Hibernate 框架
1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...
- java框架之Spring(4)-Spring整合Hibernate和Struts2
准备 导包 Struts2 导入 Struts2 zip 包解压目录下 'apps/struts-blank.war' 中所有 jar 包,如下: asm-3.3.jar asm-commons-3. ...
- Spring整合Hibernate(转)
概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 整合步骤 整合前准 ...
随机推荐
- jquery插件datepicker
jQuery UI很强大,其中的日期选择插件Datepicker是一个配置灵活的插件,我们可以自定义其展示方式,包括日期格式.语言.限制选择日期范围.添加相关按钮以及其它导航等. <script ...
- Android的ADB工具使用
在SDK的Tools文件夹下包含着Android模拟器操作的重要命令ADB,ADB的全称为Android Debug Bridge,就是调试桥的作用,借助这个工具,我们可以管理设备或手机模拟器的状态 ...
- 一些不熟悉的SQL脚本--约束条件
1.根据表名查询主键的SQL语句 SELECT D.COLUMN_NAME AS COLNAME FROM USER_CONS_COLUMNS D, USER_CONSTRAINTS M WHERE ...
- c3p0xml配置详解
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-con ...
- Spark配置&启动脚本分析
本文档基于Spark2.0,对spark启动脚本进行分析. date:2016/8/3 author:wangxl Spark配置&启动脚本分析 我们主要关注3类文件,配置文件,启动脚本文件以 ...
- C++程序设计实践指导1.14字符串交叉插入改写要求实现
改写要求:1:以指针为数据结构开辟存储空间 改写要求2:被插入字符串和插入字符串不等长,设计程序间隔插入 如被插入字符串长度为12,待插入字符串长度为5 则插入间隔为2 改写要求3:添加函数Inser ...
- 加速ssh连接
今天aws大姨妈了,也不知道是aws问题还是gfw的问题,反正我都已经问候了你们zzsbd!!! ping aws的丢包都是75%以上,这还玩个雕啊,果断去找加速的教程来看,但是发现cygwin下并没 ...
- 解决Webservice内存溢出-用XmlWriter
XmlWriter 表示一个编写器,该编写器提供一种快速.非缓存和只进的方式来生成包含 XML 数据的流或文件.这个就可以不占用内存,将数据放入磁盘中.也就不会出现内存溢出 public class ...
- Android中动态更新TextView上的文字
示例代码: 1.新线程,定时更新文字 class testThread extends Thread{ public void run() { Message message = new Messag ...
- TEA加密
TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,支持128位密码,与BlowFish一样TEA每次只能加密/解密8字节数据.TEA特点是速度快.效率高,实现也 ...