diff --git a/common/pom.xml b/common/pom.xml index 903218cb..d3f31434 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -57,9 +57,9 @@ ${mybatis-spring.version} - com.github.pagehelper - pagehelper - 4.1.6 + org.mybatis.generator + mybatis-generator-core + 1.3.5 diff --git a/common/src/main/java/com/zheng/common/plugin/PaginationPlugin.java b/common/src/main/java/com/zheng/common/plugin/PaginationPlugin.java new file mode 100644 index 00000000..d6fe3253 --- /dev/null +++ b/common/src/main/java/com/zheng/common/plugin/PaginationPlugin.java @@ -0,0 +1,99 @@ +package com.zheng.common.plugin; + +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.PluginAdapter; +import org.mybatis.generator.api.dom.java.*; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.XmlElement; + +import java.util.List; + +/** + * MySQL分页插件 + * Created by ZhangShuzheng on 2016/10/17. + */ +public class PaginationPlugin extends PluginAdapter { + + @Override + public boolean validate(List list) { + return true; + } + + /** + * 为每个Example类添加limit和offset属性已经set、get方法 + */ + @Override + public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + + PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper(); + + Field limit = new Field(); + limit.setName("limit"); + limit.setVisibility(JavaVisibility.PRIVATE); + limit.setType(integerWrapper); + topLevelClass.addField(limit); + + Method setLimit = new Method(); + setLimit.setVisibility(JavaVisibility.PUBLIC); + setLimit.setName("setLimit"); + setLimit.addParameter(new Parameter(integerWrapper, "limit")); + setLimit.addBodyLine("this.limit = limit;"); + topLevelClass.addMethod(setLimit); + + Method getLimit = new Method(); + getLimit.setVisibility(JavaVisibility.PUBLIC); + getLimit.setReturnType(integerWrapper); + getLimit.setName("getLimit"); + getLimit.addBodyLine("return limit;"); + topLevelClass.addMethod(getLimit); + + Field offset = new Field(); + offset.setName("offset"); + offset.setVisibility(JavaVisibility.PRIVATE); + offset.setType(integerWrapper); + topLevelClass.addField(offset); + + Method setOffset = new Method(); + setOffset.setVisibility(JavaVisibility.PUBLIC); + setOffset.setName("setOffset"); + setOffset.addParameter(new Parameter(integerWrapper, "offset")); + setOffset.addBodyLine("this.offset = offset;"); + topLevelClass.addMethod(setOffset); + + Method getOffset = new Method(); + getOffset.setVisibility(JavaVisibility.PUBLIC); + getOffset.setReturnType(integerWrapper); + getOffset.setName("getOffset"); + getOffset.addBodyLine("return offset;"); + topLevelClass.addMethod(getOffset); + + return true; + } + + /** + * 为Mapper.xml的selectByExample添加limit + */ + @Override + public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, + IntrospectedTable introspectedTable) { + + XmlElement ifLimitNotNullElement = new XmlElement("if"); + ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null")); + + XmlElement ifOffsetNotNullElement = new XmlElement("if"); + ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null")); + ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}")); + ifLimitNotNullElement.addElement(ifOffsetNotNullElement); + + XmlElement ifOffsetNullElement = new XmlElement("if"); + ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null")); + ifOffsetNullElement.addElement(new TextElement("limit ${limit}")); + ifLimitNotNullElement.addElement(ifOffsetNullElement); + + element.addElement(ifLimitNotNullElement); + + return true; + } + +} diff --git a/common/src/main/java/com/zheng/common/util/Paginator.java b/common/src/main/java/com/zheng/common/util/Paginator.java new file mode 100644 index 00000000..716ab121 --- /dev/null +++ b/common/src/main/java/com/zheng/common/util/Paginator.java @@ -0,0 +1,229 @@ +package com.zheng.common.util; + + +/** + * 分页实体类 + * @author shuzheng + * @date 2016年7月6日 下午6:05:00 + */ +public class Paginator { + + private long total = 0l; // 总记录数 + private int page = 1; // 当前页数 + private long totalPage = 1; // 总页数 + private int rows = 10; // 每页记录数 + private int step = 5; // 最多显示分页页码数 + private String param = "page"; // 分页参数名称,用于支持一个页面多个分页功能 + private String url = ""; // 项目路径 + private String query = ""; // 当前页所有参数 + + public long getTotal() { + return total; + } + + public void setTotal(long total) { + this.total = total; + this.initTotalPage(); + } + + public int getPage() { + return page; + } + + public void setPage(int page) { + this.page = page; + } + + public long getTotalPage() { + return totalPage; + } + + public void setTotalPage(long totalPage) { + this.totalPage = totalPage; + } + + public int getRows() { + return rows; + } + + public void setRows(int rows) { + // 设置个最大记录数,限制单页记录过多 + if (rows > 10000) { + rows = 10000; + } + this.rows = rows; + this.initTotalPage(); + } + + public int getStep() { + return step; + } + + public void setStep(int step) { + this.step = step; + } + + public String getParam() { + return param; + } + + public void setParam(String param) { + this.param = param; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getQuery() { + return query; + } + + public void setQuery(String query) { + this.query = query; + } + + /** + * 初始化分页信息 + */ + public void initTotalPage() { + totalPage = (total % rows) == 0 ? (total / rows) : ((total / rows) + 1); + if (page > totalPage) { + page = (int) totalPage; + } + if (page < 1) { + page = 1; + } + } + + /** + * 生成简单的分页页面内容 + * @return + */ + public String getHtml() { + // 根据request获取当前url,包括参数,如果有已存在名称未paramname的参数,剔除掉,后面会追加新的参数 + //String contextPath = request.getContextPath(); + //String requestURI = request.getRequestURI(); + //String url = contextPath + requestURI; + //String url = request.getRequestURI(); + //String query = request.getQueryString(); + if (query != null) { + String params = ""; + String[] querys = query.split("&"); + for (int i = 0; i < querys.length; i++) { + if (querys[i].startsWith(param)) + continue; + if (params.equals("")) + params += querys[i]; + else + params += "&" + querys[i]; + } + if (!params.equals("")) + url += "?" + params; + } + // 结果html + String pages = ""; + + int pageCount = (int) Math.ceil((double) total / rows);// 求总页数 + if (pageCount <= 1) { + return pages; + } + if (page > pageCount) { + page = pageCount;// 如果分页变量大总页数,则将分页变量设计为总页数 + } + if (page <= 0) { + page = 1;// 如果分页变量小于1,则将分页变量设为1 + } + + // 显示上一页 + if (page > 1) { + if (url.contains("?")) { + pages = pages.concat("上一页\n"); + } else { + pages = pages.concat("上一页\n"); + } + } else { + // pages = + // pages.concat("上一页\n"); + } + // 如果总页数大于要显示的个数,则拼接显示 + if (pageCount > step) { + // 显示分页码 + int listBegin = (page - (int) Math.floor((double) step / 2));// 从第几页开始显示分页信息 + if (listBegin < 1) { + listBegin = 1; + } + // 显示第1页 + if (listBegin >= 2) { + if (url.contains("?")) { + pages = pages.concat("1 ... \n"); + } else { + pages = pages.concat("1 ... \n"); + } + } + // 当前页数右侧还有未显示页码时 + if (pageCount - page >= page - listBegin) { + for (int i = listBegin; i < (listBegin + step); i++) { + if (i != page) { + if (url.contains("?")) { + pages = pages.concat("" + i + "\n"); + } else { + pages = pages.concat("" + i + "\n"); + } + } else { + pages = pages.concat("" + i + "\n"); + } + } + // 显示最后1页 + if (listBegin + step <= pageCount) { + if (url.contains("?")) { + pages = pages.concat(" ... " + pageCount + "\n"); + } else { + pages = pages.concat(" ... " + pageCount + "\n"); + } + } + } else { // 显示最后剩余的几个页码 + for (int i = (pageCount - step) + 1; i <= pageCount; i++) { + if (i != page) { + if (url.contains("?")) { + pages = pages.concat("" + i + "\n"); + } else { + pages = pages.concat("" + i + "\n"); + } + } else { + pages = pages.concat("" + i + "\n"); + } + } + } + } else { // 总页数小于等于step时,直接显示 + for (int i = 1; i <= pageCount; i++) { + if (i != page) { + if (url.contains("?")) { + pages = pages.concat("" + i + "\n"); + } else { + pages = pages.concat("" + i + "\n"); + } + } else { + pages = pages.concat("" + i + "\n"); + } + } + } + // 显示下一页 + if (page < pageCount) { + if (url.contains("?")) { + pages = pages.concat("下一页\n"); + } else { + pages = pages.concat("下一页\n"); + } + } else { + // pages = + // pages.concat("下一页\n"); + } + return pages; + } + +}