ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

分页

真分页

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

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

 

这是前端页面的数据反馈

 

高级查询

 

 

 

 我们输入内容返回相应的数据,还要查看职位是否发布

我们可以有4中情况,这里我们怎么处理sql语句呢?

做一个工具类,判断我们需要使用的sql

package cn.jiedada.util;

/**为了判断传入参数,改变sql语句,主要用于前端职位,是否全职查询
 * 的是不是
 * getCondition通过该方法获得
 * @author 杰大大是真滴帅
 *
 */
public class JobsCondition {
    // 职位名称
    private String title;
    // 职位类型
    //
    private Integer positiontype;
    public static String getCondition(String title,Integer positiontype) {
        //SELECT * FROM view_jobs_city where isenabled = 1 and title like  '%会%'
        //这里最好为""因为后面拼接的时候可以拼接多种,不要写死了
        String sql=" ";
        if(title!=null && !title.trim().equals("")){
            sql +=" and title like '%"+title+"%' ";
        }if(positiontype!=null){
            sql +=" and positiontype ="+positiontype+" ";
        }
        return sql;
        
    }
    public JobsCondition() {
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Integer getPositiontype() {
        return positiontype;
    }
    public void setPositiontype(Integer positiontype) {
        this.positiontype = positiontype;
    }
    @Override
    public String toString() {
        return "JobsCondition [title=" + title + ", positiontype=" + positiontype + "]";
    }
    
    
}
View Code

 

控制层代码

package cn.jiedada.controller.front;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.jiedada.damain.Jobs;
import cn.jiedada.service.IJobsService;
import cn.jiedada.util.JobsCondition;
import cn.jiedada.util.PageBeanUtil;

@Controller
@RequestMapping("/jobs")
public class JobsController {
    @Autowired
    private IJobsService service;
    
    @RequestMapping("/page")
    public String type(Integer localPage,Model model,JobsCondition condition) {
        PageBeanUtil<Jobs> pageBean = service.page(localPage,condition);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("condition", condition);
        return "join_us_info";
    }
}
View Code

服务层的代码

@Override
    public PageBeanUtil<Jobs> page(Integer localPage, JobsCondition condition) {
        String sql = JobsCondition.getCondition(condition.getTitle(), condition.getPositiontype());
        Integer totalNum = dao.indeFindNum(sql);
        if(localPage==null){
            localPage=1;
        }
        PageBeanUtil<Jobs> pageBean = new PageBeanUtil<Jobs>(localPage, totalNum);
        List<Jobs> list = dao.selectLimt((localPage-1)*pageBean.getPageSize(), pageBean.getPageSize(),sql);
        pageBean.setList(list);
        return pageBean;
    }
View Code

前端数据接收

<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>

    <head>
        <base href="${pageContext.request.contextPath }/">
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
        <title>源码物流校招</title>
        <link rel="stylesheet" href="css/bootstrap-theme.min.css" />
        <!--引入bootstrap样式文档-->
        <link rel="stylesheet" href="css/bootstrap.min.css" />

        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="css/commons.css" />
    </head>

    <body>
        <!--导航条-->
        <nav class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <!-- 导航上Logo和目录显示 -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#index-navbar" aria-expanded="false">
                               <span class="sr-only">导航目录</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                          </button>
                    <a class="navbar-brand" href="javascript:void(0);">源码物流校园招聘网</a>
                </div>

                <!-- 导航上其他按钮-->
                <div class="collapse navbar-collapse navbar-right" id="index-navbar">
                    <ul class="nav navbar-nav">
                        <li>
                            <a href="index.html">首页</a>
                        </li>
                        <li>
                            <a href="about.html">走进源码</a>
                        </li>
                        <li>
                            <a href="talents.html">人才发展</a>
                        </li>
                        <li>
                            <a href="javascript:void(0);">职位列表</a>
                        </li>
                        <li>
                            <a href="qa.html">Q&A</a>
                        </li>
                    </ul>
                </div>
                <!-- /.navbar-collapse -->
            </div>
            <!-- /.container-fluid -->
        </nav>
        <!--职位搜索-->
        <div class="container" style="border: 1px solid #dcdcdc;padding-top: 30px;padding-bottom: 30px;margin-top: 60px;">
            <span>
                <img src="imgs/join_us_search.jpg" alt="">
            </span>
            <div class="row" style="padding-top: 30px;">
                <form class="form-inline" action="jobs/page">
                    <div class="form-group col-md-3">
                        <label for="jobTitle">职位名称</label>
                        <input type="text" class="form-control" name="title" value="${condition.title }" id="jobTitle" placeholder="职位名称">
                    </div>
                    <div class="form-group col-md-3" style="padding-top: 5px;">
                        <label for="workingTime">工作时间:</label>
                        <label class="radio-inline">
                            <input type="radio" name="positiontype" id="workingTime" value="" 
                            <c:if test="${condition.positiontype==null }">checked="checked"</c:if>> 全部
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="positiontype" id="workingTime" value="1"
                            <c:if test="${condition.positiontype==1 }">checked="checked"</c:if>
                            > 全职
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="positiontype" id="workingTime" value="0"
                            <c:if test="${condition.positiontype==0 }">checked="checked"</c:if>
                            > 兼职
                        </label>
                    </div>
                    <button type="submit" class="btn btn-default">搜索职位</button>
                </form>
            </div>
        </div>
        <!--职位列表-->
        <div class="container job-table">
            <span>
                <img src="imgs/index_title_zw.jpg" alt="">
                <img src="imgs/index_title_more.jpg" alt="">
            </span>
            <table class="table table-hover">
                <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>${j.inputdate }</th>
                    <th>
                        <a href="freemakser/${j.htmlurl }">职位详情</a>
                    </th>
                </tr>
                </c:forEach>
            </table>
            <!--分页-->
            <nav class="navbar-right">
                <ul class="pagination" id="paging">
                    <li>
                        <span>当前第${pageBean.localPage }页</span>
                    </li>
                    <li>
                        <a href="jobs/page?localPage=1">
                            <span aria-hidden="true">首页</span>
                        </a>
                    </li>
                    <li>
                        <a href="javascript:go(${pageBean.prePage })" aria-label="上一页">
                            <span aria-hidden="true">上一页</span>
                        </a>
                    </li>
                    <li>

                    </li>
                    <li>
                        <a href="javascript:go(${pageBean.nextPage })" aria-label="下一页">
                            <span aria-hidden="true">下一页</span>
                        </a>
                    </li>
                    <li>
                        <a href="javascript:go(${pageBean.lastPage })" aria-label="尾页">
                            <span aria-hidden="true">尾页</span>
                        </a>
                    </li>
                    <li>
                        <span>总页数:共${pageBean.totalPage }页</span>
                        <span>总数据:共${pageBean.totalNum }条</span>
                    </li>
                </ul>
            </nav>
        </div>
        <!--友情链接  手机端的时候,就隐藏掉-->
        <div class="container hidden-xs hidden-sm" id="footer-link">
            <img src="imgs/index_link_img.jpg" alt="" class="out-border-left">
            <a href="" class="out-border-left">源码时代官网</a>
            <a href="" class="out-border-left">BootStrap官网</a>
        </div>
        <!--底部-->
        <div class="container-fluid footer-common">
            <p>
                <a href="javascript:void(0);" class="out-border-left">招聘首页</a>
                <a href="about.html" class="out-border-left">走进源码</a>
                <a href="talents.html" class="out-border-left">人才发展</a>
                <a href="join_us_info.html" class="out-border-left">职位列表</a>
                <a href="qa.html" class="out-border-left">Q&A</a>
            </p>
            <p>企业邮箱:test@test688.com </p>
            <p>电话热线:4000-888-888 传真:020-3333-3333</p>
            <p>公司地址:四川省成都市高新区府城大道西段399号天府新谷1号楼6F</p>
            <p>源码物流版权所有 Copyright © 2018 jobs.digitalchina.ourats.com All rights reserved.蜀ICP备18080118号-1</p>
        </div>
    </body>
    <script type="text/javascript">
    
        function go(localPage) {
            //传入参数
            var title='${condition.title }';
            var positiontype='${condition.positiontype }';
            window.location="${pageContext.request.contextPath }/jobs/page?localPage="+localPage+"&title="+title+"&positiontype="+positiontype+"";
        }
    </script>
</html>
View Code

 

posted on 2019-09-12 21:12  哦哟这个怎么搞  阅读(242)  评论(0编辑  收藏  举报