PageInfo分页插件的原理及其使用

版权声明:原创博文,共同进步 https://blog.csdn.net/qq_37939251/article/details/82812521

PageInfo分页插件原理

PageInfo本身是一个物理分页插件,实际原理就是修改最后的执行sql,增加相应的分页内容,是基于拦截器实现的。

例如,首先你配置了PageInfo的Pagenum和Pagesize属性,调用完startPage后,他会通过PageInterceptor对其后的第一个执行sql进行拦截,也就是对List<User> list = userService.findAllUser()进行拦截,这里原本的sql可能是 select * from users,他会自动拼接上分页的sql语句,比如mysql环境的话,就是拼接上limit语句,随后执行,最后的结果,可以通过PageInfo和Page进行获取。

pom.xml文件添加依赖

<!-- mybatis分页依赖start============= -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>
    <!-- mybatis分页依赖end============= -->

  spring-mybatis.xml 文件关于整合mybatis的配置(分页插件有标注)


    <!--整合mybatis -->
    <!-- 1 注入一股mybatis的sqlsessionFactory这就是我们所要做的关键步骤 2 声明式的事务管理 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 引入mappers文件 -->
        <!-- <property name="configLocation" value="classpath:mybatis-config.xml"></property> -->
        <!-- 这就要求所有的mapper文件必须在com/sz/mapper/之下 -->
        <property name="mapperLocations" value="classpath:com/sz/mapper/**/*.xml" />
        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
                <!-- 可以加入驼峰命名,其它mybatis的配置也就是mybatis.cfg.xml的相关配置都会转移到这里来
                 user_name userName
                 -->
                <property name="mapUnderscoreToCamelCase" value="true" />
            </bean>

        </property>
        <!-- 插件配置 -->
        <property name="plugins">
            <array>

                <!-- 分页插件的配置 拦截器实现分页功能 -->
                <bean class="com.github.pagehelper.PageInterceptor">
                <!-- 分页插件的配置 拦截器实现分页功能 -->

                    <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

页面代码

<tr>
                        <td>
                           共${page.total} 条记录 第 ${page.pageNum} / ${page.pages}页
                        </td>
                        <td colspan="8">
                            <a href="${ctx}/app/page/${page.pageNum=1}">首页</a>
                            <a href="${ctx}/app/page/${page.pageNum-1}">上一页</a>
                            <a href="${ctx}/app/page/${page.pageNum+1}">下一页</a>
                            <a href="${ctx}/app/page/${page.pages}">尾页</a>
                        </td>
                    </tr>

接口

package com.sz.service;

import com.github.pagehelper.PageInfo;
import com.sz.pojo.AppInfo;

public interface AppInfoService {
    PageInfo<AppInfo> queryByDevUserId(Long id,PageInfo pageInfo);
}

实现类

package com.sz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Appinfo;
import com.sz.mapper.AppInfoMapper;
import com.sz.pojo.AppInfo;
import com.sz.service.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("appInfoService")
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoMapper appInfoMapper;

    @Override
    public PageInfo<AppInfo> queryByDevUserId(Long id,PageInfo pageInfo) {

        // 去第几页, 页码的大小
        PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize());
        List<AppInfo> lo = appInfoMapper.queryByDevUserId(id);
        PageInfo<AppInfo> page = new PageInfo<AppInfo>(lo);

        return page;
    }
}

控制器代码

package com.sz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sz.pojo.AppInfo;
import com.sz.service.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/app")
public class DevAppController {

    @Autowired
    private AppInfoService appInfoService;

  
//     查询数据 ,分页查询

    @RequestMapping("/page/{page}")
    public  String pageInfo(@PathVariable("page")Integer page,Model model){
        PageInfo<AppInfo> pageInfo = new PageInfo<>();
        Long id= Long.valueOf(1);

        pageInfo.setPageSize(3); //每页的数量
        pageInfo.setPageNum(page);//当前页
//     查询数据,并调用分页插件
        pageInfo =appInfoService.queryByDevUserId(id,pageInfo);
        model.addAttribute("page",pageInfo);
        return "app/index";//app文件夹下的index.jsp页面


    }
}

关键要素:

通过 pageInfo.setPageSize(int);设置每页显示的数量

         pageInfo.setPageNum(page);设置当前所在的页数

希望通过简洁有效的方式实现分页效果

猜你喜欢

转载自blog.csdn.net/qq_37939251/article/details/82812521