使用Mybatis-PageHelper 分页

框架地址https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

 参考文档地址https://blog.csdn.net/she_lock/article/details/79975907

  1. pom依赖
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.1.1</version>
</dependency>

在springboot中使用上面方法正常,但不知道为何使用下面方式不生效

 <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.0.0</version>
  </dependency>
  1. 在mybatis的配置文件中添加拦截器
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->  
    <settings>  
        <!-- 全局映射器启用缓存 -->  
        <setting name="cacheEnabled" value="true" />  
        <!-- 查询时,关闭关联对象即时加载以提高性能 -->  
        <setting name="lazyLoadingEnabled" value="true" />  
        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->  
        <setting name="aggressiveLazyLoading" value="false" />  
        <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->  
        <setting name="multipleResultSetsEnabled" value="true" />  
        <!-- 允许使用列标签代替列名 -->  
        <setting name="useColumnLabel" value="true" />  
        <!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->  
        <!-- <setting name="useGeneratedKeys" value="true" /> -->  
        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->  
        <setting name="autoMappingBehavior" value="FULL" />  
        <!-- 对于批量更新操作缓存SQL以提高性能 -->  
        <setting name="defaultExecutorType" value="BATCH" />  
        <!-- 数据库超过25000秒仍未响应则超时 -->  
        <setting name="defaultStatementTimeout" value="25000" />
        <!-- 打印查询语句 生产中可注释-->
         <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    
    <!-- 
    In the configuration file, 
    plugins location must meet the requirements as the following order:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- config params as the following -->
        <!-- <property name="param1" value="value1"/> -->
    </plugin>
</plugins>

</configuration>
  1. 在配置文件中配置配置文件
# MyBatis配置
mybatis:
mapperLocations:classpath:/mapper/*.xml
configLocation:classpath:/mybatis-config.xml
# Mybatis 配置(持久层框架自己选择)
#mybatis.typeAliasesPackage=com.sinstar.cpmanage.bean
mybatis.mapperLocations=classpath:mapper/*.xml

或者是 .yml文件

# MyBatis配置
mybatis:
  mapperLocations: classpath*:/mapper/**/*.xml
  configLocation: classpath:/mybatis-config.xml

  1. 在service中使用
    public List<Courseinfo> getCourseByPage(Integer rows, Integer currentPage) {
        PageHelper.startPage(currentPage, rows);
        List<Courseinfo> courseinfoList = null;
        courseinfoList=courseinfoMapper.getAllCourses();

        return courseinfoList;
    }

在Controller中

courseinfoList = courseService.getCourseByPage(rows, currentPage);
PageInfo<Courseinfo> pageInfo = new PageInfo<>(courseinfoList);

直接这样得到的pageInfo 里面就包含了所有的分页信息。 在mapper中,sql语句按原来的写,无需任何变化


这行代码会通过拦截器来处理sql语句,所以这个框架是非常好用的。 而 PageInfo 中也会自动设置一些相关值,牛逼

PageHelper.startPage(currentPage, rows);

返回结果示例:

"data": {
    "pageNum": 1,
    "pageSize": 2,
    "size": 2,
    "startRow": 1,
    "endRow": 2,
    "total": 9,
    "pages": 5,
    "list": [
      {
        "courseId": 1,
        "courceName": "大学英语",
        "type": "word",
        "srcUrl": "http://video.17yiqixiu.com/%E7%88%B1%E6%98%AF%E4%BA%BA%E9%97%B4%E6%9C%80%E4%BC%9F%E5%A4%A7%E7%9A%84%E5%8A%9B%E9%87%8F.mp4",
        "imageUrl": "http://img34.ddimg.cn/4/5/9198094-1_o.jpg",
        "createTime": 1519980188000,
        "author": "1",
        "description": "大学英语",
        "hasLearn": false,
        "hasCollect": false
      },
      {
        "courseId": 13,
        "courceName": "土木制造22",
        "type": "土木工程",
        "srcUrl": "http://video.17yiqixiu.com/%E7%88%B1%E6%98%AF%E4%BA%BA%E9%97%B4%E6%9C%80%E4%BC%9F%E5%A4%A7%E7%9A%84%E5%8A%9B%E9%87%8F.mp4",
        "imageUrl": "http://img34.ddimg.cn/4/5/9198094-1_o.jpg",
        "createTime": 1520326186000,
        "author": "1",
        "description": "sinstar",
        "hasLearn": false,
        "hasCollect": false
      }
    ],
    "prePage": 0,
    "nextPage": 2,
    "isFirstPage": true,
    "isLastPage": false,
    "hasPreviousPage": false,
    "hasNextPage": true,
    "navigatePages": 8,
    "navigatepageNums": [
      1,
      2,
      3,
      4,
      5
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 5,
    "firstPage": 1,
    "lastPage": 5
  }






 

猜你喜欢

转载自blog.csdn.net/sinstar1/article/details/82150896