环境:maven+SSM 2018年几乎最新
1、pom添加依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.4</version> </dependency>
在这里尤其注意的是一定要在用到pageHelper项目的pom.xml文件里面都要添加这个依赖
否则在执行分页查询的时候出现警告
警告: Hessian/Burlap: 'com.github.pagehelper.Page' is an unknown class in WebappClassLoader
context:
delegate: false
repositories:
----------> Parent Classloader:
ClassRealm[plugin>org.apache.tomcat.maven:tomcat7-maven-plugin:2.2, parent: sun.misc.Launcher$AppClassLoader@55f96302]
参考别人的博客地址:https://blog.csdn.net/qq_38765404/article/details/78108780
2、在sqlMapConfig.xml文件里,每个人的文件名可能不一样,凑活看,记得灵活变动
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor" /> </plugins>
是不是没有看到配置方言的property?因为在pageHelper 5X后就自动识别数据库了
3、java测试类
package com.shun.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.shun.mapper.TbItemMapper; import com.shun.pojo.TbItem; import com.shun.pojo.TbItemExample; /** * @author czs * @version 创建时间:2018年5月10日 上午9:45:55 */ public class PageHelperTest { public static void main(String[] args) { // 获取spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml"); // 从容器中获得mapper代理对象 TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class); // 在执行查询之前获得设置分页的条件 PageHelper.startPage(1, 20); // 执行查询 List<TbItem> items = itemMapper.selectByExample(new TbItemExample()); // 获取分页信息PageInfo 总记录数 总页数 当前页码等待 PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(items); System.out.println("总记录数:"+pageInfo.getTotal()); System.out.println("总页数:"+pageInfo.getPages()); System.out.println("当前页码:"+pageInfo.getPageNum()); } }
4、运行测试类结果