ssm分页功能实现

该分页功能,主要是利用了mysql数据库中的limit关键字,来实现的

select * from category limit 0,10

limit a,b;这里a表示起始的下标,mysql数据库是从0开始的,参数b表示有多少条数据。

page类代码

public class myPage {

    private int start;//起始下标
    private int curPage;//当前页
    private int count; //每页显示个数
    private int total; //总个数
    private String param; //参数

    private static final int defaultCount = 1; //默认每页显示5条

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
        System.out.println("myPage" + this.getTotalPage());
    }

    public static int getDefaultCount() {
        return defaultCount;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public myPage() {
        count = defaultCount;
    }

    public myPage(int curPage, int count) {
        this();

        this.curPage = curPage;
        this.count = count;
    }

    //判断当前页是否合法
    public boolean isHasPreviouse() {
        if (curPage == 1)
            return false;
        return true;
    }

    public boolean isHasNext() {
        if (curPage == getTotalPage())
            return false;
        return true;
    }

    public int getTotalPage() {
        int totalPage;//页数
        // 假设总数是50,是能够被5整除的,那么就有10页
        if (0 == total % count)
            totalPage = total / count;
            // 假设总数是51,不能够被5整除的,那么就有11页
        else
            totalPage = total / count + 1;

        if (0 == totalPage)
            totalPage = 1;
        return totalPage;

    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    //计算数据库查询的起始下标和总页数
    public void iscurPage() {
        if (curPage > this.getTotalPage())
            curPage = this.getTotalPage();
        if (curPage < 1)
            curPage = 1;
        start = (curPage - 1) * count;
        System.out.println("curPage" + this.getTotalPage());
    }

    @Override
    public String toString() {
        return "myPage{" +
                "start=" + start +
                ", curPage=" + curPage +
                ", count=" + count +
                ", total=" + total +
                ", param='" + param + '\'' +
                '}';
    }
}

controller类

@Controller
@RequestMapping("")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @RequestMapping("admin_category_list")
    public String list(Model model,myPage page){
        int total=categoryService.total();
        page.setTotal(total);
        page.iscurPage();
//        System.out.println(page);
//        System.out.println(page.getTotalPage());
        List<Category> cs= categoryService.list(page);
        model.addAttribute("cs", cs);
        model.addAttribute("page",page);
        return "admin/listCategory";
    }
}

Mapper映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wb.mapper.CategoryMapper">
    <select id="list" resultType="Category">
        select * from   category order by id desc
        <if test="start!=null and count!=null">
        limit #{start},#{count}
        </if>
    </select>
</mapper>

前端 myPage.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>


<script>
    $(function(){
        $("ul.pagination li.disabled a").click(function(){
            return false;
        });
    });

</script>


<nav>
    <ul class="pagination">
        <%--      首页--%>
        <li <c:if test="${!page.hasPreviouse}">class="disabled"</c:if>>
            <a  href="?curPage=1${page.param}" aria-label="Previous" >
                <span >&laquo;</span>
            </a>
        </li>
        <%--上一页--%>
        <li <c:if test="${!page.hasPreviouse}">class="disabled"</c:if>>
            <a  href="?curPage=${page.curPage-1}${page.param}" aria-label="Previous" >
                <span aria-hidden="true">&lsaquo;</span>
            </a>
        </li>
        <%--当前页--%>
<%--            主要分两种情况--%>
<%--            1.总页数小于设置的页码页数时--%>
            <c:if test="${page.totalPage<=7}">
                <c:forEach var="i" begin="1" end="${page.totalPage}">
                    <c:choose>
                        <c:when test="${page.curPage==i}">
                            <li class="disabled">
                                <a href="?curPage=${i}${page.param}" class="current">${i}</a>
                            </li>
                        </c:when>
                        <c:otherwise>
                            <li>
                                <a href="?curPage=${i}${page.param}">${i}</a>
                            </li>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>
            </c:if>
<%--            2.总页数大于页码页数时又可以分为两种情况
2.1当前页小于页码页数7
2.2当前页大于页码页数的时候,为了让当前页位于正中间可以begin="${page.curPage-3}" end="${page.curPage+3}"
前面三个后面三个
--%>
            <c:if test="${page.totalPage>7}">
            <c:if test="${page.curPage<7}">
                <c:forEach  var="i" begin="1" end="7" >
                    <c:choose>
                        <c:when test="${page.curPage==i}">
                            <li class="disabled">
                                <a href="?curPage=${i}${page.param}" class="current">${i}</a>
                            </li>
                        </c:when>
                        <c:otherwise>
                            <li>
                                <a href="?curPage=${i}${page.param}">${i}</a>
                            </li>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>
            </c:if>
            <c:if test="${page.curPage>=7}">
                <c:forEach  var="i" begin="${page.curPage-3}" end="${page.curPage+3}" >
                    <c:if test="${ page.totalPage>=i}">
                    <c:choose>
                        <c:when test="${page.curPage==i}">
                            <li class="disabled">
                                <a href="?curPage=${i}${page.param}" class="current">${i}</a>
                            </li>
                        </c:when>
                        <c:otherwise>
                            <li>
                                <a href="?curPage=${i}${page.param}">${i}</a>
                            </li>
                        </c:otherwise>
                    </c:choose>
                    </c:if>
                </c:forEach>

            </c:if>

            </c:if>

        <%--    下一页--%>
        <li <c:if test="${!page.hasNext}" >class="disabled"</c:if>>
            <a href="?curPage=${page.curPage+1}" aria-label="Next">
                <span aria-hidden="true">&rsaquo;</span>
            </a>
        </li>
        <%--      最后一页--%>
        <li <c:if test="${!page.hasNext}">class="disabled"</c:if>>
            <a href="?curPage=${page.totalPage}${page.param}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
    </ul>
</nav>

 

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值