spring data jpa 之分页查询

匿名 (未验证) 提交于 2019-12-02 22:56:40
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zc_ad/article/details/83576650

JPA的分页查询确实使用起来确实很简单,但理解起来有点困难,此处只是实现JPA分页的代码块。

定义实体类:

 @Entity @Table(name = "t_pub_info") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class InfoPO implements Serializable {      @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id; 	     @Column(name ="name")     private String name;      @Column(name = "priority")     private String priority;      @Column(name = "pub_time")     private Date pubTime;      @Column(name = "depict")     private String depict; 	 	@Column(name ="city_id") 	private Long cityId;      @Column(name = "status")     private String status;      Getter...     Setter... } 

定义DAO:

 public interface InfoDao extends JpaRepository<InfoPO, Long>, JpaSpecificationExecutor<InfoPO> {  }

定义接口:

 @SuppressWarnings(value = "unused") @RestController @RequestMapping(value = "/info") public class InfoController { 	     @Autowired     InfoService infoService;      @GetMapping(value = "/list")     Object queryInfoList(@RequestParam(value = "pageNo", required = false, defaultValue = "0") Integer pageNo,                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,                           @RequestParam(value = "status", required = false) String status,                           @RequestParam(value = "priority", required = false) String priority,                           @RequestHeader("city_id")Integer cityId) {         return infoService.queryInfoList(pageNo, pageSize, priority,cityId,String status);     } } 

定义service

 public interface InfoService {     Object queryinfoList(Integer pageNo, Integer pageSize, String priority,Integer cityId,String status); } 

实现service

 @Service public class InfoServiceImpl implements InfoService {     protected static final int PAGE_SIZE = 5;      @Autowired     InfoDao infoDao;      public Object queryInfoList(Integer pageNo, Integer pageSize, String priority,Integer cityId,String status) {          //验证city_id         if(cityId == null){             throw new Exception("city id necessary");         }          Integer currentPage = pageNo  == null ? 0 : pageNo;          Page<InfoPO> page = infoDao.findAll((Root<InfoPO> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {             //添加分页筛选条件             List<Predicate> predicates = new ArrayList<>();             if (priority != null) {                 predicates.add(cb.equal(root.get("priority"), priority));             }             if (status != null) {                 String statusName = status;                 if (StringUtils.isNotBlank(statusName)) {                     predicates.add(cb.equal(root.get("status"), statusName));                 }             }              if (cityId != null) {                 predicates.add(cb.equal(root.get("cityId"), cityId));             }              return cb.and(predicates.toArray(new Predicate[]{}));         }, new PageRequest(currentPage, pageSize, new Sort(Sort.Direction.DESC, "pubTime")));          		//获取页面数据以及分页数据 		List<InfoPO> infos = page.getContent();         Integer totalPage = page.getTotalPages();         Long totalElement = page.getTotalElements();                 return infos;     } }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!