08-SpringBoot2.0完成业务集成事务管理

src\main\java\com\dev1\service\IAccountService.java

package com.dev1.service;

import com.dev1.entity.Account;

import java.util.List;

public interface IAccountService {
    public void addAccount(Account account);
    public void updateAccount(Account account);
    public void deleteAccountById(String id);
    public Account findAccountById(String id);
    public List<Account> findAccounts();
    public void transfer(String fromId,String toId,Double money);
}

src\main\java\com\dev1\service\impl\AccountServiceImpl.java

package com.dev1.service.impl;

import com.dev1.entity.Account;
import com.dev1.mapper.Account1Dao;
import com.dev1.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = false,isolation = Isolation.READ_COMMITTED,propagation= Propagation.REQUIRED)
public class AccountServiceImpl implements IAccountService {
    @Autowired
    Account1Dao dao;
    @Override
    public void addAccount(Account account) {
        dao.add(account);
    }

    @Override
    public void updateAccount(Account account) {
        dao.update(account);
    }

    @Override
    public void deleteAccountById(String id) {
        dao.delete(id);
    }

    @Override
    public Account findAccountById(String id) {
        return dao.findById(id);
    }

    @Override
    public List<Account> findAccounts() {
        return dao.findAll();
    }

    @Override
    public void transfer(String fromId, String toId, Double money) {
        //拿到账号,查询户主,余额
        Account from = dao.findById(fromId);
        Account to = dao.findById(toId);
        //判断余额是否足够
        if(from.getValue() >= money){
            from.setValue(from.getValue()-money);
            to.setValue(to.getValue()+money);
            //同步到数据库
            dao.update(from);
            //System.out.println(1/0);//系统报错
            dao.update(to);
        }else{
            throw  new RuntimeException("余额不足...搬砖");
        }
    }
}

src\test\java\com\dev1\service\impl\AccountServiceImplTest.java

package com.dev1.service.impl;

import com.dev1.service.IAccountService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class AccountServiceImplTest {

    @Autowired
    IAccountService accountService;
    @Test
    void transfer1() {
        accountService.transfer("1000","1002",10000D);
    }
    @Test
    void transfer2() {
        accountService.transfer("1000","1002",1D);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值