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);
        }
    }

    测试结果:

  • 相关阅读:
    while,dowhile,for循环和forin
    position:fixed;支持ie6,清除e6下抖动。
    数组concat()和slice()方法
    函数内arguments.callee的用法
    [转]十天学习PHP之第六天(PHP)学会添加删除修改数据 (20091125 14:26)
    [转]十天学习PHP之第三天(PHP)学会构建数据库
    [转]十天学习PHP之第二天(PHP)掌握php的流程控制
    php集成软件VertrigoServ(PHP安装)
    [转]十天学习PHP之第一天(PHP)基础知识
    PHP入门
  • 原文地址:https://www.cnblogs.com/Amywangqing/p/14819401.html
Copyright © 2011-2022 走看看