zoukankan      html  css  js  c++  java
  • SpringBoot整合mybatis-pagehelper实现分页

    1. 引入分页插件依赖

            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.3</version>
            </dependency>

    2. 配置yml

    # 分页插件配置
    pagehelper:
     helperDialect: mysql
     supportMethodsArguments: true

     

     

    3.分页数据封装到PagedGridResult类

    package com.qing.springbootmould.utils;
    
    import com.github.pagehelper.PageInfo;
    
    import java.util.List;
    
    /**
     * 
     * @Title: PagedGridResult.java
     * @Package com.imooc.utils
     * @Description: 用来返回分页Grid的数据格式
     * Copyright: Copyright (c) 2019
     */
    public class PagedGridResult {
        
        private int page;            // 当前页数
        private int total;            // 总页数    
        private long records;        // 总记录数
        private List<?> rows;        // 每行显示的内容
    
        public int getPage() {
            return page;
        }
        public void setPage(int page) {
            this.page = page;
        }
        public int getTotal() {
            return total;
        }
        public void setTotal(int total) {
            this.total = total;
        }
        public long getRecords() {
            return records;
        }
        public void setRecords(long records) {
            this.records = records;
        }
        public List<?> getRows() {
            return rows;
        }
        public void setRows(List<?> rows) {
            this.rows = rows;
        }
    
      //分页数据进行封装到PagedGridResult类,传给前端
    public static PagedGridResult setterPagedGrid(List<?> list, Integer page) { PageInfo<?> pageList = new PageInfo<>(list); PagedGridResult grid = new PagedGridResult(); grid.setPage(page); grid.setRows(list); grid.setTotal(pageList.getPages()); grid.setRecords(pageList.getTotal()); return grid; } }

    4. 分页的使用

    UserMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.qing.springbootmould.dao.UserMapper">
    
        <!--查询全部用户-->
        <select id="findAll" resultType="com.qing.springbootmould.pojo.User">
              select * from user
        </select>
    
    </mapper>

    dao层

    package com.qing.springbootmould.dao;
    
    import com.qing.springbootmould.pojo.User;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    @Mapper
    public interface UserMapper {
    
        User findUser(Integer id);
    
        List<User> findAll();
    }

    service层,在这进行分页

    UserService 接口
    public interface UserService {
    
        //分页接口
        PagedGridResult findAll(Integer page, Integer pageSize);
    }
    UserServiceImpl 实现类
    package com.qing.springbootmould.service.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.qing.springbootmould.dao.UserMapper;
    import com.qing.springbootmould.pojo.User;
    import com.qing.springbootmould.service.UserService;
    import com.qing.springbootmould.utils.PagedGridResult;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public PagedGridResult findAll(Integer page, Integer pageSize) {
            /**
             * page: 第几页
             * pageSize: 每页显示条数
             */
            PageHelper.startPage(page, pageSize);
            // PageHelper.startPage(page, pageSize)方法必须放在业务查询的上方
            List<User> userList = userMapper.findAll();
            PagedGridResult pagedGridResult = PagedGridResult.setterPagedGrid(userList, page);
            return pagedGridResult;
        }
    }
     TestController类
    package com.qing.springbootmould.controller;
    
    import com.qing.springbootmould.pojo.Result;
    import com.qing.springbootmould.pojo.User;
    import com.qing.springbootmould.service.UserService;
    import com.qing.springbootmould.utils.PagedGridResult;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import springfox.documentation.annotations.ApiIgnore;
    import sun.awt.SunHints;
    
    import java.util.List;
    import java.util.Map;
    
    @RestController
    public class TestController {
    
        @Autowired
        private UserService userService;
    
        // http://localhost:8080/query3?page=1&pageSize=2
        @GetMapping("/query3")
        public Result findUser3(@RequestParam(name="page") Integer page,
                                @RequestParam(name="pageSize") Integer pageSize){
            PagedGridResult gridResult = userService.findAll(page,pageSize);
            return Result.ok(gridResult);
        }
    }

    测试结果:

  • 相关阅读:
    C# 酒鬼买酒喝,瓶盖和空瓶子可以换新的酒
    C# 图结构操作
    C# 二叉堆
    Unity 单元测试(NUnit,UnityTestTools)
    Unity PlayerPrefs类进行扩展(整个对象进行保存)
    客户端操作判断以服务器时间为准
    C# 使用枚举获取对应的数组值时
    C# 实现简单状态机(参考代码)
    叶脉图案以及藤蔓生长算法在houdini里面的实现 Leaf Venation
    steering behaviors 转向行为 集群模拟 小结
  • 原文地址:https://www.cnblogs.com/Amywangqing/p/14819401.html
Copyright © 2011-2022 走看看