分页&高级查询

分页

真分页

点击到那一夜的数据,我们就使用范围查询

Select * from jobs where ... limit 0,3

假分页

我们把所有数据查出来,放到内存中

 分页的实现:

我们创建一个分页的工具类,

package cn.jiedada.util;

import java.util.List;

/**这是一个做分页的分为,当前页,下一页,上一页,总页数,尾页,首页,每一页的大小,
 * 当前页直接获得
 *         算出总页数
        this.totalPage = this.totalNum%pageSize==0?this.totalNum/pageSize:this.totalNum/pageSize+1;
        算出前一页
        this.prePage = this.localPage==1? 1 : this.localPage-1;
        算出后一页
        this.nextPage = this.localPage==this.totalPage? this.totalPage : this.localPage+1;
 * @author 
 *
 * @param <T>
 */
public class PageBeanUtil<T> {
    
    private Integer localPage;
    //总页数
    private Integer totalPage;
    //每页显示数量
    private Integer pageSize = 5;
    //总数据量
    private Integer totalNum;
    //首页
    private Integer firstPage = 1;
    //上一页
    private Integer prePage;
    //下一页
    private Integer nextPage;
    //尾页
    private Integer lastPage;
    //显示的数据
    private List<T> list;

    public PageBeanUtil() {
    }

    public PageBeanUtil(Integer localPage,Integer totalNum) {
        super();
        this.localPage = localPage;
        this.totalNum = totalNum;
        //算出总页数
        this.totalPage = this.totalNum%pageSize==0?this.totalNum/pageSize:this.totalNum/pageSize+1;
        //算出前一页
        this.prePage = this.localPage==1? 1 : this.localPage-1;
        //算出后一页
        this.nextPage = this.localPage==this.totalPage? this.totalPage : this.localPage+1;
        
        this.lastPage = this.totalPage;
    }

    public Integer getLocalPage() {
        return localPage;
    }

    public void setLocalPage(Integer localPage) {
        this.localPage = localPage;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

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

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;
    }

    public Integer getFirstPage() {
        return firstPage;
    }

    public void setFirstPage(Integer firstPage) {
        this.firstPage = firstPage;
    }

    public Integer getPrePage() {
        return prePage;
    }

    public void setPrePage(Integer prePage) {
        this.prePage = prePage;
    }

    public Integer getNextPage() {
        return nextPage;
    }

    public void setNextPage(Integer nextPage) {
        this.nextPage = nextPage;
    }

    public Integer getLastPage() {
        return lastPage;
    }

    public void setLastPage(Integer lastPage) {
        this.lastPage = lastPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "PageBeanUtil [localPage=" + localPage + ", totalPage=" + totalPage + ", pageSize=" + pageSize
                + ", totalNum=" + totalNum + ", firstPage=" + firstPage + ", prePage=" + prePage + ", nextPage="
                + nextPage + ", lastPage=" + lastPage + ", list=" + list + "]";
    }
    
    
}
View Code

通过这里面的构造方法,就能直接封装为一个对象,这里面都能通过计算获得,但是tolalNum是需要我们去查询数据库的

在service层中我们做的语句为

@Override
    public PageBeanUtil<Jobs> page(Integer localPage) {
                //查询中条数
        Integer totalNum = dao.findNum();
        if(localPage==null){
            localPage=1;
        }
        //构造方法
        PageBeanUtil<Jobs> pageBean = new PageBeanUtil<Jobs>(localPage, totalNum);
        //查询
        List<Jobs> list = dao.selectLimt((localPage-1)*pageBean.getPageSize(),pageBean.getPageSize());
        pageBean.setList(list);
        return pageBean;
    }            
View Code
@Override
    public Integer findNum() {
        return template.queryForObject("select count(id) from jobs", Integer.class);
    }

    @Override
    public List<Jobs> selectLimt(int index, Integer pageSize) {
        // TODO Auto-generated method stub
        return template.query("SELECT * FROM view_jobs_city LIMIT ?,?", new BeanPropertyRowMapper<Jobs>(Jobs.class),index,pageSize);
    }
View Code

这是dao层的

 

<div class="container job-table">
            <table class="table table-hover">
                <tr>
                    <th class="hidden-sm">编号</th>
                    <th>工作职位</th>
                    <th>地点</th>
                    <th>人数</th>
                    <th>薪资待遇</th>
                    <th>是否启用</th>
                    <th>发布时间</th>
                    <th>操作</th>
                </tr>
                <c:forEach items="${pageBean.list }" var="j">
                    <tr>
                        <th>#${j.id }</th>
                        <th>${j.title }</th>
                        <th>${j.cname }</th>
                        <th>${j.jobnum }</th>
                        <th>${j.treatment }</th>
                        <th>
                            <c:if test="${j.isenabled }" var="s">    
                                <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                            </c:if>
                            <c:if test="${!s }">
                                <span class="glyphicon glyphicon-remove" cia-hidden="true"></span>
                            </c:if>
                        </th>
                        <th>${j.inputdate }</th>
                        <th>
                            <a href="system/jobs/update?id=${j.id }" class="btn-default tableA"><span class="glyphicon glyphicon-pencil" aria-hidden="true">修改</span></a>
                            <a href="system/jobs/del?id=${j.id }" class="btn-default tableA"><span class="glyphicon glyphicon-trash" aria-hidden="true">删除</span></a>
                        </th>
                    </tr>
                </c:forEach>
            </table>
            <!--分页-->
            <nav class="navbar-right">
                <ul class="pagination" id="paging">
                    <li>
                        <span>当前第${pageBean.localPage }页</span>
                    </li>
                    <li>
                        <a href="system/jobs/page?localPage=1">
                            <span aria-hidden="true">首页</span>
                        </a>
                    </li>
                    <li>
                        <a href="system/jobs/page?localPage=${pageBean.prePage }" aria-label="上一页">
                            <span aria-hidden="true">上一页</span>
                        </a>
                    </li>
                    <li>

                    </li>
                    <li>
                        <a href="system/jobs/page?localPage=${pageBean.nextPage }" aria-label="下一页">
                            <span aria-hidden="true">下一页</span>
                        </a>
                    </li>
                    <li>
                        <a href="system/jobs/page?localPage=${pageBean.lastPage }" aria-label="尾页">
                            <span aria-hidden="true">尾页</span>
                        </a>
                    </li>
                    <li>
                        <span>总页数:共${pageBean.totalPage }页</span>
                        <span>总数据:共${pageBean.totalNum }条</span>
                    </li>
                </ul>
            </nav>
        </div>
    </body>
View Code

 

这是前端页面的数据反馈

 

高级查询

猜你喜欢

转载自www.cnblogs.com/xiaoruirui/p/11515217.html
今日推荐