springboot框架实现分页查询
时间: 2023-07-17 15:56:17 浏览: 114
在Spring Boot框架中,可以使用Spring Data JPA或MyBatis Plus等库来实现分页查询。下面我将为你展示如何使用这两个库来实现分页查询。
使用Spring Data JPA实现分页查询:
1. 在你的数据访问对象(DAO)接口中,继承`org.springframework.data.repository.PagingAndSortingRepository`接口,如:
```java
import org.springframework.data.repository.PagingAndSortingRepository;
public interface CateRepository extends PagingAndSortingRepository<Cate, Long> {
}
```
2. 在你的服务类中,注入CateRepository,并使用`PageRequest`对象进行分页查询,如:
```java
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
@Service
public class CateService {
private final CateRepository cateRepository;
public CateService(CateRepository cateRepository) {
this.cateRepository = cateRepository;
}
public Page<Cate> listCate(int pageNo, int pageSize) {
PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
return cateRepository.findAll(pageRequest);
}
}
```
3. 在你的控制器中,调用CateService的listCate方法来获取分页结果,如:
```java
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CateController {
private final CateService cateService;
public CateController(CateService cateService) {
this.cateService = cateService;
}
@GetMapping("/cates")
public Page<Cate> getCates(@RequestParam(defaultValue = "0") int pageNo,
@RequestParam(defaultValue = "10") int pageSize) {
return cateService.listCate(pageNo, pageSize);
}
}
```
使用MyBatis Plus实现分页查询:
1. 在你的数据访问对象(DAO)接口中,继承`com.baomidou.mybatisplus.core.mapper.BaseMapper`接口,如:
```java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface CateMapper extends BaseMapper<Cate> {
}
```
2. 在你的服务类中,注入CateMapper,并使用`com.baomidou.mybatisplus.extension.plugins.pagination.Page`对象进行分页查询,如:
```java
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
@Service
public class CateService {
private final CateMapper cateMapper;
public CateService(CateMapper cateMapper) {
this.cateMapper = cateMapper;
}
public Page<Cate> listCate(int pageNo, int pageSize) {
Page<Cate> page = new Page<>(pageNo, pageSize);
return cateMapper.selectPage(page, null);
}
}
```
3. 在你的控制器中,调用CateService的listCate方法来获取分页结果,如:
```java
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CateController {
private final CateService cateService;
public CateController(CateService cateService) {
this.cateService = cateService;
}
@GetMapping("/cates")
public Page<Cate> getCates(@RequestParam(defaultValue = "1") int pageNo,
@RequestParam(defaultValue = "10") int pageSize) {
return cateService.listCate(pageNo, pageSize);
}
}
```
以上代码示例中,`pageNo`表示要查询的页码,`pageSize`表示每页的记录数。你可以根据实际需求进行调整。通过Spring Data JPA或MyBatis Plus提供的分页功能,可以轻松实现分页查询功能。
阅读全文