From 4bb3a9bdcc86953d3fba04a7be9abd8b3bfb6a15 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期三, 07 八月 2024 17:31:04 +0800 Subject: [PATCH] 新增-查询分页优化 --- src/main/java/com/hx/mybatisTool/SqlSentence.java | 70 +++++++++++++++++++++++++++++++++++ 1 files changed, 70 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/mybatisTool/SqlSentence.java b/src/main/java/com/hx/mybatisTool/SqlSentence.java index dd7c9a7..882a2a9 100644 --- a/src/main/java/com/hx/mybatisTool/SqlSentence.java +++ b/src/main/java/com/hx/mybatisTool/SqlSentence.java @@ -26,6 +26,8 @@ private Integer startPage = 0; /**姣忛〉鏁伴噺*/ private Integer pageNum = 0; + /**鏉℃暟锛堝綋鍓嶇被浣跨敤锛�*/ + private Integer pageSize; ////////////////////////////////////////////////////////////// @@ -88,6 +90,66 @@ sqlSentence = sql; m = values; } + + /** + * sql鏁存潯璇彞-鍒嗛〉 + * @param sql 濡傦細select * from user Where name = #{m.userName} order by age desc + * @param values 瀛樻斁鐨勫�煎锛歷alues.put("userName","ChenJiaHe") + * @param pageNum 褰撳墠椤垫暟 + * @param pageSize 褰撳墠鏉℃暟 + */ + public void sqlSentencePage(String sql,Map<String,Object> values,int pageNum,int pageSize) { + if(StringUtils.isEmpty(sql)) { + throw new TipsException("sql is null"); + } + + if(values == null) { + throw new TipsException("values is null"); + } + if(pageNum < 1){ + pageNum = 1; + } + if(pageSize < 1){ + pageSize = 1; + } + this.pageNum = pageNum; + this.pageSize = pageSize; + + //绠楄捣鐐� + int pageStart = (pageNum-1)*pageSize; + sql = sql+" LIMIT "+pageStart+","+pageSize; + sqlSentence = sql; + m = values; + } + + /**鍒嗛〉缁勮淇℃伅 + * @param data 瑁呰浇淇℃伅 + * @param totalNum 鎬婚〉鏁� + * @return 瑁呰浇淇℃伅 + */ + public Map<String,Object> pageAssembleData(Map<String,Object> data,int totalNum){ + data.put("pageNum",pageNum); + data.put("pageSize",pageSize); + data.put("total",totalNum); + int pages; + if(pageSize > 0){ + if(totalNum%pageSize > 0){ + pages = totalNum/pageSize+1; + }else{ + pages = totalNum/pageSize; + } + }else{ + pages = 0; + } + data.put("pages",pages); + if(pages <= pageNum){ + data.put("isLastPage",true); + }else{ + data.put("isLastPage",false); + } + return data; + } + /** * 鏌ヨ鐨勮鍙� @@ -176,6 +238,14 @@ } this.whereExist = whereExist; } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } } -- Gitblit v1.8.0