1.修改UserService
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
public UserRespDTO getUserByUsername(String username) {
LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
.eq(UserDO::getUsername,username);
UserDO userDO = baseMapper.selectOne(queryWrapper);
if (userDO != null) {
UserRespDTO result = new UserRespDTO();
BeanUtils.copyProperties(userDO, result);
return result;
} else {
// UserDO是null,无法找到用户
return null;
}
}
}
2.完善UserContorller的为空报错
@RestController
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
/**
*根据用户名查询用户信息
*/
@GetMapping("/api/shortlink/v1/user/{username}")
public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
UserRespDTO result = userService.getUserByUsername(username);
if(result==null){
return new Result<UserRespDTO>().setCode("-1").setMessage("用户查询为空");
}else {
return new Result<UserRespDTO>().setCode("0").setData(result);
}
}
}
3.注意UserController中返回值的变化
String->UserRespDTO->Result<UserRespDTO>