1. 程式人生 > >spring-boot 整合mybatis的分頁外掛PageHelper和Generator

spring-boot 整合mybatis的分頁外掛PageHelper和Generator

原文地址:http://blog.csdn.net/zl18310999566/article/details/54097273

分頁外掛PageHelper

新增maven依賴

根據上面地址中的說明,只需要新增如下依賴,並且可以不再mybatis的依賴

[html] view plain copy  print?
  1. <!--分頁外掛-->
  2. <dependency>
  3.       <groupId>com.github.pagehelper</groupId>
  4.       <artifactId
    >pagehelper-spring-boot-starter</artifactId>
  5.       <version>1.0.0</version>
  6. </dependency>

攔截器PageInterceptor

PageInterceptor是PageHelper-5.0版本中新的攔截器,com.github.pagehelper.PageHelper則成為了介面Dialect的一個實現類

在攔截器PageInterceptor中可以看到如下內容

[java] view plain copy  
print
?
  1. private Dialect dialect;  
  2. private String default_dialect_class = "com.github.pagehelper.PageHelper";  
  3. publicvoid setProperties(Properties properties) {  
  4.     this.msCountMap = CacheFactory.createCache(properties.getProperty("msCountCache"), "ms", properties);  
  5.     String dialectClass = properties.getProperty("dialect"
    );  
  6.     if(StringUtil.isEmpty(dialectClass)) {  
  7.         dialectClass = this.default_dialect_class;  
  8.     }  
  9.     try {  
  10.         Class e = Class.forName(dialectClass);  
  11.         this.dialect = (Dialect)e.newInstance();  
  12.     } catch (Exception var5) {  
  13.         thrownew PageException(var5);  
  14.     }  
  15.     this.dialect.setProperties(properties);  
  16.     try {  
  17.         this.additionalParametersField = BoundSql.class.getDeclaredField("additionalParameters");  
  18.         this.additionalParametersField.setAccessible(true);  
  19.     } catch (NoSuchFieldException var4) {  
  20.         thrownew PageException(var4);  
  21.     }  
  22. }  
可以看出,com.github.pagehelper.PageHelper是攔截器預設的Dialect。也可以選擇其它的Dialect實現,可以在application.properties中如下配置。 [plain] view plain copy  print?
  1. pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialect  
其它的Dialect實現在com.github.pagehelper.dialect下找到,不過一般不需要進行特殊配置,使用預設的com.github.pagehelper.PageHelper即可。

而在com.github.pagehelper.PageHelper中有以下兩個引數。

[java] view plain copy  print?
  1. private PageParams pageParams;  
  2. private PageAutoDialect autoDialect;  
  3. publicvoid setProperties(Properties properties) {  
  4.     this.pageParams = new PageParams();  
  5.     this.autoDialect = new PageAutoDialect();  
  6.     this.pageParams.setProperties(properties);  
  7.     this.autoDialect.setProperties(properties);  
  8. }  
其中PageParams中有以下屬性。 [java] view plain copy  print?
  1. protectedboolean offsetAsPageNum = false;  
  2. protectedboolean rowBoundsWithCount = false;  
  3. protectedboolean pageSizeZero = false;  
  4. protectedboolean reasonable = false;  
  5. protectedboolean supportMethodsArguments = false;  
PageAutoDialect中有以下屬性。 [java] view plain copy  print?
  1. privateboolean autoDialect = true;  
  2. privateboolean closeConn = true;  
以上內容都可以在application.properties中進行修改,如下所示。 [plain] view plain copy  print?
  1. pagehelper.autoDialect=true  
  2. pagehelper.closeConn=false  
  3. pagehelper.reasonable=true  

Pagehelper的使用

我增加了如下測試程式碼。

[plain] view plain copy  print?
  1. @RequestMapping("/find/mybatis/page")  
  2. public String findUserPageFromMybatis(HttpServletRequest request, Integer pageNum, Integer pageSize) {  
  3.     pageNum = pageNum == null ? 1 : pageNum;  
  4.     pageSize = pageSize == null ? 10 : pageSize;  
  5.     PageHelper.startPage(pageNum, pageSize);  
  6.     List<UserMo> list = userMapper.selectUserList();  
  7.     PageInfo pageInfo = new PageInfo(list);  
  8.     Page page = (Page) list;  
  9.     return "PageInfo: " + JSON.toJSONString(pageInfo) + ", Page: " + JSON.toJSONString(page);  
  10. }  
執行程式後在瀏覽器輸入 http://127.0.0.1:8080/find/mybatis/page,會看到如下輸出。 [plain] view plain copy  print?
  1. PageInfo: {  
  2.     "endRow": 1,  
  3.     "firstPage": 1,  
  4.     "hasNextPage": true,  
  5.     "hasPreviousPage": false,  
  6.     "isFirstPage": true,  
  7. 相關推薦

    Spring Boot實踐——Mybatis外掛PageHelper的使用

    出自:https://blog.csdn.net/csdn_huzeliang/article/details/79350425 在springboot中使用PageHelper外掛有兩種較為相似的方式,接下來我就將這兩種方式進行總結。 方式一:使用原生的PageHelper 1.在

    Spring整合Mybatis外掛PageHelper

    1. 使用Maven方式引入分頁外掛 在pom.xml中新增如下依賴: <dependency> <groupId>com.github.pagehelper</groupId> <artif

    spring-boot 整合mybatis外掛PageHelperGenerator (番外)

    分頁外掛PageHelper 新增maven依賴 根據上面地址中的說明,只需要新增如下依賴,並且可以不再mybatis的依賴 <!--分頁外掛--> <dependency> <groupId>com.github.pag

    spring-boot 整合mybatis外掛PageHelperGenerator

    原文地址:http://blog.csdn.net/zl18310999566/article/details/54097273 分頁外掛PageHelper 新增maven依賴 根據上面地址中的說明,只需要新增如下依賴,並且可以不再myba

    SpringBoot整合MyBatis外掛PageHelper

    原創作品,可以轉載,但是請標註出處地址:https://www.cnblogs.com/V1haoge/p/9971043.html SpringBoot整合MyBatis分頁外掛PageHelper 步驟 第一步:首先整合MyBatis 參照之前SpringBoot整合MyBatis.md 第二步

    Spring Boot實踐——Mybatis插件PageHelper的使用

    page lis 屬性 gen eas col version myba bye 出自:https://blog.csdn.net/csdn_huzeliang/article/details/79350425 在springboot中使用PageHelper插件有兩種

    SpringBoot整合Mybatis外掛pageHelper事務

    spring事務分類: 1.宣告事務 2.程式設計事務 spring事務原理:AOP技術 環繞通知進行攔截 使用spring事務的注意事項:不要Try。因為要將異常丟擲給外層 Springboot預設整合事務,只要在方法上加上@Transactional註解 pom.xml

    mybatis 外掛pagehelper整合及使用

    /** * 獲取任意查詢方法的count總數 * * @param select * @return */ public static long count(ISelect select) { Page<?> page =

    【防坑指南】使用Mybatis外掛PageHelper為什麼PageInfo物件出現null的原因

    在mybatis中,先匯入pagehelper.jar所需的jar包,然後在sqlMapConfig,xml中配置外掛 <plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plug

    mybatis 外掛PageHelper的簡單使用

    分頁方式的分類: 邏輯分頁 物理分頁 MyBatis-PageHelper 的使用: 首先在pom.xml配置檔案中增加相關的外掛。 外掛地址:https://github.com/pagehelper/Mybatis-PageHelper <depende

    SSM整合兩種配置方式——xmljavaConfig,新增外掛pageHelper通用Mapper

    Spring MVC配置 1. xml方式 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  

    Mybatis外掛PageHelper的配置使用方法

    前言 在web開發過程中涉及到表格時,例如dataTable,就會產生分頁的需求,通常我們將分頁方式分為兩種:前端分頁和後端分頁。 前端分頁 一次性請求資料表格中的所有記錄(ajax),然後在前端快取並且計算count和分頁邏輯,一般前端元件(例如dataTable)會提

    MyBatis外掛PageHelper的使用

    準備工作 在pom.ml中引入依賴 <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency&

    Mybatis外掛PageHelper使用

    MyBatis分頁外掛 Author:SimpleWu 在mybatis中我們需要實現分頁功能,如果我們連線的是mysql我們就要寫mysql的分頁sql,連線oracle就要寫oracle的sql語句,這是很不友好的,而我們針對各種不同的資料庫的分頁我們有一個外掛PageHelper PageHelpe

    mybatis外掛PageHelper的簡單使用

    外掛叫做PageHelper如果你也在用Mybatis,建議嘗試該分頁外掛,這個一定是最方便使用的分頁外掛。 該外掛目前支援Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫分頁。 使用方法: 第一步:匯入mybatis的分頁jar包。(

    Spring Boot 整合Mybatis 之 Mapper外掛(自動生成XML及Mapper程式碼)

    pom檔案 主要程式碼 <dependencies> <dependency> <groupId>mysql</groupId> <artifactI

    基於Mybatis外掛PageHelper實現功能

    使用PageHelper外掛實現分頁功能 分頁的功能幾乎是所有專案必備的功能,在SSM(spring 、springmvc、mybatis)組織的專案中如何實現分頁呢? 下面介紹一種基於mybatis的分頁外掛PageHelper來幫助我們實現分頁的功能。

    MyBatisMyBatis外掛PageHelper的使用

      好多天沒寫部落格了,因為最近在實習,大部分時間在熟悉實習相關的東西,也沒有怎麼學習新的東西,這週末學習了MyBatis的一個分頁外掛PageHelper,雖然沒有那麼的強大(我在最後會說明它的缺

    mybatis外掛pageHelper簡單實用

    工作的框架spring springmvc mybatis3 首先使用分頁外掛必須先引入maven依賴,在pom.xml中新增如下 <!-- 分頁助手 --> <dependency> <groupId>com.githu

    MyBatis 外掛 PageHelper 入門介紹

    1.簡介 官網:https://pagehelper.github.io/ PageHelper可以說是目前最流行的MyBatis分頁外掛了,它使用起來非常簡單且提供一整套的解決方案。以下,我們以傳統Spring專案為例,介紹如何進行使用。SpringBoot的