通过在弹簧数据 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