SpringBoot使用PageHelper

1.引入依赖

<dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper-spring-boot-starter</artifactId>
     <version>1.2.10</version>
</dependency>

2.在application.yml中做如下配置

#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=false
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

3.在代码中使用

controller

@PostMapping("/photo/list")
    @ResponseBody
    public Object getMissionphotoList(PageRequest<MissionPhoto> page, MissionPhoto missionPhoto){
    
    
        page.setParam(missionPhoto);
        PageResult<MissionPhoto> pageResult = missionPhotoService.findPages(page);
        PageResponse response = new PageResponse(pageResult.getCount(), pageResult.getData());

        return response;
    }

service

public PageResult<MissionPhoto> findPages(PageRequest<MissionPhoto> page) {
    
    

        HashMap<String, Object> param = new HashMap<>();

        if (page != null && page.getParam() != null){
    
    
            param.put("deviceids",page.getParam().getDeviceIds());
            param.put("missionids",page.getParam().getMissionIds());
        }
        Page page1 = PageHelper.startPage(page.getStart(),page.getLimit());
        List<MissionPhoto> reslut = missionPhotoMapper.selectByCondition(param);
        return new PageResult<MissionPhoto>(page1.getTotal(),reslut);
    }

mapper

<select id="selectByCondition" parameterType="java.util.Map" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from tsk_mission_photo t

      <where>
        <if test="deviceids != null and deviceids.size() > 0">
          and t.device_id in
          <foreach item="item" collection="deviceids" separator="," open="(" close=")">
            #{
    
    item}
          </foreach>
        </if>

        <if test="missionids != null and missionids.size() > 0">
          and t.mission_id in
          <foreach item="item" collection="missionids" separator="," open="(" close=")">
            #{
    
    item}
          </foreach>
        </if>
      </where>

    </select>

返回结构封装

public class PageRequest<T> {
    
    
	//页码,1-based
	private Integer page = 1;
	//每页记录数
	private Integer limit = Integer.MAX_VALUE;
	private T param;
	
	public PageRequest() {
    
    
		
	}
	
	public PageRequest(Integer page, Integer limit, T param) {
    
    
		super();
		this.page = page;
		this.limit = limit;
		this.param = param;
	}
	public Integer getPage() {
    
    
		return page;
	}
	public void setPage(Integer page) {
    
    
		this.page = page;
	}
	public Integer getLimit() {
    
    
		return limit;
	}
	public void setLimit(Integer limit) {
    
    
		this.limit = limit;
	}
	
	public Integer getStart() {
    
    
		Integer start = (page -1) * limit; 
		return start;
	}
	public T getParam() {
    
    
		return param;
	}
	public void setParam(T param) {
    
    
		this.param = param;
	}
	
	
}
/**
 * @author lzg
 * @param <T> 分页数据里面的数据对象类型
 */
public class PageResult<T> {
    
    
	//总记录数
	private Long count;
	//数据
	private List<T> data;
	
	public PageResult(Long count, List<T> data) {
    
    
		this.count = count;
		this.data = data;
		
	}

	public Long getCount() {
    
    
		return count;
	}

	public void setCount(Long count) {
    
    
		this.count = count;
	}

	public List<T> getData() {
    
    
		return data;
	}

	public void setData(List<T> data) {
    
    
		this.data = data;
	}
}
/**
 * @author lzg
 * 暴露给前端的分页查询响应数据
 */
public class PageResponse extends AjaxResponse {
    
    
	//总记录数
	private Long count;

	public PageResponse() {
    
    
		super();
	}
	
	public PageResponse(Integer code, Long count, Object data, String msg)  {
    
    
		super(code, msg, data);
		this.count = count;
	}
	
	public PageResponse(Long count, Object data, String msg) {
    
    
		this(ResponseCode.SUCCESS.getCode(), count, data, msg);
	}
	
	public PageResponse(Long count, Object pageData) {
    
    
		this(count, pageData, "success");
	}

	public Long getCount() {
    
    
		return count;
	}

	public void setCount(Long count) {
    
    
		this.count = count;
	}
	

}

猜你喜欢

转载自blog.csdn.net/hengyunbin/article/details/103634568