--------controller-----------
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
//查詢列表數據
Query query = new Query(params);
List<ClientUserEntity> clientUserList = clientUserService.queryList(query);
int total = clientUserService.queryTotal(query);
PageUtils pageUtil = new PageUtils(clientUserList, total, query.getLimit(), query.getPage());
return R.ok().put("page", pageUtil);
}
/**
* 分頁工具類
*
*/
public class PageUtils implements Serializable {
//總記錄數
private int totalCount;
//每頁記錄數
private int pageSize;
//總頁數
private int totalPage;
//當前頁數
private int currPage;
//列表數據
private List<?> list;
/**
* 分頁
* @param list 列表數據
* @param totalCount 總記錄數
* @param pageSize 每頁記錄數
* @param currPage 當前頁數
*/
public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
this.list = list;
this.totalCount = totalCount;
this.pageSize = pageSize;
this.currPage = currPage;
this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}
/** * 查詢參數 * */public class Query extends LinkedHashMap<String, Object> { //當前頁碼 private int page; //每頁條數 private int limit; public Query(Map<String, Object> params){ this.putAll(params); //分頁參數 this.page = Integer.parseInt(params.get("page").toString()); this.limit = Integer.parseInt(params.get("limit").toString()); this.put("offset", (page - 1) * limit); this.put("page", page); this.put("limit", limit); //防止SQL注入(由於sidx、order是經過拼接SQL實現排序的,會有SQL注入風險) String sidx = (String)params.get("sidx"); String order = (String)params.get("order"); if(StringUtils.isNotBlank(sidx)){ this.put("sidx", SQLFilter.sqlInject(sidx)); } if(StringUtils.isNotBlank(order)){ this.put("order", SQLFilter.sqlInject(order)); } }}