一.配置mybaits的PageHelper插件有两种方式:
先引入pageHelper插件
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
1.SSM整合中 mybaits配置已经集成到了spring的配置文件中,所以配置SpringSessionFactory时候可以指定插件:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--数据源--><property name="dataSource" ref="dataSource"></property><!--mybatis的其他配置 --><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><props><!-- 分页的相关配置参数 详细参数解析见附录 --><prop key="helperDialect">oracle</prop></props></property></bean></array></property></bean>
<configuration><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"/></plugins></configuration>
public PageInfo<Product> findByPageHelper(Integer currPage, Integer pageSize) {//开始分页PageHelper.startPage(currPage, pageSize);//查询全部List<Product> productList = productDao.findAll();//创建PageInfo对象PageInfo<Product> pageInfo = new PageInfo<>(productList, 5);return pageInfo;
<%--默认选中每页条数--%>
$("#pageSize option[value=${pageInfo.pageSize}]").prop("selected","selected");
<!--secript 代码-->
<script type="text/javascript"> // 分页 $("#pageSize option[value=${pageInfo.pageSize}]").prop("selected","selected"); function gotoPage(currPage) { // 获取每页显示条数 var pageSize = $("#pageSize").val(); if(currPage<1){ return; } if(currPage>${pageInfo.pages}){ return; } location.href="${pageContext.request.contextPath}/product/findAll?currPage="+currPage+"&pageSize="+pageSize; }
</script>
<ul class="pagination">
<%--在超链接中访问js函数 必须添加前缀 javascript--%>
<li><a href="javascript:gotoPage(1)" aria-label="Previous">首页</a></li>
<li><a href="javascript:gotoPage(${pageInfo.prePage})">上一页</a></li>
<c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="i">
<li><a href="javascript:gotoPage(${i})">${i}</a></li>
</c:forEach>
<li><a href="javascript:gotoPage(${pageInfo.nextPage})">下一页</a></li>
<li><a href="javascript:gotoPage(${pageInfo.pages})" aria-label="Next">尾页</a></li>
</ul>
Controller 层
// PageHelper 分页助手查询 //查询全部 RequestParam 请求参数 // value 指定页面参数的名称 // required 是否必要有参数 @RequestMapping("/findAll") public ModelAndView findAll(@RequestParam(value = "currPage",defaultValue = "2") Integer currPage, @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){ // 准备数据 PageInfo<Product> pageInfo= productService.findByPageHelper(currPage,pageSize); // 创建ModelAndView 对象 ModelAndView modelAndView = new ModelAndView(); // 添加数据 modelAndView.addObject("pageInfo",pageInfo); // 指定页面 modelAndView.setViewName("product-list"); return modelAndView; }
servrice 层和serviceImp层
//根据分页助手查询
PageInfo<Product> findByPageHelper(Integer currPage,Integer pageSize);
public PageInfo<Product> findByPageHelper(Integer currPage, Integer pageSize) { //指定分页的参数 PageHelper.startPage(currPage, pageSize); //查询全部 List<Product> productList = productDao.findAll(); //创建PageInfo对象 PageInfo<Product> productPageInfo = new PageInfo<>(productList,3); return productPageInfo; }
原文:https://www.cnblogs.com/dragonyl/p/11228182.html