jpa知识点

1.分页 

public static PageRequest of(int page, int size, Direction direction, String... properties) {
        return of(page, size, Sort.by(direction, properties));
    }



官方API说明: since 2.0, use of(...) instead,2.0版本后,使用 of(...) 方法代替 PageRequest(...)构造器

官方地址:https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/PageRequest.html

支持多条件排序的,比如Pageable page = PageRequest.of(1, 10, Sort.Direction.DESC, "id","name",...);

private Pageable buildPageable(Param param) {
        //param pageNo,pageSize,sortOrder,sortField
        // 添加一个名为sort的field并根据他排序,用于把负数数据始终排在最后面
        String sortField = "sort";
        Sort sort = Sort.by(Direction.ASC, sortField);
        sort = sort.and(Sort.by(Constants.ACS.equals(param.getSortOrder()) ? Direction.ASC : Direction.DESC,param.getSortField()));
        // 处理分页
        Pageable pageable= PageRequest.of(param.getPageNo() - 1, param.getPageSize(), sort);
       
        return pageable;
    }

2.@Query之countQuery

@Query(value = "select * from "
            + "(select *, 1 as sort from student_task where data_type = ?1 and value >= 0 union "
            + "select *, 2 as sort from student_task where data_type = ?1 and value < 0) A",
            countQuery = "select count(*) from "
                    + "(select *, 1 as sort from student_task where data_type = ?1 and value >= 0 union "
                    + "select *, 2 as sort from student_task where data_type = ?1 and value < 0) A",
            nativeQuery = true)
    Page queryStudentTaskList(String dataType, Pageable page);

countQuery 就是分页的总条数,后面的条件需跟value的where条件保持一致。

你可能感兴趣的:(Jpa,分页)