springboot +mybatis分页插件PageHelper

1.问题描述

JAVA界ORM的两位大佬Hibernate和Mybatis,hb自带分页(上手挺快,以前用了好几年hb,后期运维及优化快疯了),mybatis没有分页功能,需要借助第三方插件来完成,比较流行的三方框架:PageHelper,今天结合springboot做下介绍,直接贴线上配置,保证可用(如有遗漏,朋友们可以指正下)。

2. 解决方案

2.1 配置项目pom.xml

        <!--分页-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.8</version>
        </dependency>

2.2 配置PageHelper参数

方式比有很多,这里只介绍使用过的两种吧。

方式一,使用标签Configuration:

@Configuration

public class PageHelperConfig {

? @Bean

? public PageHelper getPageHelper(){

? PageHelper pageHelper=new PageHelper();

? Properties properties=new Properties();

? properties.setProperty("helperDialect","mysql");

? properties.setProperty("reasonable","true");

? properties.setProperty("supportMethodsArguments","true");

? properties.setProperty("params","count=countSql");

? pageHelper.setProperties(properties);

? return pageHelper;

? }

}

方式二,因为我们当时mybatis插件自动生成了一个xml,就直接放xml里了。跟sp的无配置文件概念是不是有点冲突? 这里简单啰嗦一句,其实软件的过程有点三十年河东三十年河西的味道,以前是要配置集中化,都由几个中心配置文件管理;现在是去中心化,全部通过标签来定义;看个人习惯吧,我们项目中除了application.yml也还有两个配置文件,一个是数据库和事务aop那个xml,从以前项目中移过来的,懒的再写了,直接在启动类上引用下就行了,也很简单。

<?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>
    <!--设置-->
    <settings>
        <!-- 全局映射器启用缓存 -->
        <setting name="cacheEnabled" value="false"/>
        <!-- 查询时,关闭关联对象即时加载以提高性能 -->
        <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以提高性能  但是返回id有问题-->
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <!-- 数据库超过36000秒仍未响应则超时 -->
        <setting name="defaultStatementTimeout" value="36000"/>
        <!-- 日志使用logback实现 -->
        <!--<setting name="logImpl" value="LOGBACK"/>-->
        <!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>

    </settings>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">

            <property name="offsetAsPageNum" value="false"/>
            <property name="rowBoundsWithCount" value="false"/>
            <property name="pageSizeZero" value="true"/>
            <property name="reasonable" value="false"/>
            <property name="supportMethodsArguments" value="false"/>
            <property name="returnPageInfo" value="none"/>
        </plugin>
    </plugins>
</configuration>

2.3 项目中使用

使用示例:

public Object getAppUsers(ShareAppsVO shareAppsVO) {
//分页就这一行就可以了两个参数,一是当前页,一是每页显示的条数
        PageHelper.startPage(shareAppsVO.getCurrentPage(), shareAppsVO.getPageSize());
           List<DmaAppUser> dmaAppUsers = dmaAppUserMapper.getAppUsers(shareAppsVO);
        PageInfo<DmaAppUser> pageInfo = new PageInfo<DmaAppUser>(dmaAppUsers);

        Page page = new Page();
        page.setData(dmaAppUsers);
        //这里注意下,正常情况下,只需要上一页下一页的话,就不用PageInfo,info主要用于获取总页数了。PageHelper自带的类
        page.setTotal(pageInfo.getTotal());
        return page;
    }

注意: PageHelper.startPage(shareAppsVO.getCurrentPage(), shareAppsVO.getPageSize()); 需要放到执行mybatis代码前一页,否则会有问题,网上也有解决该问题的方案,该场景没碰到就不测试了。



springboot +mybatis分页插件PageHelper

原文地址:https://www.cnblogs.com/ruanjianlaowang/p/11182684.html

时间: 2025-01-07 22:38:39

springboot +mybatis分页插件PageHelper的相关文章

Mybatis分页插件PageHelper的使用

Mybatis分页插件 - PageHelper支持任何复杂的单表.多表分页.使用方法如下: 1.导入jar包 2.在Maven中添加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> 3.在mybatis配置文件中配置插件 <?xml version="1.

Mybatis分页插件-PageHelper的使用

转载:http://blog.csdn.net/u012728960/article/details/50791343 Mybatis分页插件-PageHelper的使用 怎样配置mybatis这里就不提了,我来说说我配置这个分页插件的过程吧. 下载JAR包 分页插件pagehelper.jar: https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/ http://repo

Mybatis分页插件PageHelper使用

一. Mybatis分页插件PageHelper使用  1.不使用插件如何分页: 使用mybatis实现: 1)接口: List<Student> selectStudent(Map<String, Object> map); 2)mapper.xml: <select id="selectStudent" resultMap="BaseResultMap" parameterType="java.util.Map"

基于Mybatis分页插件PageHelper

基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 --><dependency>    <groupId>com.github.pagehelper</groupId>    <artifactId>pagehelper</artifactId>    <version>4.0.

Mybatis分页插件PageHelper的配置和使用方法

http://www.cnblogs.com/kangoroo/p/7998433.html 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分页 一次性请求数据表格中的所有记录(ajax),然后在前端缓存并且计算count和分页逻辑,一般前端组件(例如dataTable)会提供分页动作. 特点是:简单,很适合小规模的web平台:当数据量大的时候会产生性能问题,在查询和网络传输的时间会很长. 后端分页 在aj

MyBatis 分页插件PageHelper 后台报错

今天遇到一个问题,使用MyBatis 分页插件PageHelper 进行分页后,能正常返回正确的结果,但后台却一直在报错 net.sf.jsqlparser.parser.ParseException 具体错误信息如下所示: net.sf.jsqlparser.JSQLParserException at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:56) at com.github.pagehelp

使用Mybatis分页插件PageHelper时的分页问题

使用Mybatis分页插件PageHelper时的分页问题 1对1查询,分页正常 1对多查询,如使用左右连接查询则会导致结果的总记录条数,子记录条数会叠加到主记录条数,导致数据不对称. 总结:使用mybatis时,在一对多的查询并且需要分页的时候需要使用子查询形式. 1) 主记录的resultMap <resultMap id="artWorkMap" type="Artwork"> <id column="id" jdbcTy

mybatis分页插件PageHelper的使用(转)

Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/HowToUse.markdown 我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 

mybatis分页插件pagehelper

Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/HowToUse.markdown 我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 jsqlparser-0.9.4.jar 添加如下依赖: <dep