一、为业务层添加声明式事务
1.如何在添加用户的业务流程中控制事务?
- 可以采用MyBatis控制事务
- 事务应该在业务逻辑层控制
- 硬编码方式,代码繁琐,且破坏分层,代码不易维护
- 可以采用AOP的方式实现
- Spring提供了声明式事务支持
2.配置声明式事务
配置步骤
- 导入tx和aop命名空间
- 定义事务管理器Bean,并为其注入数据源Bean
- 通过<tx:advice>配置事务增强,绑定事务管理器并针对不同方法定义事务规则
- 配置切面,将事务增强与方法切入点组合
3.事务属性
propagation:事务传播机制
- REQUIRED(默认值)
- REQUIRES_NEW 、MANDATORY、NESTED
- SUPPORTS
- NOT_SUPPORTED、NEVER
注:REQUIRED能够满足大多数的事务需求 可以作为首选的事务传播行为支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择
注:SUPPORTS支持当前事务,如果当前没有事务,就以非事务方式执行。
isolation:事务隔离等级
- DEFAULT(默认值)
- READ_COMMITTED
- READ_UNCOMMITTED
- REPEATABLE_READ
- SERIALIZABLE
timeout:事务超时时间,允许事务运行的最长时间,以秒为单位。默认值为-1,表示不超时
read-only:事务是否为只读,默认值为false
rollback-for:设定能够触发回滚的异常类型
- Spring默认只在抛出runtime exception时才标识事务回滚
- 可以通过全限定类名指定需要回滚事务的异常,多个类名用逗号隔开
no-rollback-for:设定不触发回滚的异常类型
- Spring默认checked Exception不会触发事务回滚
- 可以通过全限定类名指定不需回滚事务的异常,多个类名用英文逗号隔开
4.使用注解实现事务处理
- 在Spring配置文件中配置事务管理类,并添加对注解配置的事务的支持
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager"/>
- 使用@Transactional为方法添加事务支持
@Transactional @Service("userService") public class UserServiceImpl implements UserService { …… @Transactional(propagation = Propagation.SUPPORTS) public List<User> findUsersWithConditions(User user) { // 省略实现代码 } }
属性
类型
说明
propagation
枚举型:Propagation
可选的传播性设置。使用举例:
@Transactional(propagation=Propagation.REQUIRES_NEW)
isolation
枚举型:Isolation
可选的隔离性级别。使用举例:
@Transactional(isolation=Isolation.READ_COMMITTED)
readOnly
布尔型
是否为只读型事务。使用举例:@Transactional(readOnly=true)
timeout
int型(以秒为单位)
事务超时。使用举例:Transactional(timeout=10)
属性
类型
说明
rollbackFor
一组 Class 类的实例,必须是Throwable的子类
一组异常类,遇到时 必须 回滚。使用举例:@Transactional(
rollbackFor={SQLException.class}),多个异常用逗号隔开
rollbackForClassName
一组 Class 类的名字,必须是Throwable的子类
一组异常类名,遇到时 必须 回滚。使用举例:@Transactional(
rollbackForClassName={
"SQLException"}),多个异常用逗号隔开
noRollbackFor
一组 Class 类的实例,必须是Throwable的子类
一组异常类,遇到时 必须不 回滚
noRollbackForClassName
一组 Class 类的名字,必须是Throwable的子类
一组异常类名,遇到时 必须不 回滚
一、Spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--使用扫描注解注入的包-->
<context:component-scan base-package="cn.gwj"/>
<!-- 配置数据源、连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件-->
<property name="dataSource" ref="dataSource"/>
<!-- 引用MyBatis配置文件中的配置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 配置SQL映射文件信息-->
<property name="mapperLocations" value="classpath:cn/gwj/dao/**/*.xml"/>
<!-- mapperLocations是Resource[]类型-->
<!-- <list>-->
<!-- <value>classpath:cn/gwj/dao/**/*.xml</value>-->
<!-- </list>-->
<!-- </property>-->
</bean>
<!-- 配置业务bean-->
<bean id="userService" class="cn.gwj.service.user.UserServiceImpl"/>
<!-- 配置mapper 映射dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指明sqlSessionFactoryBean的名字 可要可不要-->
<!-- 指明工厂sqlSessionFactoryBean的名字-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指明接口的包-->
<property name="basePackage" value="cn.gwj.dao"/>
</bean>
<!-- 声明切面类-->
<bean class="cn.gwj.aop.AopLogger"/>
<!-- 开启使用注解驱动切面-->
<aop:aspectj-autoproxy/>
<!-- 定义事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 对注解配置的事务的支持-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!--注解和定义规则只能存在一个-->
<!--<!– 配置事务增强绑定事务管理器并针对不同方法定义事务规则–>-->
<!-- <tx:advice id="interceptor">-->
<!-- <tx:attributes>-->
<!-- <tx:method name="get*" propagation="SUPPORTS"/>-->
<!-- <tx:method name="update*" propagation="REQUIRED"/>-->
<!-- <tx:method name="delete*" propagation="REQUIRED"/>-->
<!-- <tx:method name="add*" propagation="REQUIRED"/>-->
<!-- <tx:method name="*" propagation="REQUIRED"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!--<!– 配置切面,将事务增加与方法切入点组合–>-->
<!-- <aop:config>-->
<!--<!– 定义切点–>-->
<!-- <aop:pointcut id="serviceMd" expression="execution(* cn.gwj.service..*.*(..))"/>-->
<!-- <aop:advisor advice-ref="interceptor" pointcut-ref="serviceMd"/>-->
<!-- </aop:config>-->
</beans>
二、三层
package cn.gwj.service.user;
import cn.gwj.dao.UserDao;
import cn.gwj.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
public UserServiceImpl() {}
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public User login(String userCode, String userPassword) {
// TODO Auto-generated method stub
User user = null;
try {
//user = userDao.getLoginUser(userCode);
user = userDao.getLoginUser(userCode);
System.out.println("实现类:"+user.getUserPassword());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//MybatisUtil.closeSqlSession(sqlSession);
}
//匹配密码
if(null != user){
if(!user.getUserPassword().equals(userPassword))
user = null;
}
return user;
}
}