增加mybatis生成分页的插件

This commit is contained in:
shuzheng 2016-10-17 14:36:55 +08:00
parent 4e48fc4aae
commit 8743d01bff
3 changed files with 331 additions and 3 deletions

View File

@ -57,9 +57,9 @@
<version>${mybatis-spring.version}</version> <version>${mybatis-spring.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.pagehelper</groupId> <groupId>org.mybatis.generator</groupId>
<artifactId>pagehelper</artifactId> <artifactId>mybatis-generator-core</artifactId>
<version>4.1.6</version> <version>1.3.5</version>
</dependency> </dependency>
<!-- 缓存 --> <!-- 缓存 -->
<dependency> <dependency>

View File

@ -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<String> list) {
return true;
}
/**
* 为每个Example类添加limit和offset属性已经setget方法
*/
@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;
}
}

View File

@ -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("<a class=\"prev\" href=\"" + url + "&" + param + "=" + (page - 1) + "\">上一页</a>\n");
} else {
pages = pages.concat("<a class=\"prev\" href=\"" + url + "?" + param + "=" + (page - 1) + "\">上一页</a>\n");
}
} else {
// pages =
// pages.concat("<a class=\"prev\" href=\"javascript:;\" style=\"color:#ccc\">上一页</a>\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("<a href=\"" + url + "&" + param + "=1\">1</a> ... \n");
} else {
pages = pages.concat("<a href=\"" + url + "?" + param + "=1\">1</a> ... \n");
}
}
// 当前页数右侧还有未显示页码时
if (pageCount - page >= page - listBegin) {
for (int i = listBegin; i < (listBegin + step); i++) {
if (i != page) {
if (url.contains("?")) {
pages = pages.concat("<a href=\"" + url + "&" + param + "=" + i + "\">" + i + "</a>\n");
} else {
pages = pages.concat("<a href=\"" + url + "?" + param + "=" + i + "\">" + i + "</a>\n");
}
} else {
pages = pages.concat("<span class=\"current\">" + i + "</span>\n");
}
}
// 显示最后1页
if (listBegin + step <= pageCount) {
if (url.contains("?")) {
pages = pages.concat(" ... <a href=\"" + url + "&" + param + "=" + pageCount + "\">" + pageCount + "</a>\n");
} else {
pages = pages.concat(" ... <a href=\"" + url + "?" + param + "=" + pageCount + "\">" + pageCount + "</a>\n");
}
}
} else { // 显示最后剩余的几个页码
for (int i = (pageCount - step) + 1; i <= pageCount; i++) {
if (i != page) {
if (url.contains("?")) {
pages = pages.concat("<a href=\"" + url + "&" + param + "=" + i + "\">" + i + "</a>\n");
} else {
pages = pages.concat("<a href=\"" + url + "?" + param + "=" + i + "\">" + i + "</a>\n");
}
} else {
pages = pages.concat("<span class=\"current\">" + i + "</span>\n");
}
}
}
} else { // 总页数小于等于step时直接显示
for (int i = 1; i <= pageCount; i++) {
if (i != page) {
if (url.contains("?")) {
pages = pages.concat("<a href=\"" + url + "&" + param + "=" + i + "\">" + i + "</a>\n");
} else {
pages = pages.concat("<a href=\"" + url + "?" + param + "=" + i + "\">" + i + "</a>\n");
}
} else {
pages = pages.concat("<span class=\"current\">" + i + "</span>\n");
}
}
}
// 显示下一页
if (page < pageCount) {
if (url.contains("?")) {
pages = pages.concat("<a class=\"next\" href=\"" + url + "&" + param + "=" + (page + 1) + "\">下一页</a>\n");
} else {
pages = pages.concat("<a class=\"next\" href=\"" + url + "?" + param + "=" + (page + 1) + "\">下一页</a>\n");
}
} else {
// pages =
// pages.concat("<a class=\"next\" href=\"javascript:;\" style=\"color:#ccc\">下一页</a>\n");
}
return pages;
}
}