1. 程式人生 > 其它 >Springboot 2.3.5使用 pagehelper 實現分頁

Springboot 2.3.5使用 pagehelper 實現分頁

參考

  1. SpringBoot - 獲取Get請求引數詳解(附樣例:非空、預設值、陣列、物件)
  2. 如何使用 PageHelper 一篇部落格就夠了
  3. pageHelper詳解
  4. Mybatis中的resultType和resultMap
  5. 如何使用分頁外掛
  6. springboot實現MyBatis分頁
  7. Spring Boot:實現MyBatis分頁
  8. mybatis 中文文件 配置

正文

  1. pom.xml 節點 dependencies 下新增 pagehelper 依賴
<!--        分頁工具-->
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
  1. mapper.xml 新增 id 為 findByPaging 的查(直接查詢所有即可,因為在 Service中的 PageHelper.startPage(pageNum, pageSize); 會對這個語句下面的第一個查詢進行分頁)
    <select id="findByPaging" resultType="com.xxx.blog.entity.ArticleEntity" parameterType="map">
        select
        *
        from `articles`
    </select>
  1. ArticleMapper.java
@Mapper
public interface ArticleMapper {
    public ArticleEntity findById(Integer id);
    public List<ArticleEntity> findByPaging(Map param);
}
  1. ArticleService.java 新增 findByPaging 方法,將查詢出的資料轉換為 分頁型別,對控制器隱藏處理過程
@Service
public class ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    public ArticleEntity findById(Integer id){
        return articleMapper.findById(id);
    }

    /**
     * 分頁查詢
     * @param pageNum
     * @param pageSize
     * @param map
     * @return
     */
    public PageInfo findByPaging(Integer pageNum, Integer pageSize,Map map){
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleEntity> articleEntityPage = articleMapper.findByPaging(map);
        PageInfo pageInfo = new PageInfo(articleEntityPage);
        return pageInfo;
    }
}
  1. ArticleController.java 下新增 list 方法
@RestController
@RequestMapping("v1/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @GetMapping(value = "", produces = "application/json;charset=UTF-8")
    public ResponseEntity<CustomResponseStructureDto<PageInfo>> list(@RequestParam(name = "page_num", defaultValue = "1") Integer pageNum, @RequestParam(name = "page_size",defaultValue = "10") Integer pageSize){
        Map<String, String> map = new HashMap<>();
        PageInfo pageInfo = articleService.findByPaging(pageNum, pageSize, map);
        return ResponseEntity.ok(new CustomResponseStructureDto(0, pageInfo, "成功"));
    }

    @GetMapping(value = "{id}", produces = "application/json;charset=UTF-8")
    public ResponseEntity<CustomResponseStructureDto<ArticleEntity>> list(@PathVariable("id") Integer id){
        ArticleEntity article = articleService.findById(id);
        return ResponseEntity.ok(new CustomResponseStructureDto<ArticleEntity>(0, article, "成功"));
    }
}
  1. 測試 localhost:8080/v1/article?page_num=2&page_size=1
{
    "code": 0,
    "msg": "成功",
    "data": {
        "total": 2,
        "list": [
            {
                "id": 4,
                "title": "測試文章002",
                "introduction": "描述內容",
                "created_at": null,
                "updated_at": null,
                "content": "內容12131內容12131內容12131內容12131內容12131內容12131",
                "shows": 0,
                "likes": 0
            }
        ],
        "pageNum": 2,
        "pageSize": 1,
        "size": 1,
        "startRow": 2,
        "endRow": 2,
        "pages": 2,
        "prePage": 1,
        "nextPage": 0,
        "isFirstPage": false,
        "isLastPage": true,
        "hasPreviousPage": true,
        "hasNextPage": false,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 2
    }
}
如果覺得文章對您有幫助,希望您能 關注+推薦 哦