java后台学习_Java后台开发学习(1)——User接口

第一次写后台,跟着大佬学习java后台开发。第一个任务就是写user接口,就跟着大佬以前的代码,磕磕碰碰开始写了。

Model层

@Data

@Document(collection = "user")

public class User {

@NotNull

@Id

@Field("user_id")

private String userId = UUID.randomUUID().toString();

@NotNull

@Field("username")

private String username;

@NotNull

@Field("password")

private String password;

@NotNull

private String nickname;

@NotNull

private String role;

private String token;

}复制代码

@Data:在类名上方加上了@Data注解, 为类提供读写属性, 此外还提供了equals()、

hashCode()、toString() 方法,可以简写代码。

@Document(collection = "xxx"):实体类将会映射到数据库中的名为xxx的collection。

@Field:用于POST请求,提交单个数据。(这个暂时还不是很懂?)

DAO层

@Repository

public interface UserRepository extends MongoRepository {

User findByUsername(String username);

}复制代码

@Repository:注解在持久层中,具有将数据库操作抛出的原生异常翻译转化为spring的持久层异常的功能。

Service层

@Service

public class UserService {

@Autowired

private UserRepository userRepository;

public User save(User user) throws Exception {

user.setPassword(DigestUtils.sha256Hex(user.getPassword()));

return userRepository.save(user);

}

public User find(String userId) throws Exception {

return userRepository.findById(userId).orElse(null);

}

public Page findAll(Pageable pageable) throws Exception {

return userRepository.findAll(pageable);

}

public void delete(String userId) throws Exception {

User user = userRepository.findById(userId).orElse(null);

if (user == null) {

throw new IllegalArgumentException("No such user");

}

userRepository.delete(user);

}

}复制代码

@Service:业务逻辑层注解,这个注解只是标注该类处于业务逻辑层。

Controller层

@RestController

public class UserController {

@Autowired

private UserService userService;

@PostMapping("/user")

public Result saveUser(@RequestBody User user) throws Exception {

return new Result.Builder().setData(userService.save(user)).build();

}

@LoginRequired

@GetMapping("/user/{userId}")

public Result getUser(@PathVariable String userId, @CurrentUser User user) throws Exception {

return new Result.Builder().setData(userService.find(userId)).build();

}

@LoginRequired

@GetMapping("/user/{page}/{size}")

public Result getAll(@PathVariable Integer page, @PathVariable Integer size, @CurrentUser User user) throws Exception{

return new Result.Builder().setData(userService.findAll(PageRequest.of(page-1,size))).build();

}

@LoginRequired

@DeleteMapping("/user/{userId}")

public Result deleteUser(@PathVariable String userId, @CurrentUser User user) throws Exception {

userService.delete(userId);

return new Result.Builder().build();

}

}复制代码

@RestController:相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面。

看到大佬写的@Query还有findBy方法,还有一些注解,emmm打算这两天再研究一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值