mybatis:自動分頁插件


項目地址:https://github.com/pagehelper/pagehelper-spring-boot

簡單使用:

1.在pom文件中添加

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
</dependency>

2.測試代碼

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.csget.entity.base.EntityBaseUser;
import com.csget.service.base.ServiceBaseUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestServiceBaseUser {
  @Autowired
  private ServiceBaseUser serviceBaseUser;

  @Test
  public void selectAll() {
    //設置分頁參數
    PageHelper.startPage(2,2);  
    //startPage后緊跟的這個查詢就是分頁查詢,否則線程不安全  
    List<EntityBaseUser> selectAll = serviceBaseUser.selectAll();
    //使用PageInfo包裝查詢結果,只需要將pageInfo交給頁面就可以  
    PageInfo pageInfo = new PageInfo<>(selectAll,2);
    List list = pageInfo.getList();
    for (Object object : list) {
      System.out.println(((EntityBaseUser)object).getUserID());
    }
  }
}

3.日志:可以看到執行了COUNT和limit

2018-05-11 23:18:40.056 DEBUG 5912 --- [           main] c.c.d.base.DaoBaseUser.selectAll_COUNT   : ==>  Preparing: SELECT count(0) FROM t_base_user 
2018-05-11 23:18:40.105 DEBUG 5912 --- [           main] c.c.d.base.DaoBaseUser.selectAll_COUNT   : ==> Parameters: 
2018-05-11 23:18:40.134 DEBUG 5912 --- [           main] c.c.d.base.DaoBaseUser.selectAll_COUNT   : <==      Total: 1
2018-05-11 23:18:40.143 DEBUG 5912 --- [           main] c.csget.dao.base.DaoBaseUser.selectAll   : ==>  Preparing: select userID, pass, saltkey, orgID, postID, rootOrgID, userName, fullSpell, shortSpell, sex, mobile, email, passState, passModify, state, activeTime, expireTime, isValid from t_base_user LIMIT ?, ? 
2018-05-11 23:18:40.145 DEBUG 5912 --- [           main] c.csget.dao.base.DaoBaseUser.selectAll   : ==> Parameters: 2(Integer), 2(Integer)
2018-05-11 23:18:40.156 DEBUG 5912 --- [           main] c.csget.dao.base.DaoBaseUser.selectAll   : <==      Total: 2

對比mybatis的XML:

image

XML中是沒有count語句和limit的配置的,插件做到了自動分頁

使用插件要注意的地方:

原文:https://pagehelper.github.io/docs/howtouse/#1-%E5%BC%95%E5%85%A5%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6

段落節選:

4. 什么時候會導致不安全的分頁?

PageHelper 方法使用了靜態的 ThreadLocal 參數,分頁參數和線程是綁定的。

只要你可以保證在 PageHelper 方法調用后緊跟 MyBatis 查詢方法,這就是安全的。因為 PageHelper 在 finally 代碼段中自動清除了 ThreadLocal 存儲的對象。

如果代碼在進入 Executor 前發生異常,就會導致線程不可用,這屬於人為的 Bug(例如接口方法和 XML 中的不匹配,導致找不到 MappedStatement 時), 這種情況由於線程不可用,也不會導致 ThreadLocal 參數被錯誤的使用。

但是如果你寫出下面這樣的代碼,就是不安全的用法:

PageHelper.startPage(1, 10);
List<Country> list;
if(param1 != null){
    list = countryMapper.selectIf(param1);
} else {
    list = new ArrayList<Country>();
}

這種情況下由於 param1 存在 null 的情況,就會導致 PageHelper 生產了一個分頁參數,但是沒有被消費,這個參數就會一直保留在這個線程上。當這個線程再次被使用時,就可能導致不該分頁的方法去消費這個分頁參數,這就產生了莫名其妙的分頁。

上面這個代碼,應該寫成下面這個樣子:

List<Country> list;
if(param1 != null){
    PageHelper.startPage(1, 10);
    list = countryMapper.selectIf(param1);
} else {
    list = new ArrayList<Country>();
}

這種寫法就能保證安全。


免責聲明!

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



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