SpringBoot整合PageHelper做多条件分页查询

https://yq.aliyun.com/articles/619586

 

本篇博客讲述如何在SpringBoot中整合PageHelper,如何实现带多个条件,以及PageInfo中的属性的中文解释(翻译)

一,jar包

 

        <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

 

 

二,注册Configuration插件

 

package cn.dawn.util;

/**
 * Created by Dawn on 2018/6/24.
 */
import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;

/*
 * 注册MyBatis分页插件PageHelper
 */

@Configuration
public class MybatisConf {
    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

 

三,dao层接口

 

package cn.dawn.mapper;

import cn.dawn.entity.Sys_noteDetail;

import java.util.List;
import java.util.Map;

/**
 * Created by Dawn on 2018/4/16.
 */
public interface INoteDAO {


    /*笔记分页查询*/
    public List<Sys_noteDetail> getOnePagenoteData(Map<String, Object> map) throws Exception;


}

 

  做带条件分页最好是Map,因为特别容易整合智能标签,它的多条件就是Map传进来的

四,dao层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.dawn.mapper.INoteDAO">


    <!--分页查询-->

    <select id="getOnePagenoteData" resultType="cn.dawn.entity.Sys_noteDetail">
        SELECT * FROM sys_notedetail
        <where>
            <if test="dname!= null and dname!=''">
                AND dname LIKE  '%' #{dname} '%'
            </if>
            <if test="startdate!= null and startdate!=''">
                AND dentrytime&gt;=#{startdate}
            </if>
            <if test="enddate!= null and enddate!=''">
                AND dentrytime&lt;=#{enddate}
            </if>
        </where>
    </select>


</mapper>

 

五,调用PageHelper,真正使用它

 

package cn.dawn.service.impl;

import cn.dawn.entity.Sys_noteDetail;
import cn.dawn.mapper.INoteDAO;
import cn.dawn.service.INoteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Dawn on 2018/4/16.
 */
@Service("noteServiceImpl")
public class NoteServiceImpl implements INoteService{
    @Resource(name = "INoteDAO")
    private INoteDAO iNoteDAO;


    public PageInfo<Sys_noteDetail> getOnePagenoteData(Integer pageIndex, Integer pageSize, String dname, Date startdate, Date enddate) throws Exception {
         /*创建分页工具类*/
        PageHelper.startPage(pageIndex, pageSize);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("dname",dname);
        map.put("startdate",startdate);
        map.put("enddate",enddate);
        List<Sys_noteDetail> docs = iNoteDAO.getOnePagenoteData(map);
        PageInfo<Sys_noteDetail> pageInfo = new PageInfo<>(docs);
        return pageInfo;

    }

}

 

六,讲述一下PageInfo,方便调用

 

    //当前页  
    private int pageNum;  
    //每页的数量  
    private int pageSize;  
    //当前页的数量  
    private int size;  
    //由于startRow和endRow不常用,这里说个具体的用法  
    //可以在页面中"显示startRow到endRow 共size条数据"  
  
    //当前页面第一个元素在数据库中的行号  
    private int startRow;  
    //当前页面最后一个元素在数据库中的行号  
    private int endRow;  
    //总记录数  
    private long total;  
    //总页数  
    private int pages;  
    //结果集  
    private List<T> list;  
  
    //第一页  
    private int firstPage;  
    //前一页  
    private int prePage;  
  
    //是否为第一页  
    private boolean isFirstPage = false;  
    //是否为最后一页  
    private boolean isLastPage = false;  
    //是否有前一页  
    private boolean hasPreviousPage = false;  
    //是否有下一页  
    private boolean hasNextPage = false;  
    //导航页码数  
    private int navigatePages;  
    //所有导航页号  
    private int[] navigatepageNums;  

 

posted @ 2019-11-17 10:41  kelelipeng  阅读(2449)  评论(1编辑  收藏  举报