• SpringBoot整合PageHelper


    SpringBoot整合PageHelper分页插件

    本案例实用基础环境为SpringBoot+Mybatis

    第一步:导入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
        </parent>
    
        <groupId>cn.blogsx</groupId>
        <artifactId>spring_pagehelper</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <!-- web功能起步依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--整合PageHelper-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.5</version>
            </dependency>
            <!--  mybatis依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!-- druid数据库连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.9</version>
            </dependency>
            <!-- mysql驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    
    </project>
    

    数据库文件:

    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    
    DROP TABLE IF EXISTS `books`;
    CREATE TABLE `books`  (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '书籍id',
      `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT '书籍名称',
      `author` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT '书籍作者',
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = MyISAM AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
    
    INSERT INTO `books` VALUES (1, '三国演义', '罗贯中');
    INSERT INTO `books` VALUES (2, '水浒传', '施耐庵');
    INSERT INTO `books` VALUES (3, '西游记', '吴承恩');
    INSERT INTO `books` VALUES (4, '红楼梦', '曹雪芹');
    INSERT INTO `books` VALUES (5, '周公解梦', '周公');
    
    SET FOREIGN_KEY_CHECKS = 1;
    
    

    配置文件:

    server.port=8080
    
    # 数据库连接相关配置
    spring.datasource.url=jdbc:mysql:///springboot-mybatis?characterEncoding=utf8&useSSL=true
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.username=root
    spring.datasource.password=root
    
    # MyBatis注解形式扫描实体类路径
    mybatis.type-aliases-package=cn.blogsx.entity
    
    # MyBatis XML形式配置文件路径
    mybatis.config-locations=classpath:mybatis/mybatis-config.xml
    mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
    
    #pagehelper配置
    pagehelper.helper-dialect=mysql
    pagehelper.reasonable=true
    pagehelper.support-methods-arguments=true
    pagehelper.params=count=countSql
    

    第二步:编写相关类、包括controller层、service层、mapper层

    实体类:

    package cn.blogsx.entity;
    
    public class Book {
        private Integer id;
        private String name;
        private String author;
    
       //此处省去gette setter
    }
    
    

    Mapper接口

    package cn.blogsx.mapper;
    
    import cn.blogsx.entity.Book;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    @Mapper
    public interface IBookMapper {
        List<Book> getAllBooks(); //获取所有书籍
    }
    
    

    Mapper对应的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="cn.blogsx.mapper.IBookMapper">
        <select id="getAllBooks" resultType="cn.blogsx.entity.Book">
            select id,name,author from books
        </select>
    </mapper>
    

    Service层编写,此处为实现分页的关键代码:

    package cn.blogsx.service;
    
    import cn.blogsx.entity.Book;
    import com.github.pagehelper.PageInfo;
    
    //接口
    public interface BookService {
        /**
         * PageHelper分页接口
         * @param pageNum
         * @param pageSize
         * @return PageInfo类
         */
        public PageInfo<Book> allBook(int pageNum, int pageSize);
    }
    

    实现分页接口的实现类:

    package cn.blogsx.service.impl;
    
    @Service
    public class BookServiceImpl implements BookService {
        @Autowired
        private IBookMapper iBookMapper;
    
        @Override
        public PageInfo<Book> allBook(int pageNum, int pageSize) {
            //开启分页
            PageHelper.startPage(pageNum,pageSize);  //pageNum为页码,pageSize为页面大小
            List<Book> bookList = iBookMapper.getAllBooks();
            PageInfo<Book> pageInfo = new PageInfo<Book>(bookList);
            return pageInfo;
        }
    }
    

    controller层:

    package cn.blogsx.controller;
    
    @RestController
    public class BookController {
    
        @Autowired
        private BookService bookService;
        
        @RequestMapping("/books")` `
        public PageInfo<Book> books(Integer pageNum, Integer pageSize) {
            return bookService.allBook(pageNum,pageSize);
        }
    }
    
    

    第三步:传参测试:

    http://localhost:8080/books?pageNum=1&pageSize=2

    返回值含义:

    PageInfo.list 结果集
    PageInfo.pageNum 当前页码
    PageInfo.pageSize 当前页面显示的数据条目
    PageInfo.pages 总页数
    PageInfo.total 数据的总条目数
    PageInfo.prePage 上一页
    PageInfo.nextPage 下一页
    PageInfo.isFirstPage 是否为第一页
    PageInfo.isLastPage 是否为最后一页
    PageInfo.hasPreviousPage 是否有上一页
    PageHelper.hasNextPage 是否有下一页
  • 相关阅读:
    goland 创建快捷方式 Linux
    Manjaro-Linux感觉蛮有用的系统问题处理
    Rust 语言九九乘法表
    manjaro 显卡驱动
    Archlinux启动时声音总是静音,需要手动调整音量的解决步
    错误: 无法找到目标文件分割所需的 strip 二进制文件。
    rust 安装添加代理
    2.开启aria2c多线程加速下载
    包无效或损坏(PGP签名)
    zsh的自动完成辅助工具:oh-my-zsh
  • 原文地址:https://www.cnblogs.com/sxblog/p/13674997.html
Copyright © 2020-2023  润新知