zoukankan      html  css  js  c++  java
  • PageHelper.startPage和new PageInfo(list)的一些探索和思考

    PageHelper.startPage和new PageInfo(list)的一些探索和思考

    https://blog.csdn.net/shijiujiu33/article/details/99477704

    Mybatis使用pageHelper步骤

    http://www.mamicode.com/info-detail-1725069.html

    分类专栏: Mybatis
    版权
    平常我们使用分页插件的时候,都是很机械的套用

    PageHelper.startPage(1, 10);
    Example example = new Example(Employee.class);
    example.createCriteria().andEqualTo("employeeSex", "男");
    List<Employee> list = employeeTKMapper.selectByExample(example);
    PageInfo<Employee> pageinfo=new PageInfo<>(list);
     
    先PageHelper.startPage(1, 10)开始分页,再selectlist查询数据库的时候会自动加上limit 1,10,最后封装成PageInfo的时候会自动带上页码、页大小、总数等。

    问题引入情景:

    @Autowired
    private EmployeeService employeeService;

    public ApiResult<PageInfo> getAllEmloyee() {
    PageHelper.startPage(1, 3);
    // 调用EmployeeService中的方法
    List<Employee> list = employeeService.getAll();
    PageInfo<Employee> pageInfo = new PageInfo<>(list);
    return ApiResult.success(pageInfo);
    }
     


    public List<Employee> getAll() {
    Example example = new Example(Employee.class);
    example.createCriteria().andEqualTo("employeeSex", "男");
    List<Employee> list = employeeTKMapper.selectByExample(example);
    return list;
    }

    简答说就是:在一个方法中使用PageHelper.startPage(1, 3),再调用另一个方法查询数据库
    这样的结果:查询数据库也是会带上分页信息(已验证,你们可以自己去验证看一下)

    这样我就在想,为什么PageHelper.startPage(1, 3)会有这么大能力呢?

    看一下源码


    protected static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>();
    /**
    * 设置 Page 参数
    *
    * @param page
    */
    protected static void setLocalPage(Page page) {
    LOCAL_PAGE.set(page);
    }

    这是setLocalPage()方法,LOCAL_PAGE是当前线程,通常存储数据为了在同一个线程中都可以访问到

    这里的意思就是 将分页信息保存在当前线程中

    看到这里就豁然开朗了,解释了上面为什么在另一个方法中执行selectlist的时候也会自动加上分页信息
    因为当前请求就对应一个线程,虽然是方法之间存在调用,但是他们还是处于同一个线程中,共享ThreadLocal中的数据

    至于为什么PageHelper.startPage(1, 3)就可以达到分页效果,这里不做详细的源代码解读(我也没看过…)
    但是我觉得大致流程就是:

    分页插件的使用,首先是在Mybatis里面配置了分页拦截器(PageInterceptor),即在执行相关Sql之前会拦截做一点事情;
    所以应该就是在执行selectlist的时候,会自动为sql加上limit 1,3

    还有一点就是使用了PageHelper.startPage,selectlist查询之后赋值给的List<Employee> list
    这个list可以Debug看一下是Page<Employee> 类型
    再看一下,Page类是ArrayList子类


    所以在new PageInfo<>(list)的时候可以把页码、页大小、总页数等信息给pageinfo

    可以看一下,new PageInfo<>(list)源码

    这又让我想到了,如果把PageHelper.startPage(1, 3)去掉,将查询出来的list,再new PageInfo<>(list)

    Debug看了一下

    没有PageHelper.startPage(1, 3),查询的list是ArrayList类型:所以肯定就是走下面的
    else if (list instanceof Collection)...

    以上就是我对PageHelper.startPage和new PageInfo<>(list)的一些探索和思考
    ————————————————
    版权声明:本文为CSDN博主「xiaoshijiu333」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/shijiujiu33/article/details/99477704

  • 相关阅读:
    Tensor总结
    Tensorflow池化
    conda操作
    KS值计算
    supervisor实践
    npm/yarn实践
    nni 环境搭建
    阿里云个人邮箱配置
    Jinja2宏使用
    利用VS code 远程调试 docker 中的 dotnet 应用
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/13360828.html
Copyright © 2011-2022 走看看