转账为demo,spring事务

spring 事务使用

  • 1. 业务代码
  • 2. xml配置
  • 3. 注解配置

1. 业务代码

  • 数据表结构
    转账为demo,spring事务_第1张图片
  • dao
package com.lovely.dao.impl;

import com.lovely.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author echo lovely
 * @date 2020/8/9 11:01
 */
public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;

    // 提供set方法 用于依赖注入
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // 出账
    public void out(String outMan, Double money) {
        String sql = "update account set money = money - ? where name = ?";
        jdbcTemplate.update(sql, new Object[]{money, outMan});
    }

    // 进账
    public void in(String inMan, Double money) {
        String sql = "update account set money = money + ? where name = ?";
        jdbcTemplate.update(sql, new Object[]{money, inMan});
    }
}

  • service
package com.lovely.service.impl;

import com.lovely.dao.AccountDao;
import com.lovely.service.AccountService;

/**
 * @author echo lovely
 * @date 2020/8/9 11:08
 */
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    /**
     * 转账业务具体实现 需要使用事务
     * @param outMan 转出人
     * @param inMan  收账人
     * @param money  money
     */
    public void transfer(String outMan, String inMan, Double money) {
        accountDao.in(inMan, money);
        System.out.println(1/0);
        accountDao.out(outMan, money);
    }
}

  • controller
package com.lovely.controller;

import com.lovely.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author echo lovely
 * @date 2020/8/9 11:12
 */
public class AccountController {

    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = app.getBean(AccountService.class);
        // 转账测试
        accountService.transfer("jack", "rose", 1000.0);
    }
}

  • 上面的bean的创建方式set注入,没搞注解

2. xml配置

  • 核心 事务管理器 对转账方法动态进行事务控制
    
    <bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <tx:advice id="txAdvice" transaction-manager="tranManager">
        
        <tx:attributes>
            <tx:method name="*"/>
            
        tx:attributes>
    tx:advice>

    
    <aop:config>
        
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lovely.service.impl.*.*(..))"/>
    aop:config>

3. 注解配置

  1. 注解配置
    
    <bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <tx:annotation-driven transaction-manager="tranManager">tx:annotation-driven>
  1. 事务注解
package com.lovely.service.impl;

import com.lovely.dao.AccountDao;
import com.lovely.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author echo lovely
 * @date 2020/8/9 11:08
 */

@Service("accountServiceImpl")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    /**
     * 转账业务具体实现 需要使用事务
     * @param outMan 转出人
     * @param inMan  收账人
     * @param money  money
     */
    @Transactional(timeout = 1)
    public void transfer(String outMan, String inMan, Double money) {
        accountDao.in(inMan, money);
        System.out.println(1/0);
        accountDao.out(outMan, money);
    }
}

你可能感兴趣的:(#,Spring)