PageHelper 使用流程

首先下载分页插件jar包

一、由于使用的是maven项目直接写出pom的配置,其中pagehelper-version是版本号

 <dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper</artifactId>
     <version>${pagehelper-version}</version>
 </dependency>    

二、配置PageHelper使用的数据库mybatis.xml文件,名字可以任意起了

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 告诉分页插件是哪个数据库 -->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

三、加载mybatis.xml文件,在applicationContext.xml文件(spring配置文件)配置实例工厂参数处配置configLocation属性为mabitisxml文件  其中classpath表示编译之后的文件中找,就一定能找到该文件

    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.wsb.pojo"></property>
        <property name="configLocation" value="classpath:mybatis.xml"></property>
    </bean>

四、使用如下

    public EasyUIDataGrid show(int page, int rows) {
        PageHelper.startPage(page, rows);   // 这一句一定要放在前面才行
        //查询全部
        List<TbItem> list = tbItemMapper.selectByExample(new TbItemExample());
        // 查看里面的内容
        System.out.println(list);
        //分页代码
        //设置分页条件
        PageInfo<TbItem> pi = new PageInfo<>(list);
        
        //放入到实体类
        EasyUIDataGrid datagrid = new EasyUIDataGrid();
        datagrid.setRows(pi.getList());
        datagrid.setTotal(pi.getTotal());
        return datagrid;
    }

其中PageHelper.startPage(page, rows)语句一定要在查询语句前面,查询语句会根据page rows参数进行查找,以上用于使用流程,原理待后续补充

你是一只猪
原文地址:https://www.cnblogs.com/guyibade/p/14305659.html