首頁 > 軟體

Java使用Queryable-pageable實現分頁效果

2022-06-21 18:01:23

在維護老專案的時候需要對部分分頁進行手動修改、方式是獲取前端的分頁引數 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;
 
/**
 * 頁面展示分頁
 * 
 * @author lyy
 *
 * @param <T>
 */
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分頁內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com