一,前言
spring事物最典型的案列是用在转账上面,所以SSM框架中简单的模拟转账功能。
二、ApplicationContent.xml配置
2.1 完整的ssm框架配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
default-lazy-init="true">
<!-- 自动扫描 -->
<context:component-scan base-package="com.bsoft.cn" />
<!-- 加载properties配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatisConfig.xml" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/bsoft/cn/**/dao/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bsoft.cn.test.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="quartzTestTask" class="com.bsoft.cn.test.task.TestTask"/>
<!-- quartz配置 start-->
<!-- 定义调用对象和调用对象的方法 -->
<bean id="quartzTestBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--调用的类 -->
<property name="targetObject" ref="quartzTestTask" />
<!--调用类中的方法 -->
<property name="targetMethod" value="test" />
</bean>
<!-- 定义触发时间 -->
<bean id="testCronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="quartzTestBean" />
<!--cron表达式 -->
<property name="cronExpression" value="0 0/1 * * * ?"/>
</bean>
<!--任务总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="schedulerFactoryBean" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testCronTriggerBean"/>
</list>
</property>
</bean>
<!-- quartz配置 end-->
<!-- 配置事务管理 start-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 4.2 事务详情(事务通知) , 在aop筛选基础上,对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
<tx:attributes> 用于配置事务详情(属性属性)
<tx:method name=""/> 详情具体配置
propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
isolation 隔离级别
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 4.3 AOP编程,目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.bsoft.cn.test.service.*.*(..))"/>
</aop:config>
<!-- 配置事务管理 start end-->
</beans>
2.2 spring事物的相关配置
<!-- 配置事务管理 start-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 4.2 事务详情(事务通知) , 在aop筛选基础上,对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
<tx:attributes> 用于配置事务详情(属性属性)
<tx:method name=""/> 详情具体配置
propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
isolation 隔离级别
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 4.3 AOP编程,目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.bsoft.cn.test.service.*.*(..))"/>
</aop:config>
<!-- 配置事务管理 start end-->
三、测试案列
控制器:
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService service;
@RequestMapping("/transfer")
public String transfer(){
service.transfer("jack", "rose", 1000);
return "transfer";
}
}
服务类:
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
public void transfer(String ourName,String innerName,double money){
accountDao.out(ourName, money);
int a = 1/0;
accountDao.inner(innerName, money);
}
}
持久层:
public interface AccountDao {
//出账
public void out(@Param("userName")String userName,@Param("money")double money);
//入账
public void inner(@Param("userName")String userName,@Param("money")double money);
}
Mapper文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bsoft.cn.test.dao.AccountDao">
<update id="out">
update account set money = money - ${money} where username = '${userName}'
</update>
<update id="inner">
update account set money = money + ${money} where username = '${userName}'
</update>
</mapper>