Java SSM框架实现分页

来源:互联网 发布:crossover mac性能如何 编辑:程序博客网 时间:2024/05/16 01:29

1、创建实体类Page.java

复制代码
@Entitypublic class Page {    private int totalRecord;// 表示查询后一共得到多少条结果记录    private int pageSize; // 表示页面一次要显示多少条记录    private int totalPage;// 表示将所有的记录进行分页后,一共有多少页    private int startIndex;// 表示从所有的结果记录中的哪一个编号开始分页查询    private int currentPage; // 表示用户想看的页数    @SuppressWarnings("unchecked")    private List list =null;// list集合是用来装载一个页面中的所有记录的    public Page(int pageNum, int totalRecord) {        this.currentPage = pageNum;        this.totalRecord = totalRecord;        this.pageSize = 5;// 设置一页默认显示10条查询记录        this.startIndex = (this.currentPage - 1) * this.pageSize;// 至于为什么this.page要减1,        // 是因为mysql数据库对于分页查询时,得到的所有的查询记录,第一条记录的编号是从0开始。        if (this.totalRecord % this.pageSize == 0) {            this.totalPage = this.totalRecord / this.pageSize;        } else {            this.totalPage = this.totalRecord / this.pageSize + 1;        }    }   //****此处省略了set和get方法****//}
复制代码

2、创建Dao层实现类PageDaoImpl.java

复制代码
@Repositorypublic class PageDaoImpl implements PageDao {    @Autowired    private JdbcTemplate jdbcTemplate;    /*      * 获得总记录数     */    @SuppressWarnings("deprecation")    public int getTotalRecord(String sql, Object... arrayParameters) {        int totalRecord = jdbcTemplate.queryForInt(sql, arrayParameters);        return totalRecord;    }    /*      * 获取当前页数据     */    @SuppressWarnings("unchecked")    public Page getPage(int pageNum, Class clazz, String sql, int totalRecord, Object... parameters) {        Page page = new Page(pageNum, totalRecord);        sql = sql+" limit "+page.getStartIndex()+","+page.getPageSize();        List list=jdbcTemplate.query(sql, parameters, ParameterizedBeanPropertyRowMapper.newInstance(clazz));            page.setList(list);        return page;    }}
复制代码

3、在服务层实现类中添加代码

复制代码
    public Page getClassifyPage(int pageNum) {        String sql = "select count(*) from t_classify";        int totalRecord = pageDao.getTotalRecord(sql);        sql = "select * from t_classify";        Page page = pageDao.getPage(pageNum, Classify.class, sql, totalRecord);        return page;    }
复制代码

4、在控制层中添加代码

复制代码
    @RequestMapping("/list")    public String list(HttpServletRequest request) {        String pageNum=request.getParameter("p")==null?"1":request.getParameter("p");//获取页码,默认1        request.setAttribute("page", classifyService.getClassifyPage(Integer.valueOf(pageNum)));return "admin/classify/list";    }
复制代码

5、在jsp页面中布局

内容部分:

复制代码
<c:forEach var="classify" items="${page.list}" varStatus="s">  <tr class="column_${s.count}">
       <td class="list-text color999">${classify.name}</td>
       <td class="list-text color999">${classify.id}</td>
</tr></c:forEach>
复制代码

分页按钮部分:

复制代码
       页次:${page.currentPage}/${page.totalPage}&nbsp;每页${page.pageSize}&nbsp;总数${page.totalRecord}&nbsp;&nbsp;&nbsp;&nbsp;  <a href="<c:url value='/admin/user/list.htm?p=1'/>">首页</a>    <c:choose>             <c:when test="${page.currentPage>1}">               <a href="<c:url value='/admin/classify/list.htm?p=${page.currentPage-1}'/>">上一页</a>           </c:when>             <c:otherwise>                   <a href="#">上一页</a>           </c:otherwise>      </c:choose>
    <%--
    &nbsp;&nbsp;
    <c:forEach var="i" begin="1" end="${page.totalPage}">
       <a href="<c:url value='/classify.htm?c=${page.list[0].classifyid}&p=${i}'/>">${i}</a>
    </c:forEach>
    --%> <c:choose> <c:when test="${page.currentPage<page.totalPage}"> <a href="<c:url value='/admin/classify/list.htm?p=${page.currentPage+1}'/>">下一页</a> </c:when> <c:otherwise> <a href="#">下一页</a> </c:otherwise> </c:choose>