• <tr id='ROOYa0'><strong id='ROOYa0'></strong><small id='ROOYa0'></small><button id='ROOYa0'></button><li id='ROOYa0'><noscript id='ROOYa0'><big id='ROOYa0'></big><dt id='ROOYa0'></dt></noscript></li></tr><ol id='ROOYa0'><option id='ROOYa0'><table id='ROOYa0'><blockquote id='ROOYa0'><tbody id='ROOYa0'></tbody></blockquote></table></option></ol><u id='ROOYa0'></u><kbd id='ROOYa0'><kbd id='ROOYa0'></kbd></kbd>

    <code id='ROOYa0'><strong id='ROOYa0'></strong></code>

    <fieldset id='ROOYa0'></fieldset>
          <span id='ROOYa0'></span>

              <ins id='ROOYa0'></ins>
              <acronym id='ROOYa0'><em id='ROOYa0'></em><td id='ROOYa0'><div id='ROOYa0'></div></td></acronym><address id='ROOYa0'><big id='ROOYa0'><big id='ROOYa0'></big><legend id='ROOYa0'></legend></big></address>

              <i id='ROOYa0'><div id='ROOYa0'><ins id='ROOYa0'></ins></div></i>
              <i id='ROOYa0'></i>
            1. <dl id='ROOYa0'></dl>
              1. <blockquote id='ROOYa0'><q id='ROOYa0'><noscript id='ROOYa0'></noscript><dt id='ROOYa0'></dt></q></blockquote><noframes id='ROOYa0'><i id='ROOYa0'></i>
                专注Java教育12年 全国咨询/投诉热线:400-8080-105

                首页 hot资讯 SSM框架分页的实现方法

                SSM框架分页的实现方法

                更新时间:2021-06-24 13:38 来源:玩彩网 浏览179次

                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实战视频教程学习,如有疑问,请在线咨询,有专业老师随时为您服务。

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

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

                返回顶部