1导入依赖
<!-- 分页插件-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
2.配置xml
<!--mybatis整合spring-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 指定mybatis,mapper文件的位置 -->
<property name="mapperLocations" value="classpath:com/zshop/dao/*.xml"></property>
<property name="typeAliasesPackage" value="com.zshop.pojo"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!-- 此处配置自己使用的数据库-->
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
3.编写controller
@RequestMapping("/findAll")
public String findAll(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageName, Model model){
PageHelper.startPage(pageName, 5);
List<ProductType> productTypeList = productTypeService.findAll();
PageInfo<ProductType> pageInfo = new PageInfo<ProductType>(productTypeList,5);
model.addAttribute("pageInfo",pageInfo);
return "productTypeManager";
}
4.jsp页面
<tr>
<c:forEach items="${pageInfo.list}" var="productType">
<td>${productType.id}</td>
<td>${productType.name}</td>
<td>
${productType.statusStr}
</td>
<td class="text-center">
<input type="button" class="btn btn-warning btn-sm doProTypeModify" value="修改">
<input type="button" class="btn btn-warning btn-sm doProTypeDelete" value="删除">
<c:if test="${productType.status==0}">
<input type="button" class="btn btn-success btn-sm doProTypeDisable" value="启用">
</c:if>
<c:if test="${productType.status==1}">
<input type="button" class="btn btn-danger btn-sm doProTypeDisable" value="禁用">
</c:if>
</td>
</tr>
5.写个ul标签放在需要分页条的地方
<ul id="paginator"></ul>
6.引入bootstrap前端插件
<script src="${pageContext.request.contextPath}/js/bootstrap-paginator.js"></script>
<script type="text/javascript">
$(function () {
$("#paginator").bootstrapPaginator({
bootstrapMajorVersion: 3,
currentPage:${pageInfo.pageNum},
totalPages:${pageInfo.pages},
pageUrl:function(type,page,current){
return "${pageContext.request.contextPath}/backend/productType/findAll?pageNum=+"+page;
},
itemTexts:function(type,page,current) {
switch (type) {
case "first": return "首页";
case "prev": return "上页";
case "next": return "下页";
case "last": return "末页";
case "page": return page;
}
}
});
});
</script>
大功告成!!