使用MyBatisPlus+Spring实现声明式事务

目录

在pom.xml中导入依赖

配置数据源

创建实体类

创建Mapper

创建Service

AccountService

AccountServiceImpl

TestController


在pom.xml中导入依赖

导入MyBatisPlus相关依赖

				<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

配置数据源

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
     url: jdbc:mysql://localhost:3306/page_demo?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: 
    password: 

创建实体类

package demo.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("acount")
public class Account {
    @TableField
    private Long id;
    private String name;
    private  Float  money;

}

创建Mapper

package demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import demo.entity.Account;

public interface AccountMapper  extends BaseMapper<Account> {

}

创建Service

AccountService

package demo.service;

public interface AccountService {
    boolean transfer(String account,String target,Float money);
}

AccountServiceImpl

@Transactional(开启事务)
package demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import demo.entity.Account;
import demo.mapper.AccountMapper;
import demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl implements AccountService {
  @Autowired(required = false)
  private AccountMapper accountMapper;
    //开启事务
    @Transactional
    @Override
    public boolean transfer(String account, String target, Float money) {
        //1:获取汇款方
        QueryWrapper<Account> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("name",account);
        Account account1 = accountMapper.selectOne(queryWrapper1);

        //2:获取收款方
        QueryWrapper<Account> queryWrapper2= new QueryWrapper<>();
        queryWrapper2.eq("name", target);
        Account account2 = accountMapper.selectOne(queryWrapper2);


        //3:修改汇款方金额
        account1.setMoney(account1.getMoney()-money);
        int result1 = accountMapper.updateById(account1);

        //4:修改收款方金额
        account2.setMoney(account2.getMoney()+money);
        int result2 = accountMapper.updateById(account2);

        if (result1>0&&result2>0){
            return true;
        }else{
            return false;
        }

    }
}

TestController

package demo.controller;

import demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TextController {
    @Autowired
    private AccountService accountService;
    @RequestMapping("text")
    public String text(){
        boolean IsOk = accountService.transfer("A", "B", 200F);
        if (IsOk){
            return "转账成功";
        }else{
            return "转账失败";
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值