SSH系列:(5)整合Spring和Hibernate

Spring和Hibernate整合的关键是:Hibernate的SessionFactory由Spring的IOC容器来创建、Hibernate的事务由Spring的AOP来进行管理。 注意:由于进行整合,Hibernate的配置文件(hibernate.cfg.xml)中的配置,可以全部写到Spring的配置文件当中,因此可以删除hibernate.cfg.xml文件。

Hibernate 整合前 整合后
SessionFactory 由开发者创建 由Spring IOC容器创建
Transaction 由开发者维护(细粒度事务) 由Spring AOP实现动态注入(粗粒度事务)

(1)添加jar包

(2)将hibernate配置转移到Spring的配置当中

(3)dao层和service层代码准备

(4)将dao层和service层的类注入到Spring的配置当中

(5)测试

1、添加jar包

添加spring-aop、spring-jdbc、spring-orm相关jar包


spring-aop相关jar包

aopalliance-.jar

aspectjrt.jar

aspectjweaver.jar

spring-aop-3.2.5.RELEASE.jar


spring-jdbc相关jar包

spring-jdbc-3.2.5.RELEASE.jar

spring-tx-3.2.5.RELEASE.jar


spring-orm相关jar包

spring-orm-3.2.5.RELEASE.jar

2、将hibernate配置转移到Spring的配置当中

2.1、删除hibernate.cfg.xml文件

只要删除hibernate.cfg.xml文件即可

2.2、添加bean-base.xml文件

这里面主要保存了原来hibernate.cfg.xml中的配置,主要包括以下三方面内容:数据库连接信息、Hibernate的SessionFactory对象的创建和配置、Hibernate的事务管理。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
     	http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 导入外部的properties配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driverClass}"></property>
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="${initialPoolSize}"></property>
		<!--连接池中保留的最小连接数。Default: 3 -->
		<property name="minPoolSize" value="${minPoolSize}"></property>
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${maxPoolSize}"></property>
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${acquireIncrement}"></property>
		<!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${maxIdleTime}"></property>
	</bean>

<!-- 
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/tax_sys"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="minPoolSize" value="3"></property>
		<property name="maxPoolSize" value="15"></property>
		<property name="acquireIncrement" value="3"></property>
		<property name="maxIdleTime" value="1800"></property>
	</bean>
 -->

	<!-- SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">false</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="mappingLocations">
			<list>
				<value>classpath:com/rk/*/entity/*.hbm.xml</value>
			</list>
		</property>
	</bean>

	<!-- 事务管理 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="load*" read-only="true"/>
			<tx:method name="search*" read-only="true"/>
			<tx:method name="list*" read-only="true"/>
			<tx:method name="*" read-only="false" rollback-for="Throwable"/>
		</tx:attributes>
	</tx:advice>

	<!-- 利用aop植入事务 -->
	<aop:config>
		<aop:pointcut id="pt" expression="bean(*Service)"/>
		<!-- <aop:pointcut id="pt" expression="execution(* com.rk.test.service.impl.*.*(..))"/> -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
	</aop:config>

</beans>

在上述配置当中,我们有两处优化的地方:

(1)将数据库的配置参数放置到一个db.properties文件中保存

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/tax_sys?useUnicode=true&characterEncoding=utf8
user=root
password=root
initialPoolSize=4
minPoolSize=2
maxPoolSize=12
acquireIncrement=2
maxIdleTime=1800

同时,注意bean-base.xml文件中这一配置

    <!-- 导入外部的properties配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />

(2)应用事务时,切入点表达式的改进

原来

<aop:pointcut id="pt" expression="execution(* com.rk.test.service.impl.*.*(..))"/>

转变后

<aop:pointcut id="pt" expression="bean(*Service)"/>

2.3、将bean-base.xml文件引入到applicationContext.xml文件中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
     	http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<import resource="classpath:bean-base.xml"/>

</beans>

重点是下面这一句

<import resource="classpath:bean-base.xml"/>

3、dao层和service层代码准备

PersonDao.java

package com.rk.test.dao;

import java.io.Serializable;

import com.rk.test.entity.Person;

public interface PersonDao {
	Person findById(Serializable id);
	void save(Person p);
}

PersonDaoImpl.java

package com.rk.test.dao.impl;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.rk.test.dao.PersonDao;
import com.rk.test.entity.Person;

public class PersonDaoImpl implements PersonDao {
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Person findById(Serializable id) {
		Session session = sessionFactory.getCurrentSession();
		Person p = (Person) session.get(Person.class, id);
		return p;
	}

	public void save(Person p) {
		Session session = sessionFactory.getCurrentSession();
		session.save(p);
	}

}

PersonService.java

package com.rk.test.service;

import java.io.Serializable;

import com.rk.test.entity.Person;

public interface PersonService {
	Person findById(Serializable id);
	void save(Person p);
}

PersonServiceImpl.java

package com.rk.test.service.impl;

import java.io.Serializable;

import com.rk.test.dao.PersonDao;
import com.rk.test.entity.Person;
import com.rk.test.service.PersonService;

public class PersonServiceImpl implements PersonService {
	private PersonDao personDao;
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public Person findById(Serializable id) {
//		Person p = new Person();
//		p.setpName("小红");
//		personDao.save(p);
		return personDao.findById(id);
	}

	public void save(Person p) {
		personDao.save(p);
//		int i = 1/ 0;
	}

}

4、将dao层和service层的类注入到Spring的配置当中

bean-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
     	http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="personDao" class="com.rk.test.dao.impl.PersonDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

</beans>

bean-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
     	http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="personService" class="com.rk.test.service.impl.PersonServiceImpl">
		<property name="personDao" ref="personDao"></property>
	</bean>

</beans>

applicationContext.xml引入 bean-dao.xml、bean-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
     	http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<import resource="classpath:bean-base.xml"/>
	<import resource="classpath:com/rk/*/config/bean-*.xml"/>

</beans>

重点是下面这一句

<import resource="classpath:com/rk/*/config/bean-*.xml"/>

5、测试

测试分为两方面:

(1)普通测试:能够读取一条数据、能够保存一条数据

(2)事务测试:只读事务中不能存储数据、事务出现异常要进行回滚

package com.rk.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rk.test.entity.Person;
import com.rk.test.service.PersonService;

public class TestMerge {
	private ApplicationContext ac;
	@Before
	public void init()
	{
		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	}

	@Test
	public void testFindById()
	{
		PersonService personService = (PersonService) ac.getBean("personService");
		Person p = personService.findById("4028d081564ac44401564ac4478b0000");
		System.out.println(p);
	}

	@Test
	public void testSave()
	{
		PersonService personService = (PersonService) ac.getBean("personService");
		Person p = new Person();
		p.setpName("Lucy");
		personService.save(p);
	}

	@Test
	public void testTransactionReadOnly()//只读事务,如果在只读事务中出现更新操作则回滚
	{
		PersonService personService = (PersonService) ac.getBean("personService");
		Person p = personService.findById("4028d081564ac44401564ac4478b0000");
		System.out.println(p);
	}

	@Test
	public void testTransactionRollback()//回滚事务,如果操作中出现有任务异常则回滚先前的操作
	{
		PersonService personService = (PersonService) ac.getBean("personService");
		Person p = new Person();
		p.setpName("Lily");
		personService.save(p);
	}

}
时间: 2024-08-18 12:41:27

SSH系列:(5)整合Spring和Hibernate的相关文章

SSH程序框架之Spring与HIbernate整合

spring整合hibernate 有两种方式 1.注解方式 2.xml方式实现 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 1.xml方式: 大概架构: 我们将会创建一个账户类,实现添加的功能 首先先添加Spring 和 HIbernate的jar包节点: 1 <dependencies> 2 <dependency> 3 <groupId&g

使用Maven 整合Spring和hibernate 适合初级接触的学者

本文,主要介绍Spring 和 hibernate怎么去整合,废话就不多说了,如果不知道spring 和hibernate 是干嘛的,还请去问问度娘哈.下面开始一步一步搭建: 工具: Idea 一.先搭建Spring 1.新建一个maven项目:至于填写项目名称什么的就不一一介绍了 这里我的idea没有自动生成test文件夹,需要大家自己建一下,之后的项目目录如下图所示: 2.下面我贴出项目的Pom文件: <project xmlns="http://maven.apache.org/PO

web整合Spring和Hibernate

上一篇是简单整合web和Spring, 这一篇是整合hibernate: 连接池c3p0: spring5.0, hibernate5.0 jars: ----------------------------- web.xml,增加高亮部分,不然会报 Could not obtain transaction-synchronized Session for current thread 如果不加,则需要在用的时候显示声明式事务:并且改为opensession: Session s = t.get

【SSH系列】深入浅出spring IOC中三种依赖注入方式

spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控制什么?依赖注入和控制反转是一样的概念吗?接触新的知识,小编的脑袋中全是大大的问号,不过没有关系,今天这篇博文,小编主要来简单的介绍一下在spring IOC中依赖注入的方法. 依赖注入和控制反转,目的是为了使类与类之间解耦合,提高系统的可扩展性和可维护性.我们可以从以下几个方面理解: a.参与者都

SSH系列:(3)Hibernate

(1)引入jar包 (2)配置 (3)测试 1.引入jar包 引入mysql jar包 mysql-connector-java-5.1.38-bin.jar 引入c3p0 jar包 c3p0-0.9.1.2.jar 引入hibernate相关jar包 (hibernate-distribution-3.6.0.Final) antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar hibernate3.jar hibernate-j

SSH开发实践part1:Spring与Hibernate整合

1 之前把SSH看完了,现在从头开始进行项目实践.现在讲整个过程中的点滴记录下来,希望对后来者有参考. 2 SSH是一个轻量级的java开发框架,struts负责MVC开发模式中的controller角色,hibernate则是负责对象的持久化,也就是对DB的访问,spring则是利用其IOC反转控制来完成对bean对象的管理,包括对hibernate的管理.好吧,这些东西相信大家都不陌生.现在我们正式开始,整个开发步骤主要包括以下几点: 新建web project项目 增加spring与hib

Spring与Hibernate整合

Spring与Hibernate整合 Spring与Hibernate整合关键点: 1) Hibernate的SessionFactory对象交给Spring创建:   SessionFactory即是hibernate.cfg.xml配置文件 2) hibernate事务交给spring的声明式事务管理. SSH整合: Spring与Struts: Spring与hibernate整合: SH整合步骤: 1)引入jar包 连接池/数据库驱动包   spring对c3p0连接池的支持比hiber

eclipse整合Spring 4 + Struts 2.5 + Hibernate 4.2

本次搭建的SSH项目源码已上传到百度云盘.没有使用MAVEN,下载下来,在applicationContext配置下mysql,执行下test.sql脚本就可以运行了. 链接:http://pan.baidu.com/s/1nvqOcPj 密码:yv19 1. 配置Struts2 a. 拷贝Struts2 jar包 b. 设置JSP编码为UTF-8 c. 在web.xml添加struts2 Filter d. 添加struts.xml到src目录 e. 添加测试Action,测试Struts2是

《Spring学习笔记》:Spring、Hibernate、struts2的整合(以例子来慢慢讲解,篇幅较长)

<Spring学习笔记>:Spring.Hibernate.struts2的整合(以例子来慢慢讲解,篇幅较长) 最近在看马士兵老师的关于Spring方面的视频,讲解的挺好的,到了Spring.Hibernate.struts2整合这里,由于是以例子的形式来对Spring+Hibernate+struts2这3大框架进行整合,因此,自己还跟着写代码的过程中,发现还是遇到了很多问题,因此,就记录下. 特此说明:本篇博文完全参考于马士兵老师的<Spring视频教程>. 本篇博文均以如下这