generator 配置文件
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 自定义注释生成器 MybatisGeneratorCommon类为我自定义的继承CommentGenerator的类 -->
<!--<commentGenerator type="com.datasoft.basis.config.GeneratorPluginConfig">-->
<commentGenerator >
<!-- 关闭自动生成的注释 -->
<property name="suppressAllComments" value="false" />
<property name="suppressDate" value="true" />
<property name="javaFileEncoding" value="UTF-8"/>
</commentGenerator>
<!-- 定义如何连接目标数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/basis?useSSL=false"
userId="root"
password="12345678">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定生成 Java 模型对象所属的包 -->
<javaModelGenerator targetPackage="com.datasoft.basis.bean" targetProject="src/main/java">
<property name="constructorBased" value="false"/>
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- 指定生成 SQL 映射文件所属的包和的目标项目 -->
<sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 指定目标包和目标项目生成的客户端接口和类 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.datasoft.basis.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 设置要生成的表名 -->
<table tableName="role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
在pom.xml中引入generator插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
</plugin>
下载generator源码
https://github.com/mybatis/generator
修改源码的默认注释类DefaultCommentGenerator.java
public class DefaultCommentGenerator implements CommentGenerator {
private Properties properties;
private boolean suppressDate;
private boolean suppressAllComments;
private boolean addRemarkComments;
private SimpleDateFormat dateFormat;
public DefaultCommentGenerator() {
super();
properties = new Properties();
suppressDate = false;
suppressAllComments = false;
addRemarkComments = false;
}
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
}
@Override
public void addComment(XmlElement xmlElement) {
}
@Override
public void addRootComment(XmlElement rootElement) {
}
@Override
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));
addRemarkComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
if (StringUtility.stringHasValue(dateFormatString)) {
dateFormat = new SimpleDateFormat(dateFormatString);
}
}
@Override
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable) {
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
String remarks = introspectedTable.getRemarks();
//添加导入类的信息
topLevelClass.addJavaDocLine("import org.springframework.format.annotation.DateTimeFormat;");
topLevelClass.addJavaDocLine("import com.fasterxml.jackson.annotation.JsonFormat;");
topLevelClass.addJavaDocLine("import lombok.*;");
topLevelClass.addJavaDocLine("\n");
//添加类注释
topLevelClass.addJavaDocLine("/**");
String author = " ";
try {
author = InetAddress.getLocalHost().getHostName();
if(author.contains(".")){
author = author.substring(0,author.indexOf("."));
}
} catch (UnknownHostException e) {
}
topLevelClass.addJavaDocLine(" * " + remarks);
topLevelClass.addJavaDocLine(" * 实体类对应的数据表为: " + introspectedTable.getFullyQualifiedTable());
topLevelClass.addJavaDocLine(" * " );
topLevelClass.addJavaDocLine(" * @author " + author);
//添加时间
topLevelClass.addJavaDocLine(" * @date " + getDateString());
topLevelClass.addJavaDocLine(" */");
//添加注解
topLevelClass.addJavaDocLine("@ToString");
topLevelClass.addJavaDocLine("@EqualsAndHashCode");
topLevelClass.addJavaDocLine("@AllArgsConstructor");
topLevelClass.addJavaDocLine("@NoArgsConstructor");
topLevelClass.addJavaDocLine("@Builder");
topLevelClass.addJavaDocLine("@RequiredArgsConstructor");
}
@Override
public void addEnumComment(InnerEnum innerEnum,
IntrospectedTable introspectedTable) {
}
@Override
public void addFieldComment(Field field,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
//判断数据库中该字段注释是否为空
String remarks = introspectedColumn.getRemarks();
if(remarks == null || "".equals(remarks.trim())){
if(introspectedColumn.getJdbcType() == 93) {
field.addJavaDocLine("@DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")");
}else{
field.addJavaDocLine("@NonNull");
}
return;
}
field.addJavaDocLine("/**");
field.addJavaDocLine("*"+remarks);
field.addJavaDocLine("*/");
if(introspectedColumn.getJdbcType() == 93) {
field.addJavaDocLine("@DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")");
}else {
field.addJavaDocLine("@NonNull");
}
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
}
@Override
public void addGeneralMethodComment(Method method,
IntrospectedTable introspectedTable) {
}
@Override
public void addGetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
if(introspectedColumn.getJdbcType() == 93) {
method.addJavaDocLine("@JsonFormat(locale = \"zh\", timezone = \"GMT+8\", pattern = \"yyyy-MM-dd HH:mm:ss\")");
}
}
@Override
public void addSetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
}
@Override
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
}
}
打包源码 并替换 仓库的org/mybatis/generator/mybatis-generator-core包
修改配置文件中的要生成的表名 使用插件生成对应的文件