https://yq.aliyun.com/articles/619586
How about this blog integration PageHelper in SpringBoot, how to achieve with multiple conditions and PageInfo attributes in Chinese interpretation (translation)
A, jar package
<!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
Second, the registration Configuration Plug
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; } }
Three, dao layer interface
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; }
Paging is best to do with the conditions Map, are particularly vulnerable because of the integration of smart labels, its multi-condition is passed in Map
Four, dao layer disposed 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>=#{startdate} </if> <if test="enddate!= null and enddate!=''"> AND dentrytime<=#{enddate} </if> </where> </select> </mapper>
Fifth, call PageHelper, actually using it
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;
}
}
Sixth, talk about PageInfo, easy call
// this page Private int pageNum; // page number of private int pageSize; the number of the current page // Private int size; // Since startRow and endRow unusual, to say the specific usage here // can be in the page " startRow to display endRow total size of data " // current page number of the first element row in the database private int startRow; // this last element in the page line number in the database private int endRow; // record the total number of private total Long; // pages Private pages int; // result set Private List <T> List; // first Private int firstPage; // Previous Private int prePage; // whether the first page boolean = false isFirstPage Private; // whether the last page of the Private isLastPage boolean = false; // Are there previous page Private hasPreviousPage boolean = false; // if there Next Private hasNextPage boolean = false; // navigation page number int navigatePages Private; // all navigation page number private int [] navigatepageNums;