Spring--hibernate整合_事务管理

Spring声明式事务管理
本文介绍了如何在Spring中实现声明式事务管理,通过XML配置和AOP方式来确保多个操作要么全部成功,要么全部失败。文章详细展示了配置过程及关键代码。

当要求多个事务要么同时完成,要么同时无法完成时,就涉及到了事务的处理.

Spring提供了声明式事务管理,其原理是通过AOP来完成的。

<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>

tx就是事务管理

并引入相应的命名空间。

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: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-2.5.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd"
	xmlns:context="http://www.springframework.org/schema/context">
	
	<context:component-scan base-package="com.dw"/>
	<!-- <aop:aspectj-autoproxy/> -->
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <!-- results in a setDriverClassName(String) call -->
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/user"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>com/dw/config/User.hbm.xml</value>
				<value>com/dw/config/Log.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
            </props>
		</property>
	</bean>
	
	<bean id = "userDaoImpl" class="com.dw.impl.UserDaoImpl">
	  <property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<bean id = "logDaoImpl" class = "com.dw.impl.LogDaoImpl">
	  <property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<bean id ="service" class = "com.dw.service.ServiceUser">
	  <property name="userDao" ref="userDaoImpl"/>
	  <property name="logDao" ref="logDaoImpl"/>
	</bean>
	
	<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
	<tx:annotation-driven transaction-manager="txManager"/>
	
	
	
	
	
	<!-- 
	<bean id = "iterception" class="com.dw.model.Iterception"/>
	<aop:config>
	  <aop:pointcut expression="execution(* com.dw.car.*.*(..))" id="point"/>
	  <aop:aspect id="aspect" ref="iterception">
	    <aop:before method="before1" pointcut-ref="point"/>
	    <aop:after-returning method="after1" pointcut-ref="point"/>
	    <aop:around method="around1" pointcut="execution(* com.dw.car.*.*(..))"/>
	  </aop:aspect>
	</aop:config> -->
</beans>	

在相应的处理方法上使用注解@transactional

	@Transactional
	public void add(User user) {
		userDao.save(user);
		Log log = new Log();
		log.setName("您插入了一条数据!");
		logDao.save(log);
	}

然后通过getCurrentSession()获取Session对象。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值