第一步:
加载文件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
第二步:配置文件里面配置东西
在Dao层的Spring 文件里面 ApplicationContext.xml
在在各分配置文件的工厂里面加载东西
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 传入PageHelper的插件 -->
<property name="plugins">
<array>
<!--传入插件的对象-->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
第三步:在Service层 使用以及在Controller调用
Dao层:
// 写一个查询所有的评论
@Select("select * from carcomment")
List<CarComment> findAll();
Service层:
// 写一个查询所有的评论 分页
List<CarComment> findAll(int page,int size);
ServiceImpl层:
@Autowired
CarCommentDao carCommentDao;
/**
* 查询出所有用户的评论的列表
* @return
*/
public List<CarComment> findAll(int page,int size) {
//添加分页
PageHelper.startPage(page, size);
return carCommentDao.findAll();
}
Contril层:
//写一个使用分页
@RequestMapping("/findAll")
public ModelAndView findCarComment(@RequestParam(name = "page",required = true,defaultValue = "1") int page,@RequestParam(name = "size",required = true,defaultValue = "5") int size) {
List<CarComment> commentList = carCommentService.findAll(page,size);
PageInfo pageInfo = new PageInfo(commentList);
// System.out.println(commentList);
mv.addObject("pageInfo", pageInfo);
mv.setViewName("Evaluation-list");
return mv;
}
页面实现:
实现遍历里面的内容
<c:forEach items="${pageInfo.list}" var="comment" varStatus="counts">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${counts.count }</td>
<td>${comment.username }</td>
<td>${comment.date }</td>
<td>${comment.userEvaluation }</td>
<td>${comment.user_Headportrait}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs">订单</button>
<button type="button" class="btn bg-olive btn-xs">详情</button>
<button type="button" class="btn bg-olive btn-xs">编辑</button>
</td>
</tr>
</c:forEach>
实现页面的总页数 以及编列页数
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=1&size=${pageInfo.pageSize}" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.prePage}&size=${pageInfo.pageSize}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.pages}" var="i">
<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${i}&size=${pageInfo.pageSize}">${i}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.nextPage}&size=${pageInfo.pageSize}">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.pages}&size=${pageInfo.pageSize}" aria-label="Next">尾页</a></li>
</ul>
</div>
实现页面自选每页的页数:
<div class="box-footer">
<div class="pull-left">
<div class="form-group form-inline">
总共${pageInfo.pages}页,共${pageInfo.total}条数据。 每页 <select class="form-control" id="changePageSize" onchange="changePageSize()">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select> 条
</div>
</div>
<script>
function changePageSize() {
var pagesize = $("#changePageSize").val();
location.href = "${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.pages}&size=" +pagesize;
}
</script>