手写分页

效果图
在这里插入图片描述

PageBean

package com.pojo;

import java.util.ArrayList;
import java.util.List;

public class PageBean<T> {
    /**当前页*/
    private Integer page;
    /**每页数据条数*/
    private Integer pageSize;
    /**存放数数据的集合*/
    private List<T> list = new ArrayList<>();
    /**是否显示上一页按钮*/
    private boolean showPrevious;
    /**页码偏移量*/
    private Integer offset;
    /**数据总条数*/
    private Integer totalCount;
    /**总页数*/
    private Integer totalPage;
    /**页面展示页数*/
    private List<Integer> pages = new ArrayList<>();
    /**是否显示回到第一页按钮*/
    private boolean showFirstPage;
    /**是否显示下一页按钮*/
    private boolean showNext;
    /**是否显示跳转最后一页的按钮*/
    private boolean showEndPage;

    
    public Integer getPage() {
		return page;
	}


	public void setPage(Integer page) {
		this.page = page;
	}


	public Integer getPageSize() {
		return pageSize;
	}


	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}


	public List<T> getList() {
		return list;
	}


	public void setList(List<T> list) {
		this.list = list;
	}


	public boolean isShowPrevious() {
		return showPrevious;
	}


	public void setShowPrevious(boolean showPrevious) {
		this.showPrevious = showPrevious;
	}


	public Integer getOffset() {
		return offset;
	}


	public void setOffset(Integer offset) {
		this.offset = offset;
	}


	public Integer getTotalCount() {
		return totalCount;
	}


	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}


	public Integer getTotalPage() {
		return totalPage;
	}


	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}


	public List<Integer> getPages() {
		return pages;
	}


	public void setPages(List<Integer> pages) {
		this.pages = pages;
	}


	public boolean isShowFirstPage() {
		return showFirstPage;
	}


	public void setShowFirstPage(boolean showFirstPage) {
		this.showFirstPage = showFirstPage;
	}


	public boolean isShowNext() {
		return showNext;
	}


	public void setShowNext(boolean showNext) {
		this.showNext = showNext;
	}


	public boolean isShowEndPage() {
		return showEndPage;
	}


	public void setShowEndPage(boolean showEndPage) {
		this.showEndPage = showEndPage;
	}


	public PageBean(Integer page, Integer pageSize,Integer totalCount){
        this.page = page;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        //计算总页数
        this.totalPage = (this.totalCount + this.pageSize -1)/this.pageSize;
        //控制分页的合理性
        if(page < 1){
            this.page = 1;
        }
        if (page > totalPage){
            this.page = totalPage;
        }
        if(this.totalPage == 0){
            this.page = 1;
        }
        //计算页码的偏移量
        this.offset = (this.page-1) * this.pageSize;
        //判断是否显示上一页按钮
        if(this.page == 1){
            //如果当前页为第一页不显示上一页按钮
            this.showPrevious = false;
        }else {
            //如果当前页不为第一页显示上一页按钮
            this.showPrevious = true;
        }
        //判断是否显示下一页按钮
        if(this.page.equals(this.totalPage)){
            //如果当前页为最后一页不显示下一页按钮
            this.showNext = false;
        }else{
            //如果当前页不为最后一页显示下一页按钮
            this.showNext = true;
        }
        //计算页面展示页数
        this.pages.add(this.page);
        for (int i = 1; i <= 3 ; i++) {
            if(this.page - i > 0){
                this.pages.add(0,this.page -i);
            }
            if(this.page + i <= this.totalPage){
                pages.add(this.page+i);
            }
        }
        //判断是否显示回到第一页的按钮
        if(this.pages.contains(1)){
            this.showFirstPage = false;
        }else{
            this.showFirstPage = true;
        }
        //判断是否显示回到最后一页的按钮
        if(this.pages.contains(this.totalPage)){
            this.showEndPage = false;
        }else{
            this.showEndPage = true;
        }
    }
}

controller

package com.controller;

import java.sql.SQLException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.pojo.Article;
import com.pojo.PageBean;
import com.service.CityService;

@Controller
public class CityController {

	@RequestMapping("/city")
	public ModelAndView getData(@RequestParam(required = false,defaultValue = "1")Integer page,
			@RequestParam(required = false,defaultValue = "3")Integer pageSize,
			ModelAndView modelAndView) {
		CityService cityService = new CityService();
		PageBean<Article> pageBean = null;;
		try {
			pageBean = cityService.getArtileByPage(page,pageSize);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		modelAndView.addObject("pageBean",pageBean);
		modelAndView.setViewName("page");
		return modelAndView;
					
	}
	
}

service

package com.service;

import java.sql.SQLException;
import java.util.List;

import com.dao.ArticleDao;
import com.pojo.Article;
import com.pojo.PageBean;

public class CityService {

	public PageBean<Article> getArtileByPage(Integer page, Integer pageSize) throws SQLException {
		ArticleDao articleDao = new ArticleDao();
		Integer count = articleDao.getTotal();
		PageBean<Article> pageBean = new PageBean<Article>(page, pageSize, count);
		List<Article> list = articleDao.queryArticle(pageBean.getOffset(),pageBean.getPageSize());
		pageBean.setList(list);
		return pageBean;
	}
	
}

dao

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.pojo.Article;
import com.utils.JDBCUtils;

public class ArticleDao {

	public Integer getTotal() throws SQLException {
		Connection connection = JDBCUtils.getConnection();// 连接数据库返回connection
		String sql = "select count(*) from article";
		PreparedStatement pre = connection.prepareStatement(sql);// 新建pre对象
		ResultSet executeQuery = pre.executeQuery();
		executeQuery.next();
		return executeQuery.getInt(1);
	}

	public List<Article> queryArticle(Integer offset, Integer pageSize) throws SQLException {
		Connection connection = JDBCUtils.getConnection();// 连接数据库返回connection
		String sql = "select * from article limit ?,?";
		PreparedStatement pre = connection.prepareStatement(sql);// 新建pre对象
		pre.setInt(1, offset);
		pre.setInt(2, pageSize);
		ResultSet resultSet = pre.executeQuery();// 执行查询
		ArrayList<Article> list = new ArrayList<>();
		while (resultSet.next()) {
			Long id = resultSet.getLong("id");// 文章id
			String title = resultSet.getString("article_title");// 标题
			String content = resultSet.getString("article_content");// 正文
			Long postTime = resultSet.getLong("article_post_time");// 发表时间
			Long changeTime = resultSet.getLong("article_change_time");// 更新日期
			Long like = resultSet.getLong("article_like");// 点赞
			Long userId = resultSet.getLong("user_id");// 作者id
			Long categoryId = resultSet.getLong("category_id");// 类型id
			Long commentCount = resultSet.getLong("comment_count");
			Article article = new Article(id, title, content, postTime, changeTime, like, commentCount, userId,
					categoryId);
			list.add(article);
		}
		JDBCUtils.closeResources(resultSet, pre, connection);
		return list;
	}

}

页面页码栏

jsp版本

<nav aria-label="Page navigation">
        <ul class="pagination">
        	<c:if test="${pageBean.showFirstPage == true}">
	        	<li>
	                <a href="/springmvc/city.do?page=1"  aria-label="Previous">
	                    <span aria-hidden="true">&lt;&lt;</span>
	                </a>
	            </li>
        	</c:if>
            <c:if test="${pageBean.showPrevious == true}">
            	<li>
                <a href="/springmvc/city.do?page=${pageBean.page-1}"  aria-label="Previous">
                    <span aria-hidden="true">&lt;</span>
                </a>
            </li>
            </c:if>
            
            <c:forEach items="${pageBean.pages }" var="p">
	           	<c:if test="${pageBean.page == p}">
	           	 	<li class="active">
		             	<a href="/springmvc/city.do?page=${p }">${p }</a>
		            </li>
	           	</c:if>
		        <c:if test="${pageBean.page != p}">
	           	 	<li>
		             	<a href="/springmvc/city.do?page=${p }">${p }</a>
		            </li>
	           	</c:if>
            </c:forEach>
           
            
            <c:if test="${pageBean.showNext == true}">
	            <li>
	                <a href="/springmvc/city.do?page=${pageBean.page+1}" aria-label="Next">
	                    <span aria-hidden="true">&gt;</span>
	                </a>
	            </li>
            </c:if>
            
            <c:if test="${pageBean.showEndPage == true}">
            	<li>
                <a href="/springmvc/city.do?page=${pageBean.totalPage}" aria-label="Next">
                    <span aria-hidden="true">&gt;&gt;</span>
                </a>
            </li>
            	
            </c:if>
                    </ul>
    </nav> 

thymeleaf版本

 <nav aria-label="Page navigation">
        <ul class="pagination">
            <li th:if="${pageBean.showFirstPage}">
                <a href="#" th:href="@{/(page=1)}" aria-label="Previous">
                    <span aria-hidden="true">&lt;&lt;</span>
                </a>
            </li>
            <li th:if="${pageBean.showPrevious}">
                <a href="#" th:href="@{/(page=${pageBean.page-1})}" aria-label="Previous">
                    <span aria-hidden="true">&lt;</span>
                </a>
            </li>
            <li th:each="page : ${pageBean.getPages()}" th:class="${pageBean.page == page}?'active':''"><a href="#" th:href="@{/(page=${page})}" th:text="${page}">1</a></li>
            <li th:if="${pageBean.showNext}">
                <a href="#"  th:href="@{/(page=${pageBean.page+1})}" aria-label="Next">
                    <span aria-hidden="true">&gt;</span>
                </a>
            </li>
            <li th:if="${pageBean.showEndPage}">
                <a href="#" th:href="@{/(page=${pageBean.totalPage})}"  aria-label="Next">
                    <span aria-hidden="true">&gt;&gt;</span>
                </a>
            </li>
        </ul>
    </nav>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值