【Spring】Spring+hibernate整合

整合目的:

将hibernate中的sessionFactory交给Spring创建

将hibernate中的事务交给Spring的声明式事务管理


整合准备:

导入jar包:

hibernate核心jar包


spring-core相关包


spring-aop相关包


spring-orm相关包



编写实体类Dept

package cn.qblank.entity;

public class Dept {
	private int deptId;
	private String deptName;
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
}

在对应包下,写hibernate映射Dept.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.qblank.entity">
	<class name="Dept" table="t_dept">
		<id name="deptId" column="deptId" type="int">
			<generator class="native"></generator>
		</id>
		<property name="deptName" column="deptName" type="string"></property>
	</class>
</hibernate-mapping>



然后编写DeptDao进行对象的保存

package cn.qblank.dao;

import org.hibernate.SessionFactory;

import cn.qblank.entity.Dept;

public class DeptDao {
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	//spring和hibernate整合
	public void save(Dept dept){
		sessionFactory.getCurrentSession().save(dept);
	}
}

继续编写DeptService方法调用DeptDao

package cn.qblank.service;

import cn.qblank.dao.DeptDao;
import cn.qblank.entity.Dept;

public class DeptService {
	private DeptDao deptDao;
	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
	public void save(Dept dept){
		deptDao.save(dept);
		//抛异常,整个事务回滚
		int i = 1/0;
	}
}


将数据库的连接信息写入db.properties中,然后再再bean.xml中加载出来:

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///day09
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
jdbc.maxStatements=100
jdbc.acquireIncrement=2


然后再src下面建立bean.xml编写各种配置:将各个实体加入IOC容器中、配置数据源、spring和hibernate的整合配置、

事务管理类配置、AOP配置

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
     	 http://www.springframework.org/schema/tx/spring-tx.xsd">
     <!-- 配置各个实例 -->
     <!-- 配置dao实例 -->
     <bean id="deptDao" class="cn.qblank.dao.DeptDao">
     	<property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
     <!-- 配置service实例 -->
     <bean id="deptService" class="cn.qblank.service.DeptService">
     	<property name="deptDao" ref="deptDao"></property>
     </bean>
     
     <!-- 1.加载配置文件db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>
	<!-- 2.数据源配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name= "initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
		<property name="maxStatements" value="${jdbc.maxStatements}"></property>
		<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
	</bean>
	
	<!-- 3.###########Spring与Hibernate整合########### -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 注入连接池对象 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- hibernate常用配置 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<!-- hibernate映射配置 -->
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:cn/qblank/entity/</value>
			</list>
		</property>
	</bean>
     
     <!-- 事务管理类配置 -->
     <!-- 1.配置事务管理类 -->
     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     	<property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
     <!-- 2.配置事务增强类 -->
     <tx:advice id="txAdvice" transaction-manager="txManager">
     	<tx:attributes>
     		<tx:method name="*" read-only="false"/>
     	</tx:attributes>
     </tx:advice>
     <!-- AOP配置 -->
     <aop:config>
     	<aop:pointcut expression="execution(* cn.qblank.service.*.*(..))" id="pt"/>
     	<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
     </aop:config>
</beans>

这样我们就完成了spring和hibernate的整合配置

首先我们测试插入一条数据

将service中的异常:int i = 1/0;注释掉

注意:第一次的启动的时候需要在bean.xml中把hiberbnate配置中的hibernate.hbm2ddl.auto中的值改为create

写出测试类:

package cn.qblank.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.qblank.entity.Dept;
import cn.qblank.service.DeptService;

public class App {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
		DeptService deptService = (DeptService) ac.getBean("deptService");
		Dept dept = new Dept();
		dept.setDeptName("广信部");
		deptService.save(dept);
	}
}

运行结果如下:


然后我们把异常加上,再测试事务是否会回滚

package cn.qblank.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.qblank.entity.Dept;
import cn.qblank.service.DeptService;

public class App {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
		DeptService deptService = (DeptService) ac.getBean("deptService");
		Dept dept = new Dept();
		dept.setDeptName("宣传部");
		deptService.save(dept);
	}
}

此时控制台显示插入数据的信息,也抛出了异常


然后我们再去观察数据库看看是否发生了回滚



可以看到数据并没有变,说明此时数据发生了回滚,说明Spring和hibernate的整合成功了!!!!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值