jpa查找数据库最新一条消息

主要字段说明:

pid:指导记录主键user_pid:用户主键competition_project_pid:用户作品 

Mysql表

Repository

/** * 指导记录 * @date 2020/3/24 9:26 */@Repositorypublic interface CompetitionProjectGuideRepository extends JpaRepository<CompetitionProjectGuide, String> { Optional<CompetitionProjectGuide> findFirstByCompetitionProjectPidOrderByUpdateAtDesc(Long pid);}

Service

 public Optional<CompetitionProjectGuide> findFirstByCompetitionProjectPidOrderByUpdateAtDesc(Long pid) { return repository.findFirstByCompetitionProjectPidOrderByUpdateAtDesc(pid); }

Controller

 @RequestMapping(method = {RequestMethod.GET}, value = "/task/project/guide") public ResponseEntity getGuide(Long pid) { Optional<CompetitionProjectGuide> guide = competitionProjectGuideService.findFirstByCompetitionProjectPidOrderByUpdateAtDesc(pid); guide.orElseThrow(() -> new BusinessException( HttpStatus.BAD_REQUEST, Translator.toLocale("guide.NotExists.message"))); return ResponseEntity.ok(guide); }

postman测试

控制台

总结

可以通过findFirst或者findTop来获取

相关文章