欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Data Jpa分页显示

程序员文章站 2022-04-25 11:16:49
...
        /**
	 * 分页查询电影动态信息
	 * @param page
	 * @param pageSize
	 * @return
	 */ 
        @Override
	public List<WebSiteInfo> list(WebSiteInfo webSiteInfo, Integer page, Integer pageSize) {
		Pageable pageable=new PageRequest(page, pageSize,Sort.Direction.DESC,"publishDate");
		Page<WebSiteInfo> pageWebSite=webSiteInfoRepository.findAll(new Specification<WebSiteInfo>() {
			
			@Override
			public Predicate toPredicate(Root<WebSiteInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate=cb.conjunction();
				if(webSiteInfo!=null){
					if(StringUtil.isNotEmpty(webSiteInfo.getInfo())){
						predicate.getExpressions().add(cb.like(root.get("info"), "%"+webSiteInfo.getInfo().trim()+"%"));
					}
				}
				return predicate;
			}
		}, pageable);
		return pageWebSite.getContent();
	}
        /**
	 * 获取总记录数
	 * @return
	 */
	@Override
	public Long getCount(WebSiteInfo webSiteInfo) {
		Long count=webSiteInfoRepository.count(new Specification<WebSiteInfo>() {

			@Override
			public Predicate toPredicate(Root<WebSiteInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate=cb.conjunction();
				if(webSiteInfo!=null){
					if(StringUtil.isNotEmpty(webSiteInfo.getInfo())){
						predicate.getExpressions().add(cb.like(root.get("info"), "%"+webSiteInfo.getInfo().trim()+"%"));
					}
				}
				return predicate;
			}
		});
		return count;
	}