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

这种写法就能保证安全。

猜你喜欢

转载自www.cnblogs.com/huiy/p/9027023.html