Spring整合Mybatis 之分页插件使用

【分页插件项目中的正式代码一共有个5个Java文件,这5个文件的说明如下】

  • Page<E>[必须]:分页参数类,该类继承ArrayList,虽然分页查询返回的结果实际类型是Page<E>,但是可以完全不出现所有的代码中,可以直接当成List使用。返回值不建议使用Page,建议仍然用List。如果需要用到分页信息,使用下面的PageInfo类对List进行包装即可。
  • PageHelper[必须]:分页插件拦截器类,对Mybatis的拦截在这个类中实现。
  • PageInfo[可选]:Page<E>的包装类,包含了全面的分页属性信息。
  • SqlParser[可选]:提供高效的count查询sql。主要是智能替换原sql语句为count(*),去除不带参数的order by语句。需要jsqlparser-0.9.1.jar支持。
  • SqlUtil[必须]:分页插件工具类,分页插件逻辑类,分页插件的主要实现方法都在这个类中。

使用步骤(基于maven):
 
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、配置PageHelper拦截器插件(2种方法)
 
方法一:
a : 在mybatis-config.xml中配置插件 
<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>

b:在applicationContext-mybatis.xml中引入mybatis文件

<!-- 配置sqlSessionFactory整合MyBatis的Bean组件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <property name="typeAliasesPackage" value="com.del.pojo"></property>  
</bean>
方法二:在applicationContext-mybatis.xml中整合配置拦截器插件

<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>
 
3、在代码中使用PageHelper分页
 
controller层使用:
         /*
         * 展示用户列表管理
         */
        @RequestMapping(value="/user")
        public String doUserList(@RequestParam(value="currentPage")String currentPage,Model model){
             
              if(Integer.parseInt(currentPage)==1){
                       PageHelper.startPage(1,7);
               }
               PageHelper.startPage(Integer.parseInt(currentPage),7);
               List<User> userlist = userService.findUserList();
               PageInfo<User> info = new PageInfo<User>(userlist);
              
               /*System.out.println("totol======="+info.getTotal());
               System.out.println("EndRow======="+info.getEndRow());
               System.out.println("FirstPage======="+info.getFirstPage());
               System.out.println("LastPage======="+info.getLastPage());
               System.out.println("NavigatePages======="+info.getNavigatePages());
               System.out.println("NavigatepageNums======="+info.getNavigatepageNums());
               System.out.println("PageSize======="+info.getPageSize());
               System.out.println("Size======="+info.getSize());
               System.out.println("PrePage======="+info.getPrePage());
               System.out.println("StartRow======="+info.getStartRow());
               System.out.println("Pages======="+info.getPages());
               System.out.println("OrderBy======="+info.getOrderBy());
               System.out.println("NextPage======="+info.getNextPage());
               System.out.println("List======="+info.getList());
               System.out.println("LastPage======="+info.getLastPage());
               System.out.println("FirstPage======="+info.getFirstPage());
               System.out.println("isIsFirstPage()======="+info.isIsFirstPage());
               System.out.println("isHasNextPage()======="+info.isHasNextPage());
               System.out.println("isHasPreviousPage()======="+info.isHasPreviousPage());
               System.out.println("PageNum()======="+info.getPageNum());*/
             
               //PageInfo<T> pageInfo = new PageInfo(userlist);
               //mView.addObject("userlist", userlist);
               //mView.setViewName("manager/user");
               model.addAttribute("userlist", userlist);
               model.addAttribute("page", info);
               return "manager/user.jsp";
        }

Jsp页面:

      

[重要说明] 
1:只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select方法)方法会被分页。
2:带有for update的sql,会抛出运行时异常,这样的sql建议手动分页,毕竟这样的sql需要重视。
3:不支持关联结果查询,但是支持关联嵌套查询。只会对主sql进行分页,嵌套的sql不会被分页。

原文地址:https://www.cnblogs.com/domi22/p/8059647.html