springboot分页功能

springboot分页功能

导入pageHelper依赖

  • 参照网站:https://blog.csdn.net/Inmaturity_7/article/details/107870151
<!--引入pageHelper依赖-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

Mapper层配置

// 1:根据职位进行模糊查询+附加条件
List<PositionPageBean> queryAllPosition(Map<String,Object> map);

// mapper.xml的sql配置

<!--sql块-->
<sql id="allPosition">
    team_name,company.company_user_id,company_name,company_field,company_curr,company_scale,
	position_name,position_city,position_money,position_experience,position_education,position_walfare
</sql>

<select id="queryAllPosition" resultType="com.weicai.dao.domain.PositionPageBean" parameterType="hashmap">
        SELECT DISTINCT <include refid="allPosition"/>
        FROM weicai_company company,weicai_position position,weicai_team team
        WHERE company.company_user_id = team.company_user_id
        and company.id = position.position_company_id
        and position.position_name like #{search_input}
        <where>
            <if test="salary_range!=null">
                and   position.position_money=#{salary_range}
            </if>
            <if test="work_experience!=null">
                and    position.position_experience=#{work_experience}
            </if>
            <if test="minimum_education!=null">
                and   position.position_education=#{minimum_education}
            </if>
            <if test="work_type!=null">
                and     position.position_nature=#{work_type}
            </if>
            <if test="develop_Time!=null">
                and  str_to_date(position.position_time,'%Y-%m-%d %H:%i:%s')>date_add(now(),
                     interval  #{develop_Time} day)
            </if>
            <if test="work_city!=null and work_city!='全国'">
                and  position.position_city=#{work_city}
            </if>
        </where>
    </select>
// 2:模糊查询公司
List<PositionPageBean> queryAllCompany(Map<String,Object> map);

// mapper.xml的配置
<select id="queryAllCompany" resultType="com.weicai.dao.domain.PositionPageBean" parameterType="hashmap">
        SELECT DISTINCT <include refid="allPosition"/>
        FROM weicai_company company,weicai_position position,weicai_team team
        <where>
            company.company_user_id = team.company_user_id
            and company.id = position.position_company_id
            and company.company_name
            like #{search_input}
            <if test="salary_range!=null">
             	and position.position_money=#{salary_range}
            </if>
            <if test="work_experience!=null">
             	and position.position_experience=#{work_experience}
            </if>
            <if test="minimum_education!=null">
            	and position.position_education=#{minimum_education}
            </if>
            <if test="work_type!=null">
            	and position.position_nature=#{work_type}
            </if>
            <if test="develop_Time!=null">
            	and  str_to_date(position.position_time,'%Y-%m-%d %H:%i:%s')>date_add(now(),
                     interval  #{develop_Time} day)
            </if>
            <if test="work_city!=null and work_city!='全国'">
                and  position.position_city=#{work_city}
            </if>
        </where>
    </select>

Service业务层配置

//pageNum:当前页,PageSize:每页显示条数
PageInfo<User> queryAllByPageHelper(int pageNum,int PageSize);

// 分页的模糊查询
// searchType_select:公司/职位
// map:附加参数集合
// pageNum:当前第几页
// pageSize:这页显示的条数
public PageInfo<PositionPageBean> searchInfo(String searchType_select, Map<String,Object> map, int pageNum, int pageSize);
@Override
    public PageInfo<User> queryAllByPageHelper(int pageNum,int pageSize) {
        
        // 设置分页规则
        PageHelper.startPage(pageNum, pageSize);
        
        // 从数据库查询All数据
        List<User> users = userMapper.queryAllUser();
        for (User user : users) {
            List<Label> labels = userMapper.queryLabels(user.getUserId());
            user.setLableList(labels);
        }
        // 进行数据分页
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        return pageInfo;
    }



@Override
public PageInfo<PositionPageBean> searchInfo(String searchType_select, Map<String,Object> map, int pageNum, int pageSize) {
    //设置当前页数与每页大小
    PageHelper.startPage(pageNum, pageSize);
    List<PositionPageBean> positionPageBeans = null;
    int number = 0;

    // number--->1:按职位查询 2:按公司查询
    if (searchType_select!=null&&searchType_select!=""){
        number =  Integer.parseInt(searchType_select);
    }
    System.out.println("map = " + map);
    if (map.get("search_input")==null||searchType_select==null||map.get("search_input")==""){
        System.out.println("查询主页信息...");
        // 1:没有条件搜索,直接查询所有
        positionPageBeans = companyPositionMapper.queryAllCompanyPosition();
    }else if (number==1){
        // 2:按职位搜索
        System.out.println("按职位查询信息...");
        System.out.println("map="+map);
        positionPageBeans = companyPositionMapper.queryAllPosition(map);
    }else if (number==2){
        //  3:按公司搜索
        System.out.println("按公司查询信息...");
        positionPageBeans = companyPositionMapper.queryAllCompany(map);
    }

    // 得到的数据不能为空
    if (positionPageBeans!=null) {
        // 确认公司后---进行标签查询 Duplicated code fragment
        for (PositionPageBean positionPageBean : positionPageBeans) {
            List<WeicaiLable> labels = companyPositionMapper.queryLabels(positionPageBean.getCompanyUserId());
            positionPageBean.setWeicaiLableList(labels);
        }
    }
    //插入分页数据
    PageInfo<PositionPageBean> pageInfo = new PageInfo<PositionPageBean>(positionPageBeans);
    return pageInfo;
}

Controller控制层配置

 /**
   * 模糊查询+分页
   * @param searchType_select 1:职位搜索 2:公司搜索
   * @param search_input 要查询的类容
   * @param currPage 当前页面
   * @param map 存取值
   * @param type 附加搜索
   * @param require 要求
   * @return url地址
   */
@RequestMapping("/search")
public String searchInfo(@RequestParam(required = false) String searchType_select,
                         @RequestParam(required = false) String search_input,
                         @RequestParam(defaultValue = "1") int currPage,
                         ModelMap map,
                         @RequestParam(required = false) String type,
                         @RequestParam(required = false) String require) {
    System.out.println("type = " + type);
    System.out.println("require = " + require);

    //存储参数的map
    Map<String,Object> parameterMap=new HashMap<String,Object>();

    //搜索类型
    if(searchType_select!=null&&!searchType_select.equals("")){
        searchTypeSelect=searchType_select;
    }
    //搜索条件
    if(search_input!=null&&!"".equals(search_input)){
        searchInput="%"+search_input+"%";
    }
    parameterMap.put("search_input",searchInput);
    //附加参数
    if(type!=null&&require!=null&&!"".equals(type)&&!"".equals(require)){
        parameterMap.put(type,require);
        //搜索类型
        map.put("type", type);
        //搜索条件
        map.put("require", require);
    }

    System.out.println("searchTypeSelect = " + searchTypeSelect);
    System.out.println("searchInput = " + searchInput);


    // 参数1:公司/职位 参数2:内容 参数3:第几页 参数4:每页显示数量
    PageInfo<PositionPageBean> pageBeanPageInfo = companyPositionService.searchInfo(searchTypeSelect,parameterMap,currPage,3);
    // 总页数
    int totalPage = pageBeanPageInfo.getPages();
    int beginPage;// 开始页面
    int endPage;// 结束页面

    // 前5后4---仿百度分页
    // 1:页面总数未超过10个
    if (totalPage<10){
        beginPage=1;
        endPage = totalPage;
    }else {
        // 2:页面总数超过10个
        beginPage = currPage - 5;
        endPage = currPage + 4;
        // 2、1:前面不足5个
        if (beginPage <1){
            beginPage= 1;
            endPage = 10;
        }
        // 2、2:后面不足4个
        if (endPage>totalPage){
            endPage = totalPage;
            beginPage = endPage - 9;
        }
    }

    System.out.println("职位/公司----->"+searchTypeSelect);
    System.out.println("当前页----->"+currPage);

    map.put("searchTypeSelect",searchTypeSelect);
    map.put("search_input",search_input);

    System.out.println("pageBeanPageInfo.getList() = " + pageBeanPageInfo.getList());

    map.put("positionPageBeans",pageBeanPageInfo.getList());// 分页查询后的List集合
    map.put("currPage", currPage);// 当前页面
    map.put("totalPage", totalPage);// 总页数
    map.put("beginPage", beginPage);// 开始页码
    map.put("endPage", endPage);// 结束页码
    map.put("totalRecord", pageBeanPageInfo.getTotal());// 总条数

    return "/czp/list";
}
@RequestParam(required = false) String name// 如果传值为空,不报错
@RequestParam(defaultValue = "1") // 如果未传值默认值为1

在这里插入图片描述

前端页面(仅用于显示所有)

<%--分页显示 start--%>
<div class="Pagination myself">
   <input type="text" value="${currPage}" id="currPage" hidden/>
   <input type="text" value="${totalPage}" id="totalPage" hidden/>
       
   <a class="current" id="firstPage" onclick="first_page_button()">首页</a>
   <span class="current" id="prevPage" onclick="prev_page_button()">上一页 </span>

   <%--分页页码显示--%>
   <c:forEach var="i" begin="${beginPage}" step="1" end="${endPage}">
      <a id="pageSelectID_${i}" class="current" onclick="qty_page_button(${i})">${i}</a>
   </c:forEach>

   <a class="current" id="nextPage" onclick="next_page_button()">下一页 </a>
   <a class="current" id="endPagess" onclick="end_page_button()">尾页</a>
</div>
<%--分页显示 end--%>

在这里插入图片描述

前端a标签样式

<%--点击事件和样式渲染 start--%>
<script type="text/javascript">
// 获取当前页a标签的id
var currPageSelectId = "#pageSelectID_"+${currPage};
// 当前页数
var idBeSelected = ${currPage};
// 全部页数
var totalPages = ${totalPage};
// a标签的class替换
$(currPageSelectId).toggleClass(function () {

    // 当前页样式去除
    if ($(currPageSelectId).hasClass('current')){
        $(currPageSelectId).removeClass('current');// 移除样式
        $(currPageSelectId).attr("disabled",true);// 不可点击
        $(currPageSelectId).css("pointer-events","none");// 设置鼠标事件不可用
    }

    if (idBeSelected==1){
        // 首页样式不可点击
        $("#firstPage").removeClass('current');// 移除样式
        $("#firstPage").attr("disabled",true);// 不可点击
        $("#firstPage").css("pointer-events","none");// 设置鼠标事件不可用

        // 上一页样式不可点击
        $("#prevPage").removeClass('current');// 移除样式
        $("#prevPage").attr("disabled",true);// 不可点击
        $("#prevPage").css("pointer-events","none");// 设置鼠标事件不可用
    }else if (totalPages==idBeSelected){
        // 尾页样式不可点击
        $("#endPagess").removeClass('current');// 移除样式
        $("#endPagess").attr("disabled",true);// 不可点击
        $("#endPagess").css("pointer-events","none");// 设置鼠标事件不可用

        // 下一页样式不可点击
        $("#nextPage").removeClass('current');// 移除样式
        $("#nextPage").attr("disabled",true);// 不可点击
        $("#nextPage").css("pointer-events","none");// 设置鼠标事件不可用
    }
});

// 参照网站:https://blog.csdn.net/weixin_39525197/article/details/80855822

// 首页---ok
function first_page_button(){
    var currPage = $("#currPage").val();
    if(currPage<=1){
        return;
    }
    window.location.href="${basePath}czp/showList";
}


// 上一页--ok
function prev_page_button(){
    var currPage = $("#currPage").val();
    if(currPage<=1){
        return;
    }
    window.location.href="${basePath}czp/showList?currPage="+(currPage-1);
}

// 下一页---ok
function next_page_button(){
    var currPage = Number($("#currPage").val());
    var totalPage = Number($("#totalPage").val());
    if(currPage>=totalPage){
        return;
    }
    window.location.href="${basePath}czp/showList?currPage="+(currPage+1);
}

// 尾页事件---ok
function end_page_button(){
    var currPage = Number($("#currPage").val());
    var totalPage = Number($("#totalPage").val());
    if(currPage>=totalPage){
        return;
    }
    window.location.href="${basePath}czp/showList?currPage="+totalPage;
}

// 按指定页查询
function qty_page_button(currPage){
    var totalPage = Number($("#totalPage").val());
    if(currPage<1 || currPage>totalPage){
        return;
    }
    window.location.href="${basePath}czp/showList?currPage="+currPage;
}
</script>
<%--点击事件和样式渲染 end--%>
   if(currPage>=totalPage){
        return;
    }
    window.location.href="${basePath}czp/showList?currPage="+totalPage;
}

// 按指定页查询
function qty_page_button(currPage){
    var totalPage = Number($("#totalPage").val());
    if(currPage<1 || currPage>totalPage){
        return;
    }
    window.location.href="${basePath}czp/showList?currPage="+currPage;
}
</script>
<%--点击事件和样式渲染 end--%>

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值