1.引入jar包
pagehelper-4.1.3.jar
jsqlparser-1.4.jar
2.代码块
pageNum 页码,pageSize 条数
Map<String, Object> params = new HashMap<>();
调用service方法接口 recordList(pageNum, pageSize, params);
//删除值为空的参数
recordList接口:
params = CommonUtil.removeBlankValue(params);
JSONArray recordArray;
long total;
// 利用PageHelper分页查询 注意:这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
Log.debug(">>>>>>> [查询 页码,条数]" + pageNum + "," + pageSize);
PageHelper.startPage(pageNum, pageSize);
List<LoanVo> listPage = dao.getAllListPage(params);
PageInfo<LoanVo> pageInfo = new PageInfo<>(listPage);
recordArray = JSONUtil.parseArray(pageInfo.getList(), false);
total = pageInfo.getTotal();
JSONObject resObj = JSONUtil.createObj();
resObj.put("Result", "OK");
resObj.put("Records", recordArray);
resObj.put("TotalRecordCount", total);
return resObj.toString();
( 备注:dao层写法 :getAllListPage(@Param("params") Map<String, Object> params); )
(备注2:xml非空判断
<if test="params.instid != null and params.instid != '' "> and instr(ttr.instid, #{params.instid}) >0</if> )
/**
* 删除值为null || 空的键
*
* @param map
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> removeBlankValue(Map<K, V> map) {
if (MapUtil.isEmpty(map)) {
return map;
} else {
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<K, V> entry = (Map.Entry) iter.next();
if ((ObjectUtil.isAllEmpty(entry.getValue()))) {
iter.remove();
}
}
return map;
}
}