update 8.21
This commit is contained in:
6
pom.xml
6
pom.xml
@ -79,6 +79,12 @@
|
|||||||
<version>4.4.0</version>
|
<version>4.4.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- mybatis-plus -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
|
<version>3.5.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -2,8 +2,12 @@ package com.example.practice.controller;
|
|||||||
|
|
||||||
|
|
||||||
import com.example.practice.entity.User;
|
import com.example.practice.entity.User;
|
||||||
|
import com.example.practice.exception.MoneyNotEnoughException;
|
||||||
import com.example.practice.mapper.UserMapper;
|
import com.example.practice.mapper.UserMapper;
|
||||||
|
import com.example.practice.service.UserService;
|
||||||
|
import com.example.practice.service.impl.UserServiceImpl;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -13,7 +17,25 @@ import java.util.List;
|
|||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A->B转账
|
||||||
|
*/
|
||||||
|
@Transactional // 多条DML语句要么同时执行成功,要么同时失败
|
||||||
|
@GetMapping("transfer")
|
||||||
|
public boolean transfer(@RequestParam String fromAccount,
|
||||||
|
@RequestParam String toAccount,
|
||||||
|
@RequestParam int money) {
|
||||||
|
|
||||||
|
return userService.transfer(fromAccount, toAccount, money);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/queryAll")
|
@GetMapping("/queryAll")
|
||||||
public List<User> queryAll() {
|
public List<User> queryAll() {
|
||||||
@ -50,6 +72,15 @@ public class UserController {
|
|||||||
return userMapper.selectOne(id);
|
return userMapper.selectOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("pageQuery")
|
||||||
|
public List<User> pageQuery(@RequestParam int pageNum, @RequestParam int pageSize) {
|
||||||
|
int offset = (pageNum - 1) * pageSize;
|
||||||
|
return userMapper.pageQuery(pageSize, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
package com.example.practice.entity;
|
package com.example.practice.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,85 +45,5 @@ public class User {
|
|||||||
*/
|
*/
|
||||||
private Integer money;
|
private Integer money;
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "User{" +
|
|
||||||
"id=" + id +
|
|
||||||
", nickName='" + nickName + '\'' +
|
|
||||||
", phoneNumber='" + phoneNumber + '\'' +
|
|
||||||
", userAccount='" + userAccount + '\'' +
|
|
||||||
", userPassword='" + userPassword + '\'' +
|
|
||||||
", userRole='" + userRole + '\'' +
|
|
||||||
", money=" + money +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNickName() {
|
|
||||||
return nickName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNickName(String nickName) {
|
|
||||||
this.nickName = nickName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPhoneNumber() {
|
|
||||||
return phoneNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPhoneNumber(String phoneNumber) {
|
|
||||||
this.phoneNumber = phoneNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUserAccount() {
|
|
||||||
return userAccount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserAccount(String userAccount) {
|
|
||||||
this.userAccount = userAccount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUserPassword() {
|
|
||||||
return userPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserPassword(String userPassword) {
|
|
||||||
this.userPassword = userPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUserRole() {
|
|
||||||
return userRole;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserRole(String userRole) {
|
|
||||||
this.userRole = userRole;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getMoney() {
|
|
||||||
return money;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMoney(Integer money) {
|
|
||||||
this.money = money;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User(Long id, String nickName, String phoneNumber, String userAccount, String userPassword, String userRole, Integer money) {
|
|
||||||
this.id = id;
|
|
||||||
this.nickName = nickName;
|
|
||||||
this.phoneNumber = phoneNumber;
|
|
||||||
this.userAccount = userAccount;
|
|
||||||
this.userPassword = userPassword;
|
|
||||||
this.userRole = userRole;
|
|
||||||
this.money = money;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User() {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.example.practice.exception;
|
||||||
|
|
||||||
|
public class MoneyNotEnoughException extends RuntimeException {
|
||||||
|
|
||||||
|
public MoneyNotEnoughException() {
|
||||||
|
super("余额不足");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,12 +1,13 @@
|
|||||||
package com.example.practice.mapper;
|
package com.example.practice.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.example.practice.entity.User;
|
import com.example.practice.entity.User;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper // 生成当前UserMapper接口的实现类对象,并且把这个对象放到SpringIOC 容器中
|
@Mapper // 生成当前UserMapper接口的实现类对象,并且把这个对象放到SpringIOC 容器中
|
||||||
public interface UserMapper {
|
public interface UserMapper extends BaseMapper<User> {
|
||||||
|
|
||||||
List<User> selectAll();
|
List<User> selectAll();
|
||||||
|
|
||||||
@ -18,5 +19,8 @@ public interface UserMapper {
|
|||||||
|
|
||||||
int delete(Long id);
|
int delete(Long id);
|
||||||
|
|
||||||
|
List<User> pageQuery(int pageSize, int offset);
|
||||||
|
|
||||||
|
User selectByAccount(String userAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/main/java/com/example/practice/service/UserService.java
Normal file
10
src/main/java/com/example/practice/service/UserService.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.example.practice.service;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转账功能
|
||||||
|
*/
|
||||||
|
boolean transfer(String fromAccount, String toAccount, int money);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.example.practice.service.impl;
|
||||||
|
|
||||||
|
import com.example.practice.entity.User;
|
||||||
|
import com.example.practice.exception.MoneyNotEnoughException;
|
||||||
|
import com.example.practice.mapper.UserMapper;
|
||||||
|
import com.example.practice.service.UserService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean transfer(String fromAccount, String toAccount, int money) {
|
||||||
|
User fromUser = userMapper.selectByAccount(fromAccount);
|
||||||
|
Integer fromAccountMoney = fromUser.getMoney();
|
||||||
|
if (money > fromAccountMoney) throw new MoneyNotEnoughException();
|
||||||
|
|
||||||
|
// 更新转出账户的余额
|
||||||
|
fromUser.setMoney(fromAccountMoney - money);
|
||||||
|
userMapper.update(fromUser);
|
||||||
|
|
||||||
|
User toUser = userMapper.selectByAccount(toAccount);
|
||||||
|
Integer toUserMoney = toUser.getMoney();
|
||||||
|
|
||||||
|
// 跟新转入账户的余额
|
||||||
|
toUser.setMoney(toUserMoney + money);
|
||||||
|
userMapper.update(toUser);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -8,3 +8,7 @@ spring:
|
|||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
mapper-locations: classpath:mapper/*.xml
|
mapper-locations: classpath:mapper/*.xml
|
||||||
|
|
||||||
|
|
||||||
|
server:
|
||||||
|
port: 9090
|
||||||
|
@ -23,7 +23,13 @@
|
|||||||
update user set nickName = #{nickName}, phoneNumber = #{phoneNumber}, userAccount = #{userAccount}, userPassword = #{userPassword}, userRole = #{userRole}, money = #{money} where id = #{id}
|
update user set nickName = #{nickName}, phoneNumber = #{phoneNumber}, userAccount = #{userAccount}, userPassword = #{userPassword}, userRole = #{userRole}, money = #{money} where id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<select id="pageQuery" resultType="com.example.practice.entity.User">
|
||||||
|
select * from user limit #{pageSize} offset #{offset}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="selectByAccount" resultType="com.example.practice.entity.User">
|
||||||
|
select * from user where userAccount = #{userAccount}
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
||||||
<!--SQL映射文件-->
|
<!--SQL映射文件-->
|
Reference in New Issue
Block a user