zoukankan      html  css  js  c++  java
  • 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>();
    }
    
    这种写法就能保证安全。
  • 相关阅读:
    视觉(9)争取快点看完基于图论的立体匹配算法
    STL笔记(2) STL之父访谈录
    视觉(7)Computer vision from Wikipedia
    *NIX工具使用(1) Vim notes
    STL笔记(1)map
    AI杂谈(1) 你喜欢ML里的哪些模型?希望ML未来向什么方向发展?
    AI杂谈(3): dodo:人脸识别方法个人见解(zz from prfans)
    AI杂谈(2)请教支持向量机用于图像分类
    视觉(3)blepo
    windows编程(2)深入探讨MFC消息循环和消息泵
  • 原文地址:https://www.cnblogs.com/huiy/p/9027023.html
Copyright © 2011-2022 走看看