Spring 学习Spring

Spring Boot2.0 JPA 实现分页(简单查询分页、复

2018-10-19  本文已影响114人  林_Han

一、简单分页(只有一个查询条件)

@Repository
public interface SshRepository extends JpaRepository<SshDao, Integer> {
    @Query("select s from ssh s where s.userId = :userId")
    Page<SshDao> selectAllByUserId(@Param("userId") Integer userId, Pageable pageable);
}
public ResultDao getSsh(Integer userId, Integer currentPage, Integer pageSize) {
        // spring boot 2.0推荐写法
        Pageable pageable = PageRequest.of(currentPage,pageSize);
        // spring boot 2.0 以前,2.0版本也适用,但是2.0版本推荐使用上面的方式
        // Pageable pageable = new PageRequest(currentPage,pageSize);
        
        return ResultUtil.unitedResult(ResultEnum.SUCCESS,sshRepository.selectAllByUserId(userId, pageable));
    }
@GetMapping("/ssh")
    public ResultDao getSsh(@PathParam("userId") Integer userId,
                            @PathParam("currentPage") Integer currentPage,
                            @PathParam("pageSize") Integer pageSize) {
        // 同步前端传回的当前页参数
        currentPage = currentPage - 1;
        return cloudServerService.getSsh(userId, currentPage, pageSize);
    }

二、多条件查询分页

多条件查询采用的是以元模型概念为基础的Criteria 查询方法
@Repository
public interface CloudServerRepository extends JpaRepository<CloudServerDao, Integer>,JpaSpecificationExecutor<CloudServerDao> {
}

spring boot 2.0推荐写法
使用repository的findAll(specification, pageable)查询即可

// 代码通过userId和key两个条件进行查询

public ResultDao getServer(String key, Integer userId, Integer currentPage, Integer pageSize) {
        Pageable pageable = PageRequest.of(currentPage,pageSize);
        Specification<CloudServerDao> specification = (Specification<CloudServerDao>) (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            // 第一个userId为CloudServerDao中的字段,第二个userId为参数
            Predicate p1 = criteriaBuilder.equal(root.get("userId"),userId);
            list.add(p1);
            if (!key.equals(null)) {
                // 此处为查询serverName中含有key的数据
                Predicate p2 = criteriaBuilder.like(root.get("serverName"),"%"+key+"%" );
                list.add(p2);
            }
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        return cloudServerRepository.findAll(specification, pageable);

↓ Spring Boot 2.0之前的写法,Spring Boot 2.0也适用,但是2.0版本推荐使用上面的方式 ↑

Specification<CloudServerDao> specification = new Specification<CloudServerDao>() {
            public Predicate toPredicate(Root<CloudServerDao> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                Predicate p1 = criteriaBuilder.equal(root.get("userId"),userId);
                list.add(p1);
                if (!key.equals(null)) {
                    Predicate p2 = criteriaBuilder.like(root.get("serverName"),"%"+key+"%" );
                    list.add(p2);
                }
                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            }
        };
        return cloudServerRepository.findAll(specification, pageable);

@GetMapping("/ssh")
    public ResultDao getSsh(@PathParam("userId") Integer userId,
                            @PathParam("currentPage") Integer currentPage,
                            @PathParam("pageSize") Integer pageSize) {
        // 同步前端传回的当前页参数
        currentPage = currentPage - 1;
        return cloudServerService.getSsh(userId, currentPage, pageSize);
    }
上一篇下一篇

猜你喜欢

热点阅读