一:编写实体类
@Setter
@Getter
@ToString
@Entity
@Repository
public class PageBean<T> {
private Integer currPage;//当前页
private Integer pageSize;//每页条数
private Integer totalCount;//总条数
private Integer totalPage;//总页数
private List<T> list;//当前页数据
}
二:编写dao层,写sql语句(mysql)
//查询总条数
@Select("select count(1) from product")
public Integer findByTotalCount(); //分页查询
@Select("select * from (SELECT *,(@rowNum:=@rowNum+1) as rowNo FROM product,(Select (@rowNum :=0) ) b) " +
"res where rowNo>=#{param1} and rowNo<=#{param2}")
List<Product> findByProduct(Integer start,Integer end);
三:编写service层以及实现类
1、service层
public PageBean<Product> findByProduct(Integer currPage, Integer pageSize);
2、实现类
@Override
public PageBean<Product> findByProduct(Integer currPage, Integer pageSize) {
//1、创建PageBean对象
PageBean<Product> pageBean = new PageBean<>();
//2、获取当前页面(页面传参过来)
pageBean.setCurrPage(currPage);
//3、每页条数
pageBean.setPageSize(pageSize);
//4、总条数
Integer totalCount = productDao.findByTotalCount();
pageBean.setTotalCount(totalCount);
//5、总页数
double ceil = Math.ceil(totalCount * 1.0 / pageSize);
pageBean.setTotalPage((int) ceil);
//6、当前页面数据,从数据库查询
int start = pageSize * (currPage - 1) + 1;
int end = pageSize * currPage;
List<Product> productList = productDao.findByProduct(start, end);
pageBean.setList(productList);
return pageBean;
}
四、编写控制器
//分页查询
@RequestMapping("/findByProduct")
public ModelAndView findByProduct(@RequestParam(value = "currPage",required = false,defaultValue = "1") Integer currPage,
@RequestParam(value = "pagesSize",required = false,defaultValue = "5") Integer pageSize){
PageBean<Product> pageBean = new PageBean<>();
pageBean =productService.findByProduct(currPage, pageSize);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("pageBean",pageBean);
modelAndView.setViewName("product-list");
return modelAndView;
}
五、编写web网页文件
<div class="form-group form-inline">
总共${pageBean.totalPage}页,共${pageBean.totalCount} 条数据。 每页
<select class="form-control" id="pageSize" onchange="gotoPage(1)>
      <option value="2">2</option>
<option value="3">3</option>
<option value="5" selected="selected">5</option>
<option value="10">10</option>
</select> 条
</div>
<div class="box-tools pull-right">
<ul class="pagination">
<%--在超链接中访问js函数 必须添加前缀 javascript--%>
<li><a href="javascript:gotoPage(1)" aria-label="Previous">首页</a></li>
<li><a href="javascript:gotoPage(${pageBean.currPage-1})">上一页</a></li>
<c:forEach begin="1" end="${pageBean.totalPage}" var="i">
<li><a href="javascript:gotoPage(${i})">${i}</a></li>
</c:forEach>
<li><a href="javascript:gotoPage(${pageBean.currPage+1})">下一页</a></li>
<li><a href="javascript:gotoPage(${pageBean.totalPage})" aria-label="Next">尾页</a></li>
</ul>
</div>
<script type="text/javascript">
$("#pageSize option[value=${pageBean.pageSize}]").prop("selected","selected");
function gotoPage(currPage) {
// 获取每页显示条数
var pageSize = $("#pageSize").val();
if(currPage<1){
return;
}
if(currPage>${pageBean.totalPage}){
return;
}
location.href="${pageContext.request.contextPath}/product/findByProduct?currPage="+currPage+"&pageSize="+pageSize
;
}
</script>

最新文章

  1. Mybatis整合Spring
  2. MFC 文件遍历
  3. SQL 查询两个字段相同表的不同记录
  4. redis 扩展 安装 和 memcached 安装
  5. acdream1233 Royal Federation (构造?)
  6. How to Use JUnit With JMeter
  7. 初涉SQL Server性能问题(1/4):服务器概况
  8. 答CsdnBlogger问-关于职业发展和团队管理问题
  9. MySQL存储引擎MyISAM与InnoDB的优劣
  10. HTMLParser使用详解(3)- 通过Filter访问内容
  11. asp.net TreeView与XML配合使用v1.1
  12. 11.2 afternoon
  13. Windows下caffe的配置和调用caffe库(一)
  14. WSL与Windows交互实践
  15. ASP.NET Core的实时库: SignalR简介及使用
  16. [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path
  17. 如何快速连接无线Wifi 使用二维码
  18. 在线批量将gps经纬度坐标转换为百度经纬度坐标
  19. Python基础学习---比较运算符
  20. Hive 数仓中常见的日期转换操作

热门文章

  1. 通过 Swoole\Table 实现 Swoole 多进程数据共享
  2. EF多租户实例:演变为读写分离
  3. js 异或加密
  4. Python爬虫入门(基础实战)—— 模拟登录知乎
  5. php +go关键字实现协程
  6. 2019-2020-1 20199325《Linux内核原理与分析》第五周作业
  7. GDI+ 绘制砂岩含量图版
  8. Linux网络管理员:网络概论
  9. linux rpm包
  10. Django中search fields报错:related Field has invalid lookup: icontains