(一) 封装一个bean方法
package com.seecen.ssm.pojo;
import java.util.List;
/**
* Author: CalcYu
* Date: 2018/5/18
*/
public class Page<A> {
/**
* 传递查询条件
*/
private A condition;
public A getCondition() {
return condition;
}
public void setCondition(A condition) {
this.condition = condition;
}
private List<A> list;
public List<A> getList() {
return list;
}
public void setList(List<A> list) {
this.list = list;
}
/**
* 某张表的总记录数
*/
private Integer total;
/**
* 当前页数
*/
private Integer currentPage = 1;
/**
* 每页显示多少条记录
*/
private Integer pageSize = 10;
/**
* SQL分页的开始位置
*
* @return
*/
public Integer getStart() {
return (currentPage - 1) * pageSize;
}
/**
* SQL分页的结束位置
*
* @return
*/
public Integer getEnd() {
return currentPage * pageSize;
}
public Integer getTotalPage() {
return (int) Math.ceil(total * 1.0 / pageSize);
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
(二)Service
/**
* 创建一个订单
* @return
*/
public boolean addOrder(Order order);
/**
* 订单表的分页查询
* @param page
* @return
*/
public Page findOrdersByPage(Page page);
《*》ServiceImpl
@Autowired
private OrderMapper orderMapper;
public boolean addOrder(Order order) {
return orderMapper.insert(order) > 0;
}
public Page findOrdersByPage(Page page) {
if(page!=null){
page.setList(orderMapper.selectByExample(null));
page.setTotal(orderMapper.countByExample(null));
return page;
}
return null;
}
(三)Controller
@RequestMapping("/list")
public String list(Integer currentPage,Tclass tclass, Model model){
Page<Tclass> page = new Page<Tclass>();
if (currentPage!=null){
page.setCurrentPage(currentPage);
}
if (tclass!=null){
page.setCondition(tclass);
}
List<Tclass> list = tclassService.findByClassId();
page = tclassService.findTclassByPage(page);
model.addAttribute("l",list);
model.addAttribute("p",page);
return "classfond/list";
}
//或者
@RequestMapping("")
public String list(Model model) {
Page<Order> page = new Page();
page = orderService.findOrdersByPage(page);
model.addAttribute("p", page);
return "order/list";
}
(四)jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="/js/jquery-3.3.1.min.js"></script>
</head>
<body>
<div>
<input id="crtPage" type="hidden" name="currentPage">
<script>
function go(currentPage) {
$("#crtPage").val(currentPage);
$("form").submit();
}
</script>
<c:if test="${p.currentPage>1}">
<a href="javascript:go(1)">首 页</a>
<a href="javascript:go(${p.currentPage-1})">上一页</a>
</c:if>
<c:if test="${p.currentPage==1}">
<a href="javascript:void(0)" style="color: #666666">首 页</a>
<a href="javascript:void(0)" style="color: #666666">上一页</a>
</c:if>
[${p.currentPage}/${p.totalPage}]
<c:if test="${p.currentPage<p.totalPage}">
<a href="javascript:go(${p.currentPage+1})">下一页</a>
<a href="javascript:go(${p.totalPage})">末 页</a>
</c:if>
<c:if test="${p.currentPage==p.totalPage}">
<a href="javascript:void(0)" style="color: #666666">下一页</a>
<a href="javascript:void(0)" style="color: #666666">末 页</a>
</c:if>
</div>
</body>
</html>
(HTML页面)
</tbody>
<!-- ajaxjieshu -->
<tfoot>
<tr>
<td colspan="5"><div onclick="daiban(1)" class="leftdiv">首页</div>
<div class="leftdiv" >上一页</div>
<div class="leftdiv"
onclick="daiban(parseInt($('#pageIndex').html())+1>parseInt($('#pageCount').html())?parseInt($('#pageCount').html()):parseInt($('#pageIndex').html())+1)">下一页</div>
<div class="leftdiv"
onclick="daiban(parseInt($('#pageCount').html()))">尾页</div>
当前第<font color="red" id="pageIndex">1</font>页/共<font
id="pageCount" color="red">0</font>页</td>
</tr>
</tfoot>
</table>