Mybatis分页插件PageHelper使用

使用PageHelper插件如何分页:

下载地址: 

https://github.com/pagehelper/Mybatis-PageHelper

https://github.com/JSQLParser/JSqlParser

另外一个地址:

Pagehelper 下载地址:

http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/

jsqlparser 下载地址:

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/

使用步骤:

1、导入相关包 pagehelper-x.x.x.jar 和 jsqlparser-x.x.x.jar。

2、在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">

<!-- 注意 <plugins> 在xml文件中的位置,必须要符合 http://mybatis.org/dtd/mybatis-3-config.dtd 中指定的顺序:-->
<!-- configuration (properties?, settings?, typeAliases?, typeHandlers?, 
    objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?) -->
<configuration>     
    <!-- 为SQL定义部分的parameterType或resultType属性指定自定义类型的别名 -->
	<typeAliases>	  		
		<typeAlias alias="ServiceStation" type="com.mybatis.models.ServiceStation" />
		<typeAlias alias="InspectorInfo" type="com.mybatis.models.InspectorInfo" />
		<typeAlias alias="StationInspector" type="com.mybatis.models.StationInspector" />	
	</typeAliases>
	<!-- 配置分页拦截器 -->
	<plugins>	
	    <!-- 配置分页插件  -->
	    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
		<!-- com.mybatis.util为PageHelper类所在包名 -->	
        <!-- <plugin interceptor="com.mybatis.util.PagePlugin"> -->
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
            <!-- <property name="dialect" value="SQLite" /> -->
            <!-- <property name="pageSqlId" value=".*Page.*" /> -->
        <!-- </plugin> -->
    </plugins>   
	<!-- 设置数据库连接参数 -->
	<!-- 与spring 集成之后,这些可以完全删除,数据库连接的管理交给 spring 去管理 -->
	<environments default="development">
	    <environment id="development">
		<transactionManager type="JDBC" />
			<dataSource type="POOLED">  
                <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
			    <property name="url" value="jdbc:sqlserver://mssql-rw-cyp-coopbusiness.vip.test.suixinhuan.com;DatabaseName=CYP_CoopBusiness" />
			    <property name="username" value="Umanager" />
			    <property name="password" value="ASD123asd!1" />
           </dataSource>
		</environment>
	</environments>	
    <!-- 加载SQL定义文件 -->
    <!-- 这里交给sqlSessionFactory 的 mapperLocations属性去得到所有配置信息 -->
	<mappers>	  	   	  
	     <mapper resource="com/mybatis/sql/ServiceStation.xml" />
	     <mapper resource="com/mybatis/sql/InspectorInfo.xml" />
	     <mapper resource="com/mybatis/sql/StationInspector.xml" />
	</mappers>
</configuration>

 3、使用PageHelper提供的方法进行分页

package com.mybatis;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatis.dao.InspectorInfoMapper;
import com.mybatis.dao.MyBatisUtil;
import com.mybatis.dao.ServiceStationMapper;
import com.mybatis.dao.StationInspectorMapper;
import com.mybatis.models.ServiceStation;
import com.mybatis.models.InspectorInfo;
import com.mybatis.models.StationInspector;
//import com.mybatis.util.Page;

/*
 * 测试类
 */
public class HelloMyBatisProgram {
	 public static void main(String[] args) {
		  SqlSession session = MyBatisUtil.getSession();
          //由框架生成ServiceStationMapper接口实现对象
		  ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper.class);
		  InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper.class);
		  StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper.class);
          //System.out.println(ssDaoMaper.getClass().getName());		
		  System.out.println("===========分页获取所有服务站列表============");	    
       
		  //分页设置放在查询之前            
		  Page<Object> page = PageHelper.startPage(1, 5, "StationName");
		  List<ServiceStation> listPage = ssDaoMaper.findAllPage("110100");		
		  for(ServiceStation item:listPage) {
              System.out.println(item.getStationName()+"  "+item.getCityCode()+"  "+item.getCityName());
          }     
		  System.out.println("当前页码:"+page.getPageNum());
		  System.out.println("每页的记录数:"+page.getPageSize());
		  System.out.println("总记录数:"+page.getTotal());
		  System.out.println("总页码:"+page.getPages());				
     }     
}

 4、可以使用更强大的PageInfo封装返回结果

package com.mybatis;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatis.dao.InspectorInfoMapper;
import com.mybatis.dao.MyBatisUtil;
import com.mybatis.dao.ServiceStationMapper;
import com.mybatis.dao.StationInspectorMapper;
import com.mybatis.models.ServiceStation;
import com.mybatis.models.InspectorInfo;
import com.mybatis.models.StationInspector;
//import com.mybatis.util.Page;

/*
 * 测试类
 */
public class HelloMyBatisProgram {
	 public static void main(String[] args) {
		  SqlSession session = MyBatisUtil.getSession();
          //由框架生成ServiceStationMapper接口实现对象
		  ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper.class);
		  InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper.class);
		  StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper.class);
          //System.out.println(ssDaoMaper.getClass().getName());		
		  System.out.println("===========分页获取所有服务站列表============");	    
       		
		  //分页设置放在查询之前            
		  page = PageHelper.startPage(1, 3, "StationName desc");
          List<ServiceStation> list = ssDaoMaper.findAll(); 
          for(ServiceStation item:list) {
               System.out.println(item.getStationName()+"  "+item.getCityCode()+"  "+item.getCityName());
          }          
          PageInfo<ServiceStation> info = new PageInfo<ServiceStation>(list, 3);                    
          System.out.println("当前页码:"+info.getPageNum()); 
          System.out.println("每页的记录数:"+info.getPageSize());
          System.out.println("总记录数:"+info.getTotal());
          System.out.println("总页码:"+info.getPages());
          System.out.println("是否第一页:"+info.isIsFirstPage());
          System.out.println("连续显示的页码:");
          int[] nums = info.getNavigatepageNums();
          for (int i = 0; i < nums.length; i++) {
               System.out.println(nums[i]);
          }       
     }     
}

  

  

posted @ 2018-09-26 14:21  以德为先  阅读(9390)  评论(0编辑  收藏  举报