您的当前位置:首页正文

ssm:实现分页功能

2024-12-01 来源:个人技术集锦

分页功能主要是通过sql语句实现分页功能,如果要每一页显示8条记录,那就使用limit把第0-7条记录截取显示作为第1页,第8~15条记录作为第2页,以此类推。

查询语句为

SELECT * FROM table WHERE 查询条件 ORDER BY 排序条件 LIMIT ((页码-1)*页大小),页大小;

第一步:实现分页功能,首先要创建一个page类

package com.lnsf.tmall.pojo;

public class Page {
    //总条数
    private Integer totalNumber;
    //总页数
    private Integer totalPage;
    //当前页数
    private Integer currentPage;
    //数据库中limit的参数,从第几条开始取
    private Integer dbIndex;
    //数据库中limit的参数,总共取几条
    private Integer dbNumber;
    //每页显示几条
    private Integer pageNumber=8;


    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Integer getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(Integer totalNumber) {
        this.totalNumber = totalNumber;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
        this.count();
    }

    public void count(){
    /**
     * 根据总条数计算总页数
     */
        if (this.totalNumber%this.pageNumber>0){
    /**
     * 如果总条数小于每页显示的条数,那么总页数为1
     */
            if (this.totalNumber<this.pageNumber){
                this.totalPage=1;
            }
            else{
                this.totalPage=(this.totalNumber/this.pageNumber)+1;
            }

        }
        else { this.totalPage=this.totalNumber/this.pageNumber; }
    // 设置当前页数
    // 总页数小于当前页数,应将当前页数设置为总页数
        if(this.totalPage < this.currentPage) {
            this.currentPage = this.totalPage;
        }
    // 当前页数小于1设置为1
        if(this.currentPage < 1) {
            this.currentPage = 1;
        }
    /**
     * 设置limit参数
     */
        this.dbIndex = (this.currentPage-1)*this.pageNumber;//从第几条开始查询
        this.dbNumber = this.pageNumber;//每页显示的条数
    }





    public Integer getTotalPage() {
        return totalPage;
    }

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

    public Integer getDbIndex() {
        return dbIndex;
    }

    public void setDbIndex(Integer dbIndex) {
        this.dbIndex = dbIndex;
    }

    public Integer getDbNumber() {
        return dbNumber;
    }

    public void setDbNumber(Integer dbNumber) {
        this.dbNumber = dbNumber;
    }



    public Page() {

    }

    public Page(Integer totalNumber, Integer totalPage, Integer currentPage, Integer dbIndex, Integer dbNumber, Integer pageNumber) {
        this.totalNumber = totalNumber;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.dbIndex = dbIndex;
        this.dbNumber = dbNumber;
        this.pageNumber = pageNumber;
    }

    @Override
    public String toString() {
        return "Page{" +
                "totalNumber=" + totalNumber +
                ", totalPage=" + totalPage +
                ", currentPage=" + currentPage +
                ", dbIndex=" + dbIndex +
                ", dbNumber=" + dbNumber +
                ", pageNumber=" + pageNumber +
                '}';
    }
}

第二步:在usermapper.xml中创建sql语句
接口:

 List<User> page(@Param("page") Page page);

 Integer count();

xml:

  <!--查询所有用户,并分页显示-->
  <select id="page" parameterType="com.lnsf.tmall.pojo.Page" resultMap="BaseResultMap">
    SELECT * FROM user
    ORDER BY uid limit #{page.dbIndex},#{page.dbNumber}
  </select>

  <!--总条数-->
  <select id="count"  resultType="java.lang.Integer">
    SELECT count(*) FROM user
  </select>

第三步:service层:实现获取对应页数所展示的List
接口:

List<User> showByPage(Integer start, Page page);

impl:

   @Override
    public List<User> showByPage(Integer start, Page page) {
        Integer totalCount =userMapper.count();
        page.setTotalNumber(totalCount);
        page.setCurrentPage(start);
        List<User> users=userMapper.page(page);
        return users;
    }

第三步:controller层

  @RequestMapping(value = "users",method = RequestMethod.GET)
    public String allUsers(Model m,Integer start){
        if (start == null)
            start=1;
        Page page=new Page();
        List<User> users = userService.showByPage(start,page);
        m.addAttribute("page",page);
        m.addAttribute("us",users);
        return "admin/listUser";
    }

第四步:jsp页面

  <c:forEach begin="0" end="${page.totalPage-1}" varStatus="status">

	  <li <c:if test="${status.index*page.pageNumber==page.dbIndex}">class="disabled"</c:if>>
	    	<a  
	    	href="?start=${status.count}"
	    	<c:if test="${status.index*page.pageNumber==page.dbIndex}">class="current"</c:if>
	    	>${status.count}</a>
	    </li>
		
    </c:forEach>
显示全文