- 代码
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
public class MyCommentGenerator implements CommentGenerator {
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String currentDateStr;
public MyCommentGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}
public void addJavaFileComment(CompilationUnit compilationUnit) {
// add no file level comments by default
return;
}
/**
* Adds a suitable comment to warn users that the element was generated, and
* when it was generated.
*/
public void addComment(XmlElement xmlElement) {
return;
}
public void addRootComment(XmlElement rootElement) {
// add no document level comments by default
return;
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
}
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
}
/**
* This method adds the custom javadoc tag for. You may do nothing if you do
* not wish to include the Javadoc tag - however, if you do not include the
* Javadoc tag then the Java merge capability of the eclipse plugin will
* break.
*
* @param javaElement the java element
*/
protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
javaElement.addJavaDocLine(" *");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge");
}
String s = getDateString();
if (s != null) {
sb.append(' ');
sb.append(s);
}
javaElement.addJavaDocLine(sb.toString());
}
/**
* This method returns a formated date string to include in the Javadoc tag
* and XML comments. You may return null if you do not want the date in
* these documentation elements.
*
* @return a string representing the current timestamp, or null
*/
protected String getDateString() {
String result = null;
if (!suppressDate) {
result = currentDateStr;
}
return result;
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append(" ");
sb.append(getDateString());
innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
innerClass.addJavaDocLine(" */");
}
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString().replace("\n", " "));
innerEnum.addJavaDocLine(" */");
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
public void addGetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
sb.append(" * @return ");
sb.append(introspectedColumn.getActualColumnName());
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString().replace("\n", " "));
method.addJavaDocLine(" */");
}
public void addSetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString().replace("\n", " "));
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param ");
sb.append(parm.getName());
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString().replace("\n", " "));
method.addJavaDocLine(" */");
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
sb.append(" * @author ");
sb.append(systemPro.getProperty("user.name"));
sb.append(" ");
sb.append(currentDateStr);
innerClass.addJavaDocLine(" */");
}
}
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Startup {
public static void main(String[] args) {
try {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("generatorConfig_trade.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、generator.properties
jdbc.driverLocation=D:\\maven_bin\\mysql\\mysql-connector-java\\5.1.35\\mysql-connector-java-5.1.35.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://172.18.1.1:3306/cts_trade?autoReconnect=true&useUnicode=true&characterEncoding=UTF8&
jdbc.userId=cts_trade
jdbc.password=qeBhFjJ3iXKLU39yS53wZSUb00INsLPc
#jdbc.userId=cts_acc
#jdbc.password=dA4KB7jMJTX8d2SZYRIr0IX8Xx6Srp4t
#jdbc.userId=cts_batch
#jdbc.password=mh1nNS9seEVBKlJEe82Lmx2y4ygZaka2
targetProject2Java=D:\\workspace\\mybatis-code\\cts_trade
targetProject2XML=D:\\workspace\\mybatis-code\\cts_trade
3、generatorConfig_trade.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--导入属性配置 -->
<properties resource="generator.properties"></properties>
<!--指定特定数据库的jdbc驱动jar包的位置 -->
<classPathEntry location="${jdbc.driverLocation}"/>
<context id="default" targetRuntime="MyBatis3">
<!-- 自定义物理分页 可生成支持Mysql数据的limit 不支持Oracle -->
<!--<plugin type="org.mybatis.generator.plugins.page.PaginationPlugin" />-->
<!-- 自定义查询指定字段 -->
<!--<plugin type="org.mybatis.generator.plugins.field.FieldsPlugin" />-->
<!-- 此处是将Example改名为Criteria 当然 想改成什么都行~ -->
<!--<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">-->
<!--<property name="searchString" value="Example$" />-->
<!--<!– 替换后-->
<!--<property name="replaceString" value="Criteria" />-->
<!--–>-->
<!--<property name="replaceString" value="Query" />-->
<!--</plugin>-->
<!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator type="generate.util.MyCommentGenerator">
<!--<property name="suppressDate" value="true"/> -->
<!--<property name="suppressAllComments" value="true"/> -->
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetProject="${targetProject2Java}"
targetPackage="com.fqfin.trade.model">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="false"/>
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetProject="${targetProject2XML}"
targetPackage="mapper">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成map xml对应client,也就是接口dao -->
<!-- type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java
Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetProject="${targetProject2Java}"
targetPackage="com.fqfin.trade.dao" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- <table tableName="t_user_info" schema="cif"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
去除T开头
<domainObjectRenamingRule searchString="^T" replaceString="" />
</table>-->
<!--cts-trade-->
<!--<table tableName="t_sys_param">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_sys_param_log">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_user_apply">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>-->
<!--<table tableName="t_user_loan">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_repayment">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_penalty_batch_log">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_repayment_batch_log">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_update_repayment_flow">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_offline_repayment_info">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_offline_repayment_audit_flow">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_withhold_recode">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_withhold_repayment">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_loan_flow">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_loan_trade_reversal">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_repay_trade_reversal">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_trade_reversal_audit_flow">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>-->
<!--<table tableName="t_offline_repayment_info">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_offline_repayment_audit_flow">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_loan_trade_reversal">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_repay_trade_reversal">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
<table tableName="t_trade_reversal_audit_flow">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>-->
<table tableName="t_notice_fun_repayment">
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
</context>
</generatorConfiguration>
3、pox.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.code</groupId>
<artifactId>mybatis-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis_generator</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<version>3.3</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
</dependencies>
<configuration> <!--配置文件的路径-->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

版权声明:本文为m0_38068812原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。