基于SSM整合的分页功能的实现

时间:2022-10-12 13:28:50

1.Page类用于存放分页信息:
  start: 开始位置
  count: 每页的个数
  last: 最后一页的位置
  caculateLast()方法: 通过总数total和每页的个数计算出最后一页的位置

package com.hcy.utils;

public class Page {
    public int start = 0;
    public int count = 3;
    public int last = 0;
    public int getStart() {
        return start;
    }
    public void setStart(int start) {
        this.start = start;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public int getLast() {
        return last;
    }
    public void setLast(int last) {
        this.last = last;
    }
    
    public void calculateLast(int total) {
        
        if(0==total%count) {
            last = total-count;
        }else {
            last = total-total%count;
        }
    }
    
}

2.在CategoryMapper接口中添加方法,同时CategoryMapper.xml中也加入关于分页的sql,如下

package com.hcy.dao;

import java.util.List;

import com.hcy.bean.Category;
import com.hcy.utils.Page;

public interface CategoryMapper {
    public List<Category> list(Page page);
    public int total();
}
<?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.hcy.dao.CategoryMapper">
    <select id="list" resultType="Category">
        select * from category_
        <if test="start!=null and count!=null">
        limit #{start},#{count}
        </if>
    </select>
    <select id="total" resultType="int">
        select count(*) from category_
    </select>
</mapper>

3.service层也添加对应的方法,具体如下

package com.hcy.service;

import java.util.List;

import com.hcy.bean.Category;
import com.hcy.utils.Page;

public interface CategoryService {
    public List<Category> list(Page page);
    public int total();
}
package com.hcy.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hcy.bean.Category;
import com.hcy.dao.CategoryMapper;
import com.hcy.service.CategoryService;
import com.hcy.utils.Page;
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Category> list(Page page) {
        
        return categoryMapper.list(page);
         
    }

    @Override
    public int total() {
        
        return categoryMapper.total();
    }

}

4.controller层

package com.hcy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hcy.bean.Category;
import com.hcy.service.CategoryService;
import com.hcy.utils.Page;

@Controller
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/listpage")
    public ModelAndView list(Page page){
        ModelAndView mav = new ModelAndView();
        List<Category> cs = categoryService.list(page);
        int total = categoryService.total();
        page.calculateLast(total);
        mav.addObject("cs",cs);
        mav.setViewName("listCategory");
        return mav;
    }
}

5.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  
 <div style="width:500px;margin:0px auto;text-align:center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>name</td>
        </tr>
        <c:forEach items="${cs}" var="c" varStatus="st">
            <tr>
                <td>${c.id}</td>
                <td>${c.name}</td>
            </tr>
        </c:forEach>
    </table>
    <div style="text-align:center">
        <a href="?start=0">首  页</a>
        <a href="?start=${page.start-page.count}">上一页</a>
        <a href="?start=${page.start+page.count}">下一页</a>
        <a href="?start=${page.last}">末  页</a>
    </div>
 </div>

6.运行结果

基于SSM整合的分页功能的实现