通過在彈簧資料 JPA 中使用自定義查詢傳遞 parmeter 來進行分頁
我使用 Spring Boot 1.4.4.RELEASE,使用 MySQL 作為資料庫和 Spring Data JPA 抽象來與 MySQL 一起工作。實際上,Spring Data JPA 模組使得在 Spring 引導應用程式中首先設定 Pagination 變得如此簡單。
場景暴露端點/學生/教室/ {id}。它將根據頁面和大小引數以及隨之傳遞的 classroomId 返回學生列表和其他分頁資訊(我們將在一分鐘內看到)。
首先,我建立一個域學生
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@NotNull
@Column(name = "rollnumber", nullable = false)
private Integer rollnumber;
@Column(name = "date_of_birth")
private LocalDate dateOfBirth;
@Column(name = "address")
private String address;
@ManyToOne(optional = false)
@NotNull
private Classroom classroom;
//getter and setter
}
學生與課堂有關
@Entity
@Table(name = "classroom")
public class Classroom {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "standard")
private String standard;
@Column(name = "section")
private String section;
@Column(name = "year")
private String year;
//getter && setter
}
我們有 RestController
@RestController
@RequestMapping("/api")
public class StudentResource {
private final StudentService studentService;
public StudentResource(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping("/students/classroom/{id}")
public ResponseEntity<Page<StudentDTO>> getAllStudentsBasedOnClassroom(@ApiParam Pageable pageable,@PathVariable Long id)
throws URISyntaxException {
Page<StudentDTO> page = studentService.findByClassroomId(id, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/students/classroom");
return new ResponseEntity<Page<StudentDTO>>(page, headers, HttpStatus.OK);
}
}
請注意,我們還沒有將 RequestParams 傳遞給我們的處理程式方法。當命中端點/ students / classroom / 1?page = 0&size = 3 時,Spring 會自動解析頁面和大小引數並建立一個 Pageable 例項。然後我們將這個 Pageable 例項傳遞給 Service 層,它將它傳遞給我們的 Repository 層。
服務類
public interface StudentService {
Page<StudentDTO> findByClassroomId(Long id,Pageable pageable);
}
service impl(這裡我是使用者 StudentMapper,使用 mapStruct 將 Class 轉換為 DTOby,或者我們可以手工製作)
@Service
@Transactional
public class StudentServiceImpl implements StudentService{
private final StudentRepository studentRepository;
private final StudentMapper studentMapper;
public StudentServiceImpl(StudentRepository studentRepository, StudentMapper studentMapper) {
this.studentRepository = studentRepository;
this.studentMapper = studentMapper;
}
@Override
public Page<StudentDTO> findByClassroomId(Long id, Pageable pageable) {
log.debug("Request to get Students based on classroom : {}", id);
Page<Student> result = studentRepository.findByClassroomId(id, pageable);
return result.map(student -> studentMapper.studentToStudentDTO(student));
}
}
這是對映器介面
@Mapper(componentModel = "spring", uses = {})
public interface StudentMapper{
StudentDTO studentToStudentDTO(Student student);
}
然後在 StudentRepository 我 worte 自定義方法
public interface StudentRepository extends JpaRepository<Student,Long> {
Page<Student> findByClassroomId(Long id, Pageable pageable);
}
然後它會給我們所有以下資訊和相應的資料
"last": false,
"totalElements": 20,
"totalPages": 7,
"size": 3,
"number": 0,
"sort": null,
"first": true,
"numberOfElements": 3