Skip to content

Instantly share code, notes, and snippets.

@piglovesx
Created November 2, 2015 03:42
Show Gist options
  • Save piglovesx/e222ed580e3b7809b1bc to your computer and use it in GitHub Desktop.
Save piglovesx/e222ed580e3b7809b1bc to your computer and use it in GitHub Desktop.
mybatis generator 加入mysql分页
package org.mybatis.generator.plugins;
import org.mybatis.generator.api.ProgressCallback;
import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl;
import java.util.List;
/**
* Created by Administrator on 2015/10/29.
*/
public class IntrospectedTableMyWay3Impl extends IntrospectedTableMyBatis3Impl{
@Override
protected void calculateXmlMapperGenerator(AbstractJavaClientGenerator javaClientGenerator, List<String> warnings, ProgressCallback progressCallback) {
this.xmlMapperGenerator = new SqlMapperExtend();
this.initializeAbstractGenerator(this.xmlMapperGenerator, warnings, progressCallback);
}
}
package org.mybatis.generator.plugins;
/**
* Created by Administrator on 2015/10/27.
*/
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.*;
import java.util.List;
/**
* Created by Administrator on 2015/10/27.
*/
public class PagePlugin extends PluginAdapter{
@Override
public boolean validate(List<String> list) {
if (new Boolean(this.getContext().getSqlMapGeneratorConfiguration().getProperties().getProperty("pagination")))
return true;
return false;
}
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
FullyQualifiedJavaType type = new FullyQualifiedJavaType("com.dyyt.framework.bean.Page");
topLevelClass.addImportedType(type);
Field field = new Field();
field.setName("page");
field.setVisibility(JavaVisibility.PUBLIC);
field.setType(type);
topLevelClass.addField(field);
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("setPage");
method.addParameter(new Parameter(type, "page"));
method.addBodyLine("this.page = page;");
topLevelClass.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("getPage");
method.setReturnType(type);
method.addBodyLine("return page;");
topLevelClass.addMethod(method);
return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
}
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
element.addElement(5, createSqlPagePrefix());
element.addElement(createSqlPageSuffix());
return true;
}
XmlElement createSqlPagePrefix() {
XmlElement element = new XmlElement("include");
element.addAttribute(new Attribute("refid", "Pageable_Prefix"));
return element;
}
XmlElement createSqlPageSuffix() {
XmlElement element = new XmlElement("include");
element.addAttribute(new Attribute("refid", "Pageable_Suffix"));
return element;
}
}
package org.mybatis.generator.plugins;
import org.mybatis.generator.api.FullyQualifiedTable;
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 org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator;
import org.mybatis.generator.internal.util.messages.Messages;
/**
* Created by Administrator on 2015/10/29.
*/
public class SqlMapperExtend extends XMLMapperGenerator{
@Override
protected XmlElement getSqlMapElement() {
FullyQualifiedTable table = this.introspectedTable.getFullyQualifiedTable();
this.progressCallback.startTask(Messages.getString("Progress.12", table.toString()));
XmlElement answer = new XmlElement("mapper");
String namespace = this.introspectedTable.getMyBatis3SqlMapNamespace();
answer.addAttribute(new Attribute("namespace", namespace));
this.context.getCommentGenerator().addRootComment(answer);
this.addResultMapWithoutBLOBsElement(answer);
this.addResultMapWithBLOBsElement(answer);
this.addExampleWhereClauseElement(answer);
this.addMyBatis3UpdateByExampleWhereClauseElement(answer);
this.addBaseColumnListElement(answer);
this.addBlobColumnListElement(answer);
this.addSelectByExampleWithBLOBsElement(answer);
this.addSelectByExampleWithoutBLOBsElement(answer);
this.addSelectByPrimaryKeyElement(answer);
this.addDeleteByPrimaryKeyElement(answer);
this.addDeleteByExampleElement(answer);
this.addInsertElement(answer);
this.addInsertSelectiveElement(answer);
this.addCountByExampleElement(answer);
this.addUpdateByExampleSelectiveElement(answer);
this.addUpdateByExampleWithBLOBsElement(answer);
this.addUpdateByExampleWithoutBLOBsElement(answer);
this.addUpdateByPrimaryKeySelectiveElement(answer);
this.addUpdateByPrimaryKeyWithBLOBsElement(answer);
this.addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
if (new Boolean(this.getContext().getSqlMapGeneratorConfiguration().getProperties().getProperty("pagination"))) {
answer.addElement(createPagePrefix());
answer.addElement(createPateSuffix());
}
return answer;
}
XmlElement createPagePrefix() {
XmlElement sqlX = new XmlElement("sql");
sqlX.addElement(new TextElement("<!-- 分页前的操作 -->"));
sqlX.addAttribute(new Attribute("id", "Pageable_Prefix"));
XmlElement sqlXif = new XmlElement("if");
sqlXif.addAttribute(new Attribute("test", "page != null"));
sqlXif.addElement(new TextElement("<!-- -->"));
sqlX.addElement(sqlXif);
return sqlX;
}
XmlElement createPateSuffix() {
XmlElement sqlX1 = new XmlElement("sql");
sqlX1.addElement(new TextElement("<!-- 分页语句 -->"));
sqlX1.addAttribute(new Attribute("id", "Pageable_Suffix"));
XmlElement sqlXif = new XmlElement("if");
sqlXif.addAttribute(new Attribute("test", "page != null"));
sqlXif.addElement(new TextElement("limit #{page.begin}, #{page.size}"));
sqlX1.addElement(sqlXif);
return sqlX1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment