zoukankan      html  css  js  c++  java
  • 【SSM__分页】MyBatis 分页插件

    1、Maven配置

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>3.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.github.jsqlparser</groupId>
        <artifactId>jsqlparser</artifactId>
        <version>0.9.1</version>
    </dependency>
    

    2、在Mybatis配置xml中配置拦截器插件

    <!-- 
        plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
        properties?, settings?, 
        typeAliases?, typeHandlers?, 
        objectFactory?,objectWrapperFactory?, 
        plugins?, 
        environments?, databaseIdProvider?, mappers?
    -->
    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
            <!-- 该参数默认为false -->
            <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
            <!-- 和startPage中的pageNum效果一样-->
            <property name="offsetAsPageNum" value="true"/>
            <!-- 该参数默认为false -->
            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
            <property name="rowBoundsWithCount" value="true"/>
            <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
            <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
            <property name="pageSizeZero" value="true"/>
            <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
            <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
            <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
    

    这里的com.github.pagehelper.PageHelper使用完整的类路径。
    其他五个参数说明:

    1. 增加dialect属性,使用时必须指定该属性,可选值为oracle,mysql,mariadb,sqlite,hsqldb,postgresql,没有默认值,必须指定该属性。
    2. 增加offsetAsPageNum属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。当该参数设置为true时,使用RowBounds分页时,会将offset参数当成pageNum使用,可以用页码和页面大小两个参数进行分页。
    3. 增加rowBoundsWithCount属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。当该参数设置为true时,使用RowBounds分页会进行count查询。
    4. 增加pageSizeZero属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。当该参数设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是Page类型)。
    5. 增加reasonable属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。具体作用请看上面配置文件中的注释内容。

    3、Spring配置方法

    1、mybatis默认的xml配置

    参考 2、在Mybatis配置xml中配置拦截器插件

    2、使用spring的属性配置方式

    使用spring的属性配置方式,可以使用plugins属性像下面这样配置:

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="mapperLocations">
        <array>
          <value>classpath:mapper/*.xml</value>
        </array>
      </property>
      <property name="typeAliasesPackage" value="com.isea533.ssm.model"/>
      <property name="plugins">
        <array>
          <bean class="com.github.pagehelper.PageHelper">
            <property name="properties">
              <value>
                dialect=hsqldb
                reasonable=true
              </value>
            </property>
          </bean>
        </array>
      </property>
    </bean>
    

    属性配置按照上面的方式配置,每个配置独立一行即可。

    4、如何在代码中使用

    首先该分页插件支持以下两种调用方式:

    //第一种,RowBounds方式的调用
    List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(1, 10));
    
    //第二种,Mapper接口方式的调用,推荐这种使用方式。
    PageHelper.startPage(1, 10);
    List<Country> list = countryMapper.selectIf(1);
    

    1 RowBounds方式的调用

    List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(1, 10));
    
    //这种情况下也会进行物理分页查询
    List<Country> selectAll(RowBounds rowBounds);  
    

    不推荐,详情看文档

    2、PageHelper.startPage静态方法调用

        public void m1() {
            //从xml配置文件创建spring容器
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext*.xml");
            //从spring容器获取TbItemMapper的代理对象
            TbItemMapper mapper = applicationContext.getBean(TbItemMapper.class);
            //执行查询,分页
            TbItemExample tbItemExample = new TbItemExample();
            //分页处理
            PageHelper.startPage(1, 10);
            List<TbItem> tbItems = mapper.selectByExample(tbItemExample);
            //取商品列表
            for (TbItem item : tbItems) {
                System.out.println(item.getTitle());
            }
            //取分页信息
            PageInfo<TbItem> tbItemPageInfo = new PageInfo<>(tbItems);
            long total = tbItemPageInfo.getTotal();
            System.out.println("共有信息 " + total);
        }
    

    5、重要提示

    1、PageHelper.startPage方法重要提示

    只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select方法)方法会被分页。

    2、分页插件不支持带有for update语句的分页

    对于带有for update的sql,会抛出运行时异常,对于这样的sql建议手动分页,毕竟这样的sql需要重视。

    3、分页插件不支持关联结果查询

    原因以及解决方法可以看这里:
    http://my.oschina.net/flags/blog/274000
    分支插件不支持关联结果查询,但是支持关联嵌套查询。只会对主sql进行分页,嵌套的sql不会被分页

    6、作者链接

    对应于oschub的项目地址:http://git.oschina.net/free/Mybatis_PageHelper
    对应于github的项目地址:https://github.com/pagehelper/Mybatis-PageHelper
    Mybatis-Sample(分页插件测试项目):http://git.oschina.net/free/Mybatis-Sample
    Mybatis项目:https://github.com/mybatis/mybatis-3
    Mybatis文档:http://mybatis.github.io/mybatis-3/zh/index.html
    Mybatis专栏:
    • Mybatis示例
    • Mybatis问题集
    作者博客:
    http://my.oschina.net/flags/blog
    http://blog.csdn.net/isea533
    作者QQ: 120807756
    作者邮箱: abel533@gmail.com
    Mybatis工具群: 211286137 (Mybatis相关工具插件等等)

  • 相关阅读:
    Windows 10+Ubuntu 16.04在MBR分区上安装双系统(转)
    安装Ubuntu 16.04时出现:没有定义根文件系统,请到分区菜单修改
    Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后启动菜单的System Setup选项提示:can't find command "fwsetup"
    Ubuntu 16.04安装GTX960闭源驱动
    Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后没有Windows 10的启动菜单解决方法
    Ubuntu16.04安装deb文件时提示:此软件来自第三方且可能包含非自由组件
    完全卸载SQL Server 2008 R2(转)
    Windows超级卸载工具Total Uninstaller,能完全卸载.NET Framework
    Windows 10卸载Edge浏览器(不成功的别试了)
    完全卸载VS2015的方法
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/13369623.html
Copyright © 2011-2022 走看看