Spring(15):为业务层Service 配置声明式事务(下)-- @Transactional注解配置

本文介绍如何在Spring框架中使用注解实现声明式事务管理。通过在Service层应用@Transactional注解简化了事务配置,并提供了对不同事务规则的支持。

2018/1/3

承接《Spring(15):为业务层Service 配置声明式事务(上)-- AOP配置》,项目介绍使用注解实现声明式事务的功能:

【1】对工程的修改并不大,只是利用aop的注解代替aop的xml配置,在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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx 
     	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 数据源配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url">
			<value><![CDATA[jdbc:mysql://127.0.0.1:3306/test?
					useUnicode=true&characterEncoding=utf-8]]></value>
		</property>
		<property name="username" value="root"></property>
		<property name="password" value=""></property>    
    </bean>
    
    <!-- SqlSessionFactoryBean 配置 -->
    <bean id="sqlSessionFactory"
    		class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 引用数据源组件 -->
		<property name="dataSource" ref="dataSource"></property>
	<!-- 引用Mybatis配置文件的配置 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
	
	<!-- 配置MapperScannerConfigurer,扫描生成mapper -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.smbms.dao"></property>  
    </bean>     	
    
    <!-- 扫描注解配置 -->
    <context:component-scan base-package="com.smbms.service"></context:component-scan>
    
    <!-- 事务管理器组件,以提供对事务处理的全面支持和统一管理,在切面中相当于增强处理的角色 ,需要注入数据源组件-->
    <bean id="txManager"
           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>
    
</beans>

解释:

(1)仍然需要在Spring配置文件中配置 事务管理器(类),并添加对注解配置事务的支持;


【2】对实现类UserServiceImpl.java作以下修改:

package com.smbms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.smbms.dao.UserMapper;
import com.smbms.entities.User;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public boolean addNewUser(User user) {
		boolean result = false;
		try{
			if(userMapper.add(user) == 1){
				result  = true; 
			}
		}catch (RuntimeException e){
			e.printStackTrace();
			throw e;
		}
		return result;
	}
	
	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public List<User> findUserByUsername(String username) {
		return findUserByUsername(username);
	}

	public UserServiceImpl(UserMapper userMapper) {
		super();
		this.userMapper = userMapper;
	}
	public UserServiceImpl() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public UserMapper getUserMapper() {
		return userMapper;
	}
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

}

解释:

(1)使用@Transactional 注解即可为该类的所以业务方法统一添加事务处理;

(2)如果有例外,某个方法需要采用不同的事务规则,可以在改业务方法上添加@Transactional注解,单独设置;

(3)@Transactional 也是可以设置事务属性的值。


【3】工程说明:

(1)这是Mybatis和Spring整合练习的工程;
(2)实际使用有3个包:dao、entities、service;
(3)Mybatis配置文件内容很简洁,Spring完成大部分配置管理;
(4)通过SQLSessionTemplate的实现类对数据库进行操作;
(5)配置DAO组件并进入SqlSessionTemplate实例;
(6)配置业务Bean并注入DAO实例 ;

(7)完成的功能是:查询provider的列表,模糊查询供应商信息;
(8)与smbms05MybatisSpring的区别:去掉了实现类ProviderMapperImpl;仅仅保留ProviderMapper
接口和相关SQL映射文件,通过MapperFactoryBean注入给业务组件。

(9)此外,也新增了一个User查询,照壶画瓢;
(10)smbms06是使用MapperFactoryBean注入映射器,
         而smbms07是更加方便的使用MapperScannerConfigurer注入映射器。
         
(11)smbms08增加新功能,实现按条件查询订单表Bill;使用resultMap做显示列表字段的自定义映射,
	使用Map传参,
             采用MapperFactoryBean注册映射器实现。
     [bill 和 provider 表]
(12) smbms09 在 smbms08 的基础上,进行拓展,用MapperScannerConfigurer注入映射器。
(13) smbms10,声明式事务,提高在XML配置文件注册org.springframework.jdbc.datasource.DataSourceTransactionManager,
          然后利用AOP切面对事务进行注入时。

(14) smbms11,使用注解去实现声明式事务,@Transaction。


综上,这里使用了@Transactional注解代替xml的配置,利用xml文件配置aop其实也不难,不妨参考上一篇博文:《Spring(15):为业务层Service 配置声明式事务(上)-- AOP配置》


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

后台技术汇

对你的帮助,是对我的最好鼓励。

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

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

打赏作者

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

抵扣说明:

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

余额充值