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的声明式事务 整合步骤 整合前准 ...
随机推荐
- Java学习——接口Interface
接口: 初期理解可以认为是一个特殊的抽象类 当抽象类中的方法都是抽象的,那么该类可以通过接口的形式来表示.class用于定义类interface 用于定义接口 接口定义时,格式特点:1,接口中常量见定 ...
- EasyuiCombobox三级联动
有许多刚接触Easyui中Combobox控件的朋友可能都会遇到的问题:如何将Combobox做成三级联动? 先本人有个三级联动的案例给大家参考参考,经测试能通过.注意Combobox绑定的数据是Js ...
- iOS 关于tableView中有多个textField,输入框被遮住的解决方法
这里采用tableView整体上移的方法. 代码:(其中 60 为 单元格的高度) //点击输入框触发 - (void)textFieldDidBeginEditing:(UITextField *) ...
- Cortex-M3知识点
1.不再像别的ARM7那样从thumb状态和ARM状态来回切换 Thumb-2指令集横空出世,Cortex-M3不支持ARM指令集 2.BKP备份寄存器(42个16位寄存器组成),用来存储用户应用程序 ...
- toJOSN()方法
toJSON方法可以作为函数过滤器的补充.序列化的顺序如下: (1)如果存在toJSON方法而且能够通过它取得有效值,则调用该方法. (2)如果提供了第二个参数,应用该函数过滤器.传入过滤器的值是步骤 ...
- js戳和php戳时间换算
问题:剩余多少时间,如果只用php来输出,却看不到动态效果.解决办法,利用获取的时间减去当前时间js 时间格式转换php时间商品距离秒杀时间的天数时分秒<input name="tes ...
- shell脚本练习(autocert)
#!/bin/bash#By Spinestars#20131118 #name:ca_cert#certficate ca cd /etc/pki/CA/auto num=$RANDOM mv ./ ...
- mysqldump 的方式来搭建master-->slave 的复制架构
1.master 上要满足的最小条件: 1.server_id 已经设置成了一个非0值 2.log_bin 配置好binlog 2.slave 上要满足的最小条件 1.server_id 已经设置成了 ...
- atoi 和itoa用法
1.itoa 在linux下没有itoa这个函数 原型:char *itoa(int value,char *string,int radix) 用法 ...
- 【总结】OJ练习,进行的一些编程语言方面总结
1.STL vector只有四个构造函数 ) explicit vector (const allocator_type& alloc = allocator_type()); fill () ...