分页 page
作者:互联网
分页工具类:
package com.hisoft.util;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class PageUtil<T> {
private Integer pageSize = 10; //每页显示的条数
private Integer totalSize; //当前总条数
private Integer pageNo; //当前页
private Integer start; //起始位置
private List<T> items; //当前查询的结果集
private Integer totalPage;//末页
public PageUtil() {
}
public PageUtil(Integer pageSize, Integer totalSize, Integer pageNo) {
this.pageSize = pageSize;
setTotalSize(totalSize);
setPageNo(pageNo);
}
public PageUtil(Integer totalSize,Integer pageNo){
setTotalSize(totalSize);
setPageNo(pageNo);
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
/**
* 计算总页数
* @param totalSize
*/
public void setTotalSize(Integer totalSize) {
totalPage = totalSize / pageSize;
if(totalSize % pageSize != 0){
totalPage++;
}
this.totalSize = totalSize;
}
public Integer getTotalPage() {
return totalPage;
}
public Integer getPageNo() {
return pageNo;
}
/**
* 赋值当前页并计算起始位置
* @param pageNo
*/
public void setPageNo(Integer pageNo) {
if(pageNo <1){
pageNo = 1;
}
if(pageNo > totalPage){
pageNo = totalPage;
}
start = (pageNo - 1)*pageSize;
this.pageNo = pageNo;
}
public Integer getStart() {
return start;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
}
Dao层:
/**
* 分页
* @param pageNo
* @param pageSize
* @return
*/
public PageUtil<Book> findAll(Integer pageNo,Integer pageSize){
Integer totalSize = getCount().intValue();
PageUtil<Book> page = null;
if(pageSize == null){
page = new PageUtil<Book>(totalSize,pageNo);
}
page = new PageUtil<Book>(pageSize,totalSize,pageNo);
String sql = "select id,bookname,author,publisher,total,nownum from book limit ?,?";
List<Book> bookList = jdbcTemplate.query(sql, new BeanRowMapper(), page.getStart(), page.getPageSize());
page.setItems(bookList);
return page;
}
/**
* 计算总条数
*/
public Long getCount(){
String sql = "select count(*) from book";
// Long sum = jdbcTemplate.queryForObject(sql,Long.class);
// return sum;
Long sum = jdbcTemplate.queryForObject(sql, new RowMapper<Long>() {
public Long mapRow(ResultSet resultSet, int i) throws SQLException {
return resultSet.getLong(1);
}
});
return sum;
}
Controller层:
//分页
@RequestMapping("/find/{page}")
public String findAll(@PathVariable(required = false) Integer page,Model model){
if(page==null){
page = 1;
}
PageUtil<Book> pageUtil = bookService.findAll(page,10);
model.addAttribute("pageUtil",pageUtil);
return "/book/books";
}
jsp页面:
<c:forEach items="${pageUtil.items}" var="list">
<tr>
<td>${list.id}</td>
<td>${list.bookName}</td>
<td>${list.author}</td>
<td>${list.publisher}</td>
<td>${list.total}</td>
<td>${list.nowNum}</td>
<td>
<a href="/book/update/${list.id}">修改</a>
<a href="/book/del/${list.id}">删除</a>
<a href="">借阅</a>
</td>
</tr>
</c:forEach>
<nav aria-label="...">
<ul class="pager">
<li><a href="/book/find/1">首页</a></li>
<li><a href="/book/find/${pageUtil.pageNo - 1}">上一页</a></li>
<c:if test="${pageUtil.pageNo == pageUtil.totalPage}">
<li><a href="javascript:void(0)" disabled>下一页</a></li>
</c:if>
<c:if test="${pageUtil.pageNo < pageUtil.totalPage}">
<li><a href="/book/find/${pageUtil.pageNo + 1}">下一页</a></li>
</c:if>
<li><a href="/book/find/${pageUtil.totalPage}">末页</a></li>
</ul>
</nav>
标签:pageNo,pageSize,totalSize,page,Integer,public,分页 来源: https://blog.csdn.net/weixin_44889526/article/details/120411138