Java使用Queryable-pageable实现分页效果

2022-11-13 10:11:10 java Queryable

在维护老项目的时候需要对部分分页进行手动修改、方式是获取前端的分页参数 page以及rows、 new PageRequest对象把分页参数传进去、通过 queryable.setPageable(pageable);保存、最后进行调用。

完整代码如下:

    @ResponseBody
    @RequestMapping(value = "getLbjzkList", method = RequestMethod.GET)
    @PageableDefaults(sort = "id=desc")
    private void ajaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, httpservletRequest request,
        HttpServletResponse response) throws ioException {
        EntityWrapper<LbjZk> entityWrapper = new EntityWrapper<LbjZk>(entityClass);
        propertyPreFilterable.addQueryProperty("id");
        propertyPreFilterable.addQueryProperty("name");
       Integer page= Integer.parseInt(request.getParameter("page").toString());
       Integer rows= Integer.parseInt(request.getParameter("rows").toString());
 
        // 预处理
        queryable.setPageable(new PageRequest(page, rows));
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
        SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
        PageJSON<LbjZk> pagejson = new PageJson<LbjZk>(lbjZkService.list(queryable,entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

PageJson分页工具类:

package com.railway.core.model;
 
import java.io.Serializable;
import java.util.List;
 
import com.railway.core.query.data.Page;
 

public class PageJson<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	private long page = 1;// 当前页
	private long rows = 10;// 每页显示记录数
	private long total; // 总数
	private long totalPages; // 总页数
	private List<T> results; // 结果
 
	public PageJson() {
 
	}
 
	public PageJson(List<T> results) {
		this(0, Long.MAX_VALUE, Long.MAX_VALUE, results);
	}
 
	public PageJson(Page<T> page) {
		this(page.getNumber(), page.getSize(), page.getTotalElements(), page.getContent());
	}
 
	public PageJson(long page, long rows, long total, List<T> results) {
		this.page = page;
		this.rows = rows;
		this.total = total;
		this.results = results;
		this.totalPages = getRows() == 0 ? 1 : (int) Math.ceil((double) total / (double) getRows());
	}
 
	public long getPage() {
		return page;
	}
 
	public void setPage(long page) {
		this.page = page;
	}
 
	public long getRows() {
		return rows;
	}
 
	public void setRows(long rows) {
		this.rows = rows;
	}
 
	public long getTotal() {
		return total;
	}
 
	public void setTotal(long total) {
		this.total = total;
	}
 
	public long getTotalPages() {
		return totalPages;
	}
 
	public List<T> getResults() {
		return results;
	}
 
	public void setResults(List<T> results) {
		this.results = results;
	}
 
}

到此这篇关于Java使用Queryable-pageable实现分页效果的文章就介绍到这了,更多相关Java分页内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章