SSM框架分页的实现方法 - 极悦
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 SSM框架分页的实现方法

SSM框架分页的实现方法

更新时间:2021-06-24 13:38:53 来源:极悦 浏览1257次

1.首先创建一个分页的工具类

package cn.page.po; 
import java.io.Serializable; 
public class Page implements Serializable {
    private static final long serialVersionUID = -3198048449643774660L;        
    private int pageNow = 1; // 当前页数    
    private int pageSize = 4; // 每页显示记录的条数    
    private int totalCount; // 总的记录条数    
    private int totalPageCount; // 总的页数    
    @SuppressWarnings("unused") 
    private int startPos; // 开始位置,从0开始         
    /**
     * 通过构造函数 传入  总记录数  和  当前页
     * @param totalCount
     * @param pageNow
     */ 
    public Page(int totalCount, int pageNow) { 
        this.totalCount = totalCount; 
        this.pageNow = pageNow; 
    }        
    /**
     * 取得总页数,总页数=总记录数/每页显示记录的条数  
     * @return
     */ 
    public int getTotalPageCount() { 
        totalPageCount = getTotalCount() / getPageSize(); 
        return (totalCount % pageSize == 0) ? totalPageCount  //总页数
                : totalPageCount + 1; 
    }    
    public void setTotalPageCount(int totalPageCount) { 
        this.totalPageCount = totalPageCount; 
    }    
    public int getPageNow() { 
        return pageNow; 
    }    
    public void setPageNow(int pageNow) { 
        this.pageNow = pageNow; 
    }    
    public int getPageSize() { 
        return pageSize; 
    }    
    public void setPageSize(int pageSize) { 
        this.pageSize = pageSize; 
    }    
    public int getTotalCount() { 
        return totalCount; 
    }    
    public void setTotalCount(int totalCount) { 
        this.totalCount = totalCount; 
    } 
    /**
     * 取得选择记录的初始位置
     * @return
     */ 
    public int getStartPos() { 
        return (pageNow - 1) * pageSize; 
    }    
    public void setStartPos(int startPos) { 
        this.startPos = startPos; 
    }    
}

2.写mapper.xml文件

<?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="cn.page.mapper.UserMapper">
<resultMap type="cn.page.po.User" id="userMap">
        <id property="id" column="id" />
        <result property="emplnumb" column="emplnumb" />
        <result property="username" column="username" />
        <result property="password" column="password" />
        <result property="address" column="address" />     
</resultMap>
    <!-- 分页SQL语句 --> 
    <select id="selectUserByPage" resultMap="userMap"> 
        select * 
        from user limit #{startPos},#{pageSize}  
    </select> 
<!-- 取得记录的总数 --> 
<select id="getUserCount" resultType="long">
        SELECT COUNT(*) FROM user  
</select>
</mapper>

3.mapper接口

/**
 * 使用注解方式传入多个参数
 * @param page
 * @return startPos},#{pageSize}
 */
public List<User> selectUserByPage(@Param(value="startPos") Integer startPos,
        @Param(value="pageSize") Integer pageSize); 
/**
 * 用户总数
 */
public long getUserCount();

4.service接口与实现类

void showUserByPage(HttpServletRequest request,Model model); 
@Autowired
private UserMapper userMapper;
    @Override
    public void showUserByPage(HttpServletRequest request, Model model) {
        // TODO Auto-generated method stub
        String pageNow = request.getParameter("pageNow");            
        Page page = null;        
        List<User> user = new ArrayList<User>(); 
      //查询用户总数
        int totalCount = (int) userMapper.getUserCount();       
        if (pageNow != null) { 
            page = new Page(totalCount, Integer.parseInt(pageNow)); 
            user = this.userMapper.selectUserByPage(page.getStartPos(), page.getPageSize()); 
        } else { 
            page = new Page(totalCount, 1); 
            user = this.userMapper.selectUserByPage(page.getStartPos(), page.getPageSize()); 
        }        
        model.addAttribute("user", user); 
        model.addAttribute("page", page); 
    }

5.controller方法

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/pagingUser.action")
    public String pagingUser(HttpServletRequest request,Model model){
        userService.showUserByPage(request, model);
        return "pagingUser";         
    }
}

6.页面显示

<c:forEach items="${user }" var="user">
        <tr>
            <td>${user.emplnumb }</td>
            <td>${user.username }</td>
            <td>${user.address }</td>
            <td>${user.telephone }</td>
        </tr>
    </c:forEach>
</table> 
 <div align="center"> 
        <font size="2">共 ${page.totalPageCount} 页</font> <font size="2">第 
            ${page.pageNow} 页</font> <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=1">首页</a> 
        <c:choose> 
            <c:when test="${page.pageNow - 1 > 0}"> 
                <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow - 1}">上一页</a> 
            </c:when> 
            <c:when test="${page.pageNow - 1 <= 0}"> 
                <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=1">上一页</a> 
            </c:when> 
        </c:choose> 
        <c:choose> 
            <c:when test="${page.totalPageCount==0}"> 
                <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow}">下一页</a> 
            </c:when> 
            <c:when test="${page.pageNow + 1 < page.totalPageCount}"> 
                <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow + 1}">下一页</a> 
            </c:when> 
            <c:when test="${page.pageNow + 1 >= page.totalPageCount}"> 
                <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.totalPageCount}">下一页</a> 
            </c:when> 
        </c:choose> 
        <c:choose> 
            <c:when test="${page.totalPageCount==0}"> 
                <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.pageNow}">尾页</a> 
            </c:when> 
            <c:otherwise> 
                <a href="${pageContext.request.contextPath}/pagingUser.action?pageNow=${page.totalPageCount}">尾页</a> 
            </c:otherwise> 
        </c:choose> 
    </div> 

以上就是极悦小编介绍的"SSM框架分页的实现方法",希望对大家有帮助,想了解更多可观看SSM实战视频教程学习,如有疑问,请在线咨询,有专业老师随时为您服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>