@PostMapping
@ApiOperation("新增用户")
public Result saveUser(@RequestBody UserDTO userDTO) {
User user = new User();
BeanUtil.copyProperties(userDTO, user);
userService.save(user);
return Result.success();
}
在 service 和 mapper 层都不需要写任何东西,因为 IService 已提供具体实现
根据 id 扣减余额(自定义)
controller
java复制代码
@ApiOperation("根据id扣减余额")
@PutMapping("/{id}/deduction/{money}")
public Result balanceMinusById(@PathVariable("id") Long id,@PathVariable("money") Double money){
userService.balanceMinusById(id,money);
return Result.success();
}
service
先用内置的 getById 获得 user,然后进行非法判断
用 updateWrapper 封装条件
update 有两种重载方法:
其中一种方法直接传入装饰器 updateWrapper,这种方法需要在构造的时候使用 set,指定要更新的操作,通过 User 的 get 方法获得余额
java复制代码
public void balanceMinusById(Long id, Double money) {
User user = getById(id);
if (user == null) {
throw new RuntimeException("用户不存在");
}
if (user.getBalance() < money) {
throw new RuntimeException("用户余额不足");
}
UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>()
.set("balance", user.getBalance() - 200)
.eq("id", id);
update(updateWrapper);
}
循环依赖(Circular Dependency)是指两个或多个对象(类、模块、Bean 等)相互依赖,形成一个循环的依赖关系。具体来说,如果对象 A 依赖对象 B,而对象 B 又依赖对象 A,这种情况就会导致循环依赖问题。循环依赖可能导致系统无法正确初始化,尤其是在依赖注入框架(如 Spring)中,会抛出异常,无法实例化相关的对象
加入一张 address 表
在 UserVO 中加入
java复制代码
private List<AddressVO> addressList;
1.9.1 查询单个用户及其地址
现在对代码进行改造,根据id查询用户,返回用户信息,也包括对应的 地址 信息
注意,这里很重要
需要操作什么实体类,从而访问对应的表,需要 在mapper 创建一个对应的接口
这个接口要继承 BaseMapper,这样才具有 mp 的方法
这里不用加 @Mapper的原因是在启动类上加了扫描Mapper的注解:
controller
java复制代码
@GetMapping("/{id}")
@ApiOperation("根据id查询用户")
public Result<UserVO> getUserById(@PathVariable Long id) {
User user = userService.getById(id);
UserVO userVO = new UserVO();
// log.info("UserVO.class:\n"+String.valueOf(UserVO.class)); // class com.wyn.springcloud.pojo.vo.UserVO
return Result.success(BeanUtil.copyProperties(user, UserVO.class)); // 反射 UserVO.class
}
service
这里使用 Db 的 lambdaQuery 方法,传参是查询的实体类的 字节码(反射)
根据 userid 查询,并且转为 list
java复制代码
public UserVO queryUserAndAddress(Long id) {
User user = getById(id);
if (user == null || user.getStatus() == 0) {
throw new RuntimeException("用户异常!");
}
List<Address> addressList = Db.lambdaQuery(Address.class)
.eq(Address::getUserId, id)
.list();
UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
userVO.setAddressList(BeanUtil.copyToList(addressList, AddressVO.class));
return userVO;
}
/**
* batch query [users] and [addresses] by ids
*
* @param ids
* @return
*/
public List<UserVO> queryUsersAndAddress(List<Long> ids) {
List<User> users = listByIds(ids);
if (users.isEmpty()) {
throw new RuntimeException("user table is null!");
}
users.forEach(user -> {
if (user.getStatus() == 0) {
users.remove(user);
}
});
if (users.isEmpty()) {
throw new RuntimeException("users are frozen");
}
// HashMap<Long,List<Address>> addressListMap = new HashMap<>();
// We don't use HashMap here, because the hash table is 1 to 1, and here it's 1 to many
List<UserVO> userVOList = new ArrayList<>();
for (User user : users) {
List<Address> addressList = Db.lambdaQuery(Address.class)
.eq(Address::getUserId, user.getId())
.list();
UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
List<AddressVO> addressVOList = BeanUtil.copyToList(addressList, AddressVO.class);
userVO.setAddressList(addressVOList);
userVOList.add(userVO);
}
return userVOList;
}