一慎璧、簡(jiǎn)單分頁(yè)(只有一個(gè)查詢條件)
- 在Repository層將查詢語(yǔ)句的返回值類型設(shè)置為為Page類型床嫌,查詢參數(shù)中加入Pageable pageable,如:
@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);
}
- 在Service層中實(shí)例化Pageable對(duì)象,并指定currentPage(當(dāng)前頁(yè))炸卑、pageSize(每頁(yè)最大容量)既鞠,其中PageRequest.of為Spring Boot 2.0的方法,之前版本為new PageRequest(),如:
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));
}
- 在Controller層設(shè)置相應(yīng)的接口,由于currentPage規(guī)定從0開始五续,而前端通常返回的是從1開始洒敏,需要同步一下
@GetMapping("/ssh")
public ResultDao getSsh(@PathParam("userId") Integer userId,
@PathParam("currentPage") Integer currentPage,
@PathParam("pageSize") Integer pageSize) {
// 同步前端傳回的當(dāng)前頁(yè)參數(shù)
currentPage = currentPage - 1;
return cloudServerService.getSsh(userId, currentPage, pageSize);
}
二、多條件查詢分頁(yè)
多條件查詢采用的是以元模型概念為基礎(chǔ)的Criteria 查詢方法
- Repository層繼承JpaSpecificationExecutor<實(shí)體名>疙驾,并將返回類型改為Page類型凶伙,如:
@Repository
public interface CloudServerRepository extends JpaRepository<CloudServerDao, Integer>,JpaSpecificationExecutor<CloudServerDao> {
}
- 在Service層構(gòu)建Specification方法,具體實(shí)現(xiàn)見代碼它碎,同樣Spring Boot 2.0和Spring Boot 2.0之前的方法有差異函荣,簡(jiǎn)單介紹一下各字段的含義:
- root:查詢根显押,指實(shí)體(此處為CloudServerDao),root.get("userId")為獲取實(shí)體(此處為CloudServerDao)中的字段userId傻挂,第二個(gè)userId為函數(shù)的參數(shù)
- Predicate:定義查詢條件乘碑。Predicate 對(duì)象通過(guò)調(diào)用CriteriaBuilder的條件方法( equal,notEqual金拒, gt兽肤, ge,lt绪抛, le资铡,between,like等)創(chuàng)建幢码,具體方法自行搜索
spring boot 2.0推薦寫法
使用repository的findAll(specification, pageable)查詢即可
// 代碼通過(guò)userId和key兩個(gè)條件進(jìn)行查詢
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<>();
// 第一個(gè)userId為CloudServerDao中的字段笤休,第二個(gè)userId為參數(shù)
Predicate p1 = criteriaBuilder.equal(root.get("userId"),userId);
list.add(p1);
if (!key.equals(null)) {
// 此處為查詢serverName中含有key的數(shù)據(jù)
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);
- 在Controller層設(shè)置和簡(jiǎn)單查詢的配置一致
@GetMapping("/ssh")
public ResultDao getSsh(@PathParam("userId") Integer userId,
@PathParam("currentPage") Integer currentPage,
@PathParam("pageSize") Integer pageSize) {
// 同步前端傳回的當(dāng)前頁(yè)參數(shù)
currentPage = currentPage - 1;
return cloudServerService.getSsh(userId, currentPage, pageSize);
}