小案例-转账

转账

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');

1.2.1 Dao层

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);
    }
}

1.2.2 service层

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);
    }
}

1.2.3 spring配置文件

 
    
        
        
        
        
    

    
    
        
    

    
    
        
    

1.2.4 测试

public class TestApp {
    @Test
    public void demo01(){
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        AccountService accountService = applicationContext.getBean("accountService",AccountService.class);
        accountService.transfer("jack","rose",100);
    }
}

1.3 手动管理事务

  • spring底层使用TransactionTemplate 事务模板进行操作
  • 操作
  1. 需要获得TransactionTemplate
  2. spring配置模板,并注入给service
  3. 模板需要注入事务管理器
  4. 配置事务管理器DataSourceTransactionManager,需要注入DataSource

1.3.1 修改service


    //需要spring注入模板
    private TransactionTemplate transactionTemplate;
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    @Override
    public void transfer(String outer, String inner, Integer money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                accountDao.out(outer,money);
                // 断电
//        int i=1/0;
                accountDao.in(inner,money);
            }
        });
    }

1.3.2 修改spring配置


    
    
        
        
    

    
    
        
    

    
    
        
    

1.4 工厂 bean 生成代理:半自动

  • spring提供 管理事务的代理工厂 bean TransactionProxyFactoryBean
  1. getBean() 获得代理对象
public class TestApp {
    @Test
    public void demo01(){
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        AccountService accountService = applicationContext.getBean("proxyAccountService",AccountService.class);
        accountService.transfer("jack","rose",100);
    }
}
  1. spring配置一个代理

    
        
        
        
        
            
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT
            
        

    

    
    
        
    

1.5 AOP 配置基于xml(重要)

  • 在spring xml配置aop自动生成代理,进行事务的管理
  1. 配置管理器
  2. 配置事务详情
  3. 配置aop

    
    
    
        
    
    
    
        
            
        
    


    
    
        
    

1.6 AOP 配置基于注解[重要]

  • 配置事务管理器,并将事务管理器交于 spring
  • 在目标类或目标方法添加注解即可 @Transactional
 
    
    
        
    

    
    

service 层

@Transactional
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(String outer, String inner, Integer money) {
 doInTransactionWithoutResult(TransactionStatus transactionStatus) {

        accountDao.out(outer,money);
        // 断电
//                int i=1/0;
        accountDao.in(inner,money); 
    }
}

1.7整合Junit

  1. 让Junit通知spring加载配置文件
  2. 让spring容器自动进行注入
  • 修改测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext2.xml")
public class TestApp {

    @Autowired //与junit整合,不需要再spring xml设置扫描
    private AccountService accountService;
    @Test
    public void demo01(){
 applicationContext.getBean("accountService", AccountService.class);
        accountService.transfer("jack","rose",100);
    }
}

2.整合web

1.tomcat 启动加载配置文件

    servlet-->init(ServletConfig) -->2
    
    filter--> ServletContextListener --> servletContext 对象监听
    spring 提供监听器 ContextLoaderListener -->web.xml ....
    如果只配置监听器,默认加载xml位置:/WEB-INF/applicationContext.xml
  1. 确定配置文件 文职,通过系统初始化参数
SercletContext 初始化参数web.xml
    
    contextConfigLocation
    classpath:applicationContext.xml
 
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
    
    
        org.springframework.web.context.ContextCleanupListener
    

你可能感兴趣的:(小案例-转账)