目录
在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 "转账失败";
}
}
}