This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eca01af
Showing
7 changed files
with
1,086 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.gradle | ||
!gradle/wrapper/gradle-wrapper.jar | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
|
||
### NetBeans ### | ||
nbproject/private/ | ||
build/ | ||
nbbuild/ | ||
dist/ | ||
nbdist/ | ||
.nb-gradle/ | ||
|
||
### JRebel ### | ||
rebel.xml |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# learn-boot-step1 | ||
--- | ||
|
||
## 简介 | ||
这是用于 [MyBatis Generator](http://www.mybatis.org/generator/) 中生成注释这一步骤的自定义生成器,默认将数据库注释作为 Java 注释,并支持在生成的 Java 代码中附加注解 | ||
|
||
## 示例 | ||
``` | ||
<?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="package/to/your/data_source.properties"/> | ||
<context id="context_mysql" defaultModelType="flat" targetRuntime="MyBatis3"> | ||
<property name="javaFileEncoding" value="UTF-8"/> | ||
<!-- CommentWithAnnotationCommentGenerator --> | ||
<commentGenerator type="io.github.since1986.mybatis.comment.generator.CommentWithAnnotationCommentGenerator"> | ||
<property name="fieldAnnotationFullyQualifiedNames" value="one.your.customer.FieldAnnotationClass,another.your.customer.FieldAnnotationClass"/> | ||
<property name="classAnnotationFullyQualifiedNames" value="one.your.customer.ClassAnnotationClass,another.your.customer.ClassAnnotationClass"/> | ||
</commentGenerator> | ||
<jdbcConnection driverClass="${driverClass}" connectionURL="${connectionURL}" userId="${userId}" password="${password}"> | ||
<!-- better keep this --> | ||
<property name="useInformationSchema" value="true"/> | ||
</jdbcConnection> | ||
<javaModelGenerator targetPackage="your.model" targetProject="/path/to/your/project/src/main/java"> | ||
</javaModelGenerator> | ||
<sqlMapGenerator targetPackage="your.mapper" targetProject="src/main/resources"> | ||
</sqlMapGenerator> | ||
<javaClientGenerator targetPackage="your.mapper" type="ANNOTATEDMAPPER" targetProject="src/main/java"> | ||
</javaClientGenerator> | ||
<table tableName="your_table" domainObjectName="YourDomain"> | ||
</table> | ||
</context> | ||
</generatorConfiguration> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>io.github.since1986</groupId> | ||
<artifactId>mybatis-comment-generator</artifactId> | ||
<version>0.1</version> | ||
|
||
<licenses> | ||
<license> | ||
<name>GNU General Public License v3.0</name> | ||
<url>http://www.gnu.org/licenses/agpl-3.0.html</url> | ||
<distribution>repo</distribution> | ||
<comments>A mybatis-comment-generator</comments> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<name>since1986</name> | ||
<url>https://since1986.github.io/</url> | ||
<email>ffzone@gmail.com</email> | ||
</developer> | ||
</developers> | ||
|
||
<scm> | ||
<connection>scm:git:https://github.com/since1986/mybatis-comment-generator.git</connection> | ||
<developerConnection>scm:git:https://github.com/since1986/mybatis-comment-generator.git</developerConnection> | ||
<url>https://github.com/since1986</url> | ||
<tag>v${project.version}</tag> | ||
</scm> | ||
|
||
<distributionManagement> | ||
<snapshotRepository> | ||
<id>snapshots</id> | ||
<url>https://oss.sonatype.org/content/repositories/snapshots</url> | ||
</snapshotRepository> | ||
<repository> | ||
<id>central</id> | ||
<name>Maven Central Staging Repository</name> | ||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> | ||
</repository> | ||
</distributionManagement> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.resources.overwrite>true</maven.resources.overwrite> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
|
||
<version.mybatis-generator-core>1.3.7</version.mybatis-generator-core> | ||
<version.log4j-core>2.2</version.log4j-core> | ||
<version.log4j-slf4j-impl>2.2</version.log4j-slf4j-impl> | ||
|
||
<version.maven-javadoc-plugin>3.0.1</version.maven-javadoc-plugin> | ||
<version.maven-source-plugin>3.0.1</version.maven-source-plugin> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.mybatis.generator</groupId> | ||
<artifactId>mybatis-generator-core</artifactId> | ||
<version>${version.mybatis-generator-core}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>${version.log4j-core}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<version>${version.log4j-slf4j-impl}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-javadoc-plugin</artifactId> | ||
<version>${version.maven-javadoc-plugin}</version> | ||
<configuration> | ||
<encoding>${project.build.sourceEncoding}</encoding> | ||
<charset>${project.build.sourceEncoding}</charset> | ||
<docencoding>${project.build.sourceEncoding}</docencoding> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
<version>${version.maven-source-plugin}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
103 changes: 103 additions & 0 deletions
103
src/main/java/io/github/since1986/mybatis/comment/generator/AbstractCommentGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package io.github.since1986.mybatis.comment.generator; | ||
|
||
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 java.util.Properties; | ||
import java.util.Set; | ||
|
||
public abstract class AbstractCommentGenerator implements CommentGenerator { | ||
|
||
@Override | ||
public void addConfigurationProperties(Properties properties) { | ||
|
||
} | ||
|
||
@Override | ||
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { | ||
|
||
} | ||
|
||
@Override | ||
public void addFieldComment(Field field, IntrospectedTable introspectedTable) { | ||
|
||
} | ||
|
||
@Override | ||
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { | ||
|
||
} | ||
|
||
@Override | ||
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { | ||
|
||
} | ||
|
||
@Override | ||
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { | ||
|
||
} | ||
|
||
@Override | ||
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { | ||
|
||
} | ||
|
||
@Override | ||
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { | ||
|
||
} | ||
|
||
@Override | ||
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { | ||
|
||
} | ||
|
||
@Override | ||
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { | ||
|
||
} | ||
|
||
@Override | ||
public void addJavaFileComment(CompilationUnit compilationUnit) { | ||
|
||
} | ||
|
||
@Override | ||
public void addComment(XmlElement xmlElement) { | ||
|
||
} | ||
|
||
@Override | ||
public void addRootComment(XmlElement rootElement) { | ||
|
||
} | ||
|
||
@Override | ||
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { | ||
|
||
} | ||
|
||
@Override | ||
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) { | ||
|
||
} | ||
|
||
@Override | ||
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { | ||
|
||
} | ||
|
||
@Override | ||
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) { | ||
|
||
} | ||
|
||
@Override | ||
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { | ||
|
||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
.../io/github/since1986/mybatis/comment/generator/CommentWithAnnotationCommentGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package io.github.since1986.mybatis.comment.generator; | ||
|
||
import org.mybatis.generator.api.IntrospectedColumn; | ||
import org.mybatis.generator.api.IntrospectedTable; | ||
import org.mybatis.generator.api.dom.java.Field; | ||
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; | ||
import org.mybatis.generator.api.dom.java.TopLevelClass; | ||
import org.mybatis.generator.config.PropertyRegistry; | ||
import org.mybatis.generator.internal.util.StringUtility; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.StringTokenizer; | ||
|
||
public class CommentWithAnnotationCommentGenerator extends AbstractCommentGenerator { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CommentWithAnnotationCommentGenerator.class); | ||
|
||
private static final String SUPPRESS_ALL_ANNOTATIONS = "suppressAllAnnotations"; | ||
private static final String FIELD_ANNOTATION_FULLY_QUALIFIED_NAMES = "fieldAnnotationFullyQualifiedNames"; | ||
private static final String CLASS_ANNOTATION_FULLY_QUALIFIED_NAMES = "classAnnotationFullyQualifiedNames"; | ||
|
||
private static final String FIELD_COMMENT_TEMPLATE = "/** %s */"; | ||
private static final String CLASS_COMMENT_TEMPLATE = "/** %s */"; | ||
|
||
private Properties properties = new Properties(); | ||
|
||
// 保留官方的重要设置属性,这样更符合直觉 | ||
private boolean suppressAllComments; | ||
|
||
private boolean suppressAllAnnotations; | ||
private List<String> fieldAnnotationFullyQualifiedNames = new LinkedList<>(); | ||
private List<String> classAnnotationFullyQualifiedNames = new LinkedList<>(); | ||
|
||
@Override | ||
public void addConfigurationProperties(Properties properties) { | ||
this.properties.putAll(properties); | ||
suppressAllComments = StringUtility.isTrue(this.properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS)); | ||
suppressAllAnnotations = StringUtility.isTrue(this.properties.getProperty(SUPPRESS_ALL_ANNOTATIONS)); | ||
fieldAnnotationFullyQualifiedNames.addAll(Util.toList(this.properties.getProperty(FIELD_ANNOTATION_FULLY_QUALIFIED_NAMES))); | ||
classAnnotationFullyQualifiedNames.addAll(Util.toList(this.properties.getProperty(CLASS_ANNOTATION_FULLY_QUALIFIED_NAMES))); | ||
} | ||
|
||
@Override | ||
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { | ||
if (!suppressAllComments && Util.isNotBlank(introspectedColumn.getRemarks())) { | ||
LOGGER.debug(String.format("field = %s, column = %s, columnRemarks = %s", field.getName(), introspectedColumn.getActualColumnName(), introspectedColumn.getRemarks())); | ||
field.addJavaDocLine(String.format(FIELD_COMMENT_TEMPLATE, introspectedColumn.getRemarks())); | ||
} | ||
if (!suppressAllAnnotations) { | ||
LOGGER.debug(String.format("fieldAnnotationFullyQualifiedNames = %s]", fieldAnnotationFullyQualifiedNames)); | ||
fieldAnnotationFullyQualifiedNames.forEach(fieldAnnotationFullyQualifiedName -> { | ||
field.addJavaDocLine(Util.atAnnotationName(fieldAnnotationFullyQualifiedName)); | ||
}); | ||
} else { | ||
LOGGER.debug(String.format("suppressAllComments = %b, suppressAllAnnotations = %b", suppressAllComments, suppressAllAnnotations)); | ||
} | ||
} | ||
|
||
@Override | ||
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { | ||
if (!suppressAllComments && Util.isNotBlank(introspectedTable.getRemarks())) { | ||
LOGGER.debug(String.format("class = %s, table = %s, tableRemarks = %s", topLevelClass.getType(), introspectedTable.getFullyQualifiedTable(), introspectedTable.getRemarks())); | ||
topLevelClass.addJavaDocLine(String.format(CLASS_COMMENT_TEMPLATE, introspectedTable.getRemarks())); | ||
} | ||
if (!suppressAllAnnotations) { | ||
LOGGER.debug(String.format("classAnnotationFullyQualifiedNames = %s]", classAnnotationFullyQualifiedNames)); | ||
classAnnotationFullyQualifiedNames.forEach(classAnnotationFullyQualifiedName -> { | ||
topLevelClass.addJavaDocLine(Util.atAnnotationName(classAnnotationFullyQualifiedName)); | ||
topLevelClass.addImportedType(new FullyQualifiedJavaType(classAnnotationFullyQualifiedName)); | ||
}); | ||
fieldAnnotationFullyQualifiedNames.forEach(fieldAnnotationFullyQualifiedName -> { | ||
topLevelClass.addImportedType(new FullyQualifiedJavaType(fieldAnnotationFullyQualifiedName)); | ||
}); | ||
} else { | ||
LOGGER.debug(String.format("suppressAllComments = %b, suppressAllAnnotations = %b", suppressAllComments, suppressAllAnnotations)); | ||
} | ||
} | ||
|
||
static class Util { | ||
|
||
static boolean isBlank(String string) { | ||
return string == null || string.trim().length() == 0; | ||
} | ||
|
||
static boolean isNotBlank(String string) { | ||
return !isBlank(string); | ||
} | ||
|
||
static List<String> toList(String commaSeparatedString) { | ||
List<String> list = new LinkedList<>(); | ||
assert isNotBlank(commaSeparatedString); | ||
StringTokenizer stringTokenizer = new StringTokenizer(commaSeparatedString, ","); | ||
String token; | ||
while (stringTokenizer.hasMoreTokens()) { | ||
token = stringTokenizer.nextToken(); | ||
list.add(token); | ||
} | ||
if (list.size() == 0) { | ||
list.add(commaSeparatedString); | ||
} | ||
return list; | ||
} | ||
|
||
static String atAnnotationName(String annotationFullyQualifiedName) { | ||
int lastIndexOfDot = annotationFullyQualifiedName.lastIndexOf("."); | ||
assert lastIndexOfDot != -1; | ||
String atAnnotationName = annotationFullyQualifiedName.substring(lastIndexOfDot + 1); | ||
assert isNotBlank(atAnnotationName); | ||
return String.format("@%s", atAnnotationName); | ||
} | ||
} | ||
} |
Oops, something went wrong.