SSH项目Spring自动管理事务

applicationContext.xml配置

  • 开启事务管理器(transactionManager),其子标签property为sessionFactory
  • 配置通知,其中查找类方法propagation=“SUPPORTS”,read-only=“true”
  • 配置切点切面,expression=“execution(* service.impl..(…))”,切点为service.impl层的任意方法

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

	
	<context:property-placeholder
		location="classpath:db.properties" />

	
	<bean name="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}">property>
		<property name="driverClass" value="${jdbc.driverClass}">property>
		<property name="user" value="${jdbc.user}">property>
		<property name="password" value="${jdbc.password}">property>
	bean>

	
	<bean name="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource">property>
		
		<property name="hibernateProperties">
			<props>
				
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
				
				<prop key="hibernate.show_sql">trueprop>
				<prop key="hibernate.format_sql">trueprop>
				<prop key="hibernate.hbm2ddl.auto">updateprop>
			props>
		property>
		
		<property name="mappingDirectoryLocations"
			value="classpath:domain">property>
	bean>

	
	<bean name="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory">property>
	bean>

	
	<tx:advice id="txAdvice"
		transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="find*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="login" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="show*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="*Get*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="*Show*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="*Find*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="*Select*" propagation="SUPPORTS"
				read-only="true" />
			<tx:method name="checkForStu" propagation="SUPPORTS"
				read-only="true" />
		tx:attributes>
	tx:advice>

	
	<aop:config>
		<aop:pointcut
			expression="execution(* service.impl.*.*(..))" id="txPc" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
	aop:config>
	
	
	<context:component-scan base-package="action" />
	<context:component-scan base-package="service.impl" />
	<context:component-scan base-package="dao.impl" />
	
	
	
beans>

serviceImpl方法实现

不需要在手动控制事务

@Override
	// 用户登录获取个人信息
	public User login(User u) {
		User existU = userDao.login(u);
		return existU;		
	}

dao层

使用SessionFactory的getCurrentSession,此时session会自动关闭

private SessionFactory sessionFactory;
public User login(User u) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
	}

你可能感兴趣的:(SSH项目Spring自动管理事务)