能够理解AOP的作用
能够完成AOP的入门案例
能够理解AOP的工作流程
能够说出AOP的五种通知类型
能够完成"测量业务层接口万次执行效率"案例
能够掌握Spring事务配置
Spring提供的事务管理是数据层的事务还是业务层的事务?

Spring整合Mybatis相关代码(依赖、JdbcConfig、MybatisConfig、SpringConfig)省略。
xxxxxxxxxx
public interface AccountDao { ("update tbl_account set money = money + #{money} where name = #{name}") void inMoney(("name") String name, ("money") Double money); ("update tbl_account set money = money - #{money} where name = #{name}") void outMoney(("name") String name, ("money") Double money);}public interface AccountService { /** * 转账操作 * @param out 传出方 * @param in 转入方 * @param money 金额 */ public void transfer(String out,String in ,Double money) ;}public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void transfer(String out,String in ,Double money) { accountDao.outMoney(out,money); int i = 1/0; accountDao.inMoney(in,money); }}
xxxxxxxxxx
public interface AccountService { //配置当前接口方法具有事务 public void transfer(String out,String in ,Double money) ;}
注意事项
说明:可以在JdbcConfig中配置事务管理器
xxxxxxxxxx
//配置事务管理器,mybatis使用的是jdbc事务public PlatformTransactionManager transactionManager(DataSource dataSource){ DataSourceTransactionManager dtm = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource); return transactionManager;}
注意事项
xxxxxxxxxx
("com.itheima")("classpath:jdbc.properties")({JdbcConfig.class,MybatisConfig.class})//开启注解式事务驱动public class SpringConfig {}
xxxxxxxxxx
(SpringJUnit4ClassRunner.class)(classes = SpringConfig.class)public class AccountServiceTest { private AccountService accountService; public void testTransfer() throws IOException { accountService.transfer("Tom","Jerry",100D); }}
什么是事务管理员,什么是事务协调员?

什么样的异常,Spring事务默认是不进行回滚的?

说明:对于RuntimeException类型异常或者Error错误,Spring事务能够进行回滚操作。但是对于编译器异常,Spring事务是不进行回滚的,所以需要使用rollbackFor来设置要回滚的异常。

xxxxxxxxxx
USE spring_db;CREATE TABLE tbl_log( id INT PRIMARY KEY AUTO_INCREMENT, info VARCHAR(255), createDate DATE);
xxxxxxxxxx
public interface LogService { //propagation设置事务属性:传播行为设置为当前操作需要新事务 void log(String out, String in, Double money);}public class LogServiceImpl implements LogService { private LogDao logDao; public void log(String out,String in,Double money ) { logDao.log("转账操作由"+out+"到"+in+",金额:"+money); }}public interface LogDao { ("insert into tbl_log (info,createDate) values(#{info},now())") void log(String info);}
xxxxxxxxxx
public class AccountServiceImpl implements AccountService { private AccountDao accountDao; private LogService logService; public void transfer(String out,String in ,Double money) { try{ accountDao.outMoney(out,money); int i = 1/0; accountDao.inMoney(in,money); }finally { logService.log(out,in,money); } }}
xxxxxxxxxx
public interface LogService { //propagation设置事务属性:传播行为设置为当前操作需要新事务 (propagation = Propagation.REQUIRES_NEW) void log(String out, String in, Double money);}
xxxxxxxxxx
(SpringJUnit4ClassRunner.class)(classes = SpringConfig.class)public class AccountServiceTest { private AccountService accountService; public void testTransfer() throws IOException { accountService.transfer("Tom","Jerry",50D); }}
