MyBatis_与spring的整合之添加事务



源码下载

    MyBatis与spring的整合-添加事务-src.zip


在上篇的基础上做如下修改(添加)


1. 添加jar


    com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar


2. ApplicationContext.xml 事务配置

	<tx:advice id="userTxAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="select*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<!-- 第一个"*"代表所有类,第二个"*"代表所有方法 ".."代表任意参数 -->
		<aop:pointcut id="pc" expression="execution(* com.hehe.mybatis.service.*.*(..))" />
		<!--把事务控制在Service层-->
		<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
	</aop:config>
	<!-- =================================================================================    -->	

3. 添加 service 层


public interface UserService {
    
    public void updateUser(User user);
}
public class UserServiceImpl implements UserService {

    private UserDao userDao;
    
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
        System.out.println( 1/0 );
    }
}

4. junit test

 4.1 TestUserDao

/*
	<update id="updateUserByCondition" parameterType="User">
		update user
		<set>
			<if test="name != null">
				name = #{name} ,
			</if>
			<if test="address != null">
				address = #{address}
			</if>
		</set>
		where id = #{id}
	</update>
 */
	@Test
	public void testUpdateUserByCondition() {
		UserDao userDao = (UserDao) context.getBean("userDao");
		User user = new User();
		user.setId("0001");
		user.setName("zhangsan44");
		userDao.updateUser(user);
	}	
		
 4.2 TestUserService
	@Test
	public void testUpdateUser() {
		ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		UserService userService = (UserService) context.getBean("userService");
		
		User user = new User();
		user.setId("0001");
		user.setName("zhangsan77");
		userService.updateUser(user);
	}

5. 完整的 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: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-3.0.xsd
				         http://www.springframework.org/schema/context
				         http://www.springframework.org/schema/context/spring-context-3.0.xsd
				         http://www.springframework.org/schema/tx
				         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
				         http://www.springframework.org/schema/aop 
				         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 配置数据源 简单数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql:///mybatis"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
	</bean>
	
	<!-- SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- mybatis总配置文件的位置 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
	</bean>
	
	
	<!-- ================================事务相关控制=================================================    -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<tx:advice id="userTxAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="select*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<!-- 第一个"*"代表所有类,第二个"*"代表所有方法 ".."代表任意参数 -->
		<aop:pointcut id="pc" expression="execution(* com.hehe.mybatis.service.*.*(..))" />
		<!--把事务控制在Service层-->
		<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
	</aop:config>
	<!-- =================================================================================    -->	
	
	
	<bean id="userDao" class="com.hehe.mybatis.dao.impl.UserDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>
	
	<bean id="userService" class="com.hehe.mybatis.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"/>
	</bean>
	
	
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值