generatorConfig.xml MyBatisGeneratorUtils.java package com.school.zhibotest.util;import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.
MyBatisGeneratorUtils.java
package com.school.zhibotest.util; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; /** * 反向生成工具类 * @author qiaoliang * */ public class MyBatisGeneratorUtils { public static void main(String[] args) { try { ListMySQLLimitPlugin.javawarnings = new ArrayList (); boolean overwrite = true; File configFile = new File(MyBatisGeneratorUtils.class.getResource("/generatorConfig.xml").getFile()); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); System.err.println("---------请刷新!!!-----------"); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.school.zhibotest.util; import java.util.List; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.JavaVisibility; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.Parameter; import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper; import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; /** *pom.xml【】【2017年11月30日 上午9:49:02】
* @author QL * */ public class MySQLLimitPlugin extends PluginAdapter { @Override public boolean validate(Listlist) { return true; } /** * 为每个Example类添加limitStart和limitEnd属性已经set、get方法 */ @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper(); Field limit = new Field(); limit.setName("limitStart"); limit.setVisibility(JavaVisibility.PRIVATE); limit.setType(integerWrapper); topLevelClass.addField(limit); Method setLimit = new Method(); setLimit.setVisibility(JavaVisibility.PUBLIC); setLimit.setName("setLimitStart"); setLimit.addParameter(new Parameter(integerWrapper, "limitStart")); setLimit.addBodyLine("this.limitStart = limitStart;"); topLevelClass.addMethod(setLimit); Method getLimit = new Method(); getLimit.setVisibility(JavaVisibility.PUBLIC); getLimit.setReturnType(integerWrapper); getLimit.setName("getLimitStart"); getLimit.addBodyLine("return limitStart;"); topLevelClass.addMethod(getLimit); Field offset = new Field(); offset.setName("limitEnd"); offset.setVisibility(JavaVisibility.PRIVATE); offset.setType(integerWrapper); topLevelClass.addField(offset); Method setOffset = new Method(); setOffset.setVisibility(JavaVisibility.PUBLIC); setOffset.setName("setLimitEnd"); setOffset.addParameter(new Parameter(integerWrapper, "limitEnd")); setOffset.addBodyLine("this.limitEnd = limitEnd;"); topLevelClass.addMethod(setOffset); Method getOffset = new Method(); getOffset.setVisibility(JavaVisibility.PUBLIC); getOffset.setReturnType(integerWrapper); getOffset.setName("getLimitEnd"); getOffset.addBodyLine("return limitEnd;"); 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", "limitEnd != null")); XmlElement ifOffsetNotNullElement = new XmlElement("if"); ifOffsetNotNullElement.addAttribute(new Attribute("test", "limitStart != null")); ifOffsetNotNullElement.addElement(new TextElement("limit ${limitStart}, ${limitEnd}")); ifLimitNotNullElement.addElement(ifOffsetNotNullElement); XmlElement ifOffsetNullElement = new XmlElement("if"); ifOffsetNullElement.addAttribute(new Attribute("test", "limitStart == null")); ifOffsetNullElement.addElement(new TextElement("limit ${limitEnd}")); ifLimitNotNullElement.addElement(ifOffsetNullElement); element.addElement(ifLimitNotNullElement); return true; } }
gistfile1.txt4.0.0 com.school zhibowar 0.0.1-SNAPSHOT zhibo Maven Webapp http://maven.apache.org 4.3 xxg-repository http://repo.maven.xxgblog.com/ org.webjars.bower jquery3.0.0-alpha1 org.apache.httpcomponents httpclient${httpclient.version} org.apache.httpcomponents httpmime${httpclient.version} org.apache.httpcomponents httpasyncclient4.1 org.json json20140107 junit junit3.8.1 test commons-codec commons-codec1.10 commons-httpclient commons-httpclient3.1 javax.servlet javax.servlet-api3.1.0 provided org.mybatis.generator mybatis-generator-core1.3.5 jstl jstl1.2 taglibs standard1.1.2 mysql mysql-connector-java5.1.37 runtime c3p0 c3p00.9.1.2 org.mybatis mybatis3.3.0 org.mybatis mybatis-spring1.2.3 taglibs standard1.1.2 jstl jstl1.2 com.fasterxml.jackson.core jackson-databind2.5.4 javax.servlet javax.servlet-api3.1.0 org.springframework spring-core4.1.7.RELEASE org.springframework spring-beans4.1.7.RELEASE org.springframework spring-context4.1.7.RELEASE org.springframework spring-jdbc4.1.7.RELEASE org.springframework spring-tx4.1.7.RELEASE org.springframework spring-web4.1.7.RELEASE org.springframework spring-webmvc4.1.7.RELEASE org.springframework spring-test4.1.7.RELEASE redis.clients jedis2.7.3 com.dyuproject.protostuff protostuff-core1.0.8 com.dyuproject.protostuff protostuff-runtime1.0.8 commons-collections commons-collections3.2 org.bgee.log4jdbc-log4j2 log4jdbc-log4j2-jdbc4.11.16 org.slf4j slf4j-api1.7.13 org.slf4j slf4j-log4j121.7.13 org.logback-extensions logback-ext-spring0.1.4 org.slf4j slf4j-api1.6.4 org.slf4j jcl-over-slf4j1.6.4 ch.qos.logback logback-core1.1.6 ch.qos.logback logback-access1.1.6 ch.qos.logback logback-classic1.1.6 zhibo org.mortbay.jetty jetty-maven-plugin8.1.14.v20131031 30 / src/main/resources/jetty-deploy.xml org.apache.maven.plugins maven-compiler-plugin3.1 org.mybatis.generator mybatis-generator-maven-plugin1.3.2 mysql mysql-connector-java5.1.34 com.xxg mybatis-generator-plugin1.0.0 true
用法 将该插件添加到MyBatis Generator配置文件中。 与Maven一起运行 的pom.xml ... xxg-repository http://repo.maven.xxgblog.com/ 运行mvn mybatis-generator:generator生成java和xml文件。 org.mybatis.generator mybatis-generator-maven-plugin1.3.2 mysql mysql-connector-java5.1.34 com.xxg mbg-limit-plugin1.0.0 true