其他分享
首页 > 其他分享> > JPA分页查询

JPA分页查询

作者:互联网

仓储层

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ResourceDockingRepository extends JpaRepository<ResourceDocking,String>  {

    Page<ResourceDocking> findAll(Specification<ResourceDocking> resourceDockingSpecification, Pageable pageable);
}

实现层

 @Override
    public Page<ResourceDocking> getResourceDockingList(getResourceDockingListParams param, Pageable pageable) {

        return resourceDockingRepository.findAll(new Specification<ResourceDocking>() {
            @Override
            public Predicate toPredicate(Root<ResourceDocking> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                // 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<String> name=root.get("systemName");
                Path<String> deptId=root.get("deptId");
                Path<String> state=root.get("dockingStatus");

                List<Predicate> list = new ArrayList<>();

                if(StringUtils.isNotEmpty(param.getKeyWord())){
                    list.add(criteriaBuilder.like(name,param.getKeyWord()));
                }
                if(StringUtils.isNotEmpty(param.getDeptId())){
                    list.add(criteriaBuilder.equal(deptId,param.getDeptId()));
                }
                if(StringUtils.isNotEmpty(param.getState())){
                    list.add(criteriaBuilder.equal(state,param.getState()));
                }

                Predicate[] arr = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(arr));
            }
        }, pageable);
    }

控制器

  @PostMapping("/dockingList")
    @RequiresPermissions("resourcedocking:list")
    @ApiOperation(value = "资源对接管理目录列表")
    public ResultData<DataCollectInit> dockingList(@Validated getResourceDockingListParams param){
        Pageable pageable = PageRequest.of(param.getPageNum(), param.getPageSize());
       Page<ResourceDocking> lie=  resourceDockingService.getResourceDockingList(param,pageable );
        return ResultData.success(lie);
    }

 

搜索

复制

标签:pageable,JPA,list,param,查询,springframework,criteriaBuilder,import,分页
来源: https://www.cnblogs.com/axu92312/p/16543194.html