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]);
          }       
     }     
}

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM