目录
一、spring框架事务管理相关的类
1.PlatformTransactionManager接口
2.TransactionDefinition接口
二、spring框架声明式事务管理
1.配置文件的方式
(1)配置文件
(2)业务层
(3)持久层
(4)测试类
(5)运行
(6)查看数据库
(7)出现异常运行
2.半注解的方式
(1)配置文件
(2)db.properties
(3)持久层
(4)业务层
(5)测试类
(6)运行
(7)查看数据库
(8)加上异常
3.纯注解的方式
(1)持久层
(2)业务层
(3)配置类
(4)测试类
(5)运行
(6)查看数据库
(7)加上异常
一、spring框架事务管理相关的类
1.PlatformTransactionManager接口
平台事务管理器
2.TransactionDefinition接口
事务定义信息的接口
定义了事务隔离级别和事务传播行为
二、spring框架声明式事务管理
它有三种方式:配置文件的方式、半注解的方式(配置文件+注解)、纯注解的方式
每种方式都包括配置文件/配置类,业务层,持久层,测试类,运行结果及检验,还有模拟异常
1.配置文件的方式
(1)配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--第二种写法:使用提供标签的方式--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--加载属性的文件--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--配置平台事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置事务的通知--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="pay" isolation="DEFAULT" propagation="REQUIRED"/><tx:method name="find" read-only="true"/></tx:attributes></tx:advice><!--配置AOP的增强--><aop:config><!--spring框架提供系统通知,是使用advisor标签--><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.qcby.service.impl.AccountServiceImpl.pay(..))"/></aop:config><!--配置service--><bean id="accountService" class="com.qcby.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><bean id="accountDao" class="com.qcby.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"></property></bean></beans>
(2)业务层
import com.qcby.dao.AccountDao;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void pay(String out, String in, double money) {// 调用dao方法accountDao.outMoney(out,money);//异常//int a=1/0;accountDao.inMoney(in,money);}
}
(3)持久层
import com.qcby.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import javax.annotation.Resource;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {@Overridepublic void outMoney(String out, double money) {this.getJdbcTemplate().update("update account set money = money - ? where name = ?",money,out);}@Overridepublic void inMoney(String in, double money) {this.getJdbcTemplate().update("update account set money = money + ? where name = ?",money,in);}
}
(4)测试类
import com.qcby.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class demo {@Autowiredprivate AccountService accountService;@Testpublic void run(){accountService.pay("李四","张三",100);}
}
(5)运行
(6)查看数据库
原来
现在
(7)出现异常运行
查看数据库(数据没变)
2.半注解的方式
(1)配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="com.qcby"></context:component-scan><!--第二种写法:使用提供标签的方式--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--加载属性的文件--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--配置事务平台管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置JDBC模板类--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--开启事务注解的支持--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>
(2)db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_db
jdbc.username=root
jdbc.password=2020
(3)持久层
import com.qcby.dao.AccountDao;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;@Service
@Transactional(isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;@Overridepublic void pay(String out, String in, double money) {accountDao.outMoney(out,money);//模拟异常//int a=1/0;accountDao.inMoney(in, money);}
}
(4)业务层
import com.qcby.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;@Repository
public class AccountDaoImpl implements AccountDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void outMoney(String out, double money) {jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);}@Overridepublic void inMoney(String in, double money) {jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);}
}
(5)测试类
import com.qcby.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class demo {@Autowiredprivate AccountService accountService;@Testpublic void run(){accountService.pay("张三","李四",100);}
}
(6)运行
(7)查看数据库
原来是600 300
(8)加上异常
查看数据库
3.纯注解的方式
(1)持久层
import com.qcby.dao.AccountDao;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;@Service
@Transactional(isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;@Overridepublic void pay(String out, String in, double money) {accountDao.outMoney(out,money);//模拟异常//int a=1/0;accountDao.inMoney(in, money);}
}
(2)业务层
import com.qcby.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;@Repository
public class AccountDaoImpl implements AccountDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void outMoney(String out, double money) {jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);}@Overridepublic void inMoney(String in, double money) {jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);}
}
(3)配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.annotation.Resource;
import javax.sql.DataSource;@Configuration
@ComponentScan("com.qcby")
@EnableTransactionManagement
public class SpringConfig {@Beanpublic DataSource createDataSource(){DriverManagerDataSource dataSource=new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql:///spring_db");dataSource.setUsername("root");dataSource.setPassword("2020");return dataSource;}@Resource(name = "dataSource")@Bean(name = "jdbcTemplate")public JdbcTemplate createJdbcTemplate(DataSource dataSource) {JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}@Resource(name = "dataSource")@Bean(name = "transactionManager")public PlatformTransactionManager createTransactionManager(DataSource dataSource){DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource);return manager;}}
(4)测试类
import com.qcby.config.SpringConfig;
import com.qcby.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
public class demo2 {@Autowiredprivate AccountService accountService;@Testpublic void run(){accountService.pay("bbb","aaa",100);}
}
(5)运行
(6)查看数据库
原来
现在
(7)加上异常
查看数据库(数据没变)