2.3.1搭建环境
2.3.1.1创建表
create database ee19_spring_day03;
use ee19_spring_day03;
create table account(
id int primary key auto_increment,
username varchar(50),
money int
);
insert into account(username,money) values(‘jack’,‘10000’);
insert into account(username,money) values(‘rose’,‘10000’);
2.3.1.2导入jar包
核心:4+1
aop : 4 (aop联盟、spring aop、aspectj规范、spring aspect)
数据库:2 (jdbc/tx)
驱动:mysql
连接池:c3p0
2.3.1.3dao层
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void out(String outer, Integer money) {
this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,outer);
}
@Override
public void in(String inner, Integer money) {
this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,inner);
}
}
2.3.1.4service层
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, Integer money) {
accountDao.out(outer, money);
//断电
// int i = 1/0;
accountDao.in(inner, money);
}
}
2.3.1.5spring配置
<?xml version="1.0" encoding="UTF-8"?><!-- 1 datasource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ee19_spring_day03"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<!-- 2 dao -->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3 service -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
2.3.1.6测试
@Test
public void demo01(){
String xmlPath = “applicationContext.xml”;
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
AccountService accountService = (AccountService) applicationContext.getBean(“accountService”);
accountService.transfer(“jack”, “rose”, 1000);
}
2.3.2手动管理事务(了解)
spring底层使用 TransactionTemplate 事务模板进行操作。
操作
1.service 需要获得 TransactionTemplate
2.spring 配置模板,并注入给service
3.模板需要注入事务管理器
4.配置事务管理器:DataSourceTransactionManager ,需要注入DataSource
2.3.2.1Dao类
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImple extends JdbcDaoSupport implements AccountDao {
@Override
public void out(String outer, int money) {
this.getJdbcTemplate().update("update account set money = money - ? where name = ?",money,outer);
}
@Override
public void in(String inner, int money) {
this.getJdbcTemplate().update("update account set money = money + ? where name = ?", money,inner);
}
}
2.3.2.2修改service
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
//需要spring注入模板
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
@Override
public void transfer(final String outer,final String inner,final Integer money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
accountDao.out(outer, money);
//断电
// int i = 1/0;
accountDao.in(inner, money);
}
});
}
}
2.3.2.3修改spring配置
<?xml version="1.0" encoding="UTF-8"?><!-- 创建数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study"></property>
<property name="user" value="root"></property>
<property name="password" value="10086"></property>
</bean>
<!-- 配置dao -->
<bean id="accountDaoImple" class="cn.lm.tx01.AccountDaoImple">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置service -->
<bean id="accountServiceImplId" class="cn.lm.tx01.AccountServiceImpl">
<property name="accountDao" ref="accountDaoImple"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
<!-- 创建模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>
<!-- 配置事务管理器 ,管理器需要事务,事务从Connection获得,连接从连接池DataSource获得 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
2.3.2.4测试 import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
String xmlPath = "cn/lm/tx01/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
AccountService accountService = (AccountService) applicationContext.getBean("accountServiceImplId");
accountService.transfer("jack", "rose", 1000);
}
}
2.3.3工厂bean 生成代理:半自动
spring提供 管理事务的代理工厂bean TransactionProxyFactoryBean
1.getBean() 获得代理对象
2.spring 配置一个代理
2.3.3.1Dao类
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImple extends JdbcDaoSupport implements AccountDao {
@Override
public void out(String outer, int money) {
this.getJdbcTemplate().update("update account set money = money - ? where name = ?",money,outer);
}
@Override
public void in(String inner, int money) {
this.getJdbcTemplate().update("update account set money = money + ? where name = ?", money,inner);
}
}
2.3.3.2Service类
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, int money) {
accountDao.out(outer, money);
int i = 1 / 0;
accountDao.in(inner, money);
}
}
2.3.3.3spring配置
<?xml version="1.0" encoding="UTF-8"?><!-- 创建数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study"></property>
<property name="user" value="root"></property>
<property name="password" value="10086"></property>
</bean>
<!-- 配置dao -->
<bean id="accountDaoImple" class="cn.lm.tx02.AccountDaoImple">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置service -->
<bean id="accountServiceImplId" class="cn.lm.tx02.AccountServiceImpl">
<property name="accountDao" ref="accountDaoImple"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4 service 代理对象
4.1 proxyInterfaces 接口
4.2 target 目标类
4.3 transactionManager 事务管理器
4.4 transactionAttributes 事务属性(事务详情)
prop.key :确定哪些方法使用当前事务配置
prop.text:用于配置事务详情
格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
传播行为 隔离级别 是否只读 异常回滚 异常提交
例如:
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop> 默认传播行为,和隔离级别
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop> 只读
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop> 有异常扔提交 -->
<bean id="proxyAccountService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces" value="cn.lm.tx02.AccountService"></property>
<property name="target" ref="accountServiceImplId"></property>
<property name="transactionManager" ref="txManager"></property>
<property name="transactionAttributes">
<props>
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
</bean>
2.3.3.4测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
String xmlPath = "cn/lm/tx02/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
AccountService accountService = (AccountService) applicationContext.getBean("proxyAccountService");
accountService.transfer("jack", "rose", 1000);
}
}
2.3.4AOP 配置基于xml【掌握】
在spring xml 配置aop 自动生成代理,进行事务的管理
1.配置管理器
2.配置事务详情
3.配置aop
2.3.4.1Dao
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImple extends JdbcDaoSupport implements AccountDao {
@Override
public void out(String outer, int money) {
this.getJdbcTemplate().update("update account set money = money - ? where name = ?",money,outer);
}
@Override
public void in(String inner, int money) {
this.getJdbcTemplate().update("update account set money = money + ? where name = ?", money,inner);
}
}
2.3.4.2Service
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, int money) {
accountDao.out(outer, money);
int i = 1 / 0;
accountDao.in(inner, money);
}
}
2.3.4.3配置文件
<!-- 创建数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study"></property>
<property name="user" value="root"></property>
<property name="password" value="10086"></property>
</bean>
<!-- 配置dao -->
<bean id="accountDaoImple" class="cn.lm.tx03_xml.AccountDaoImple">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置service -->
<bean id="accountServiceImplId" class="cn.lm.tx03_xml.AccountServiceImpl">
<property name="accountDao" ref="accountDaoImple"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 事务详情(事务通知) , 在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>
<!-- AOP编程,目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.lm.tx03_xml.*.*(..))"/>
</aop:config>
2.3.4.4测试 import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
String xmlPath = "cn/lm/tx03_xml/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
AccountService accountService = (AccountService) applicationContext.getBean("accountServiceImplId");
accountService.transfer("jack", "rose", 1000);
}
}
2.3.5AOP配置基于注解【掌握】
1.配置事务管理器,将并事务管理器交予spring
2.在目标类或目标方法添加注解即可 @Transactional
2.3.5.1Service层
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, int money) {
accountDao.out(outer, money);
//int i = 1 / 0;
accountDao.in(inner, money);
}
}
2.3.5.2spring配置
<!-- 4.1 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.2 将管理器交予spring
* transaction-manager 配置事务管理器
* proxy-target-class
true : 底层强制使用cglib 代理
-->
<tx:annotation-driven transaction-manager="txManager"/>
2.3.5.3事务详情配置
@Transactional(propagation=Propagation.REQUIRED , isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {