Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mbg-comment
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
mbg-comment
Commits
11a56752
Commit
11a56752
authored
Sep 26, 2018
by
mizhoux
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a custom comment generator for MySQL
parent
71d63ec7
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
281 additions
and
0 deletions
+281
-0
generatorConfig.xml
generatorConfig.xml
+55
-0
pom.xml
pom.xml
+43
-0
src/main/java/me/mizhoux/Generator.java
src/main/java/me/mizhoux/Generator.java
+29
-0
src/main/java/me/mizhoux/mbgcomment/MySQLCommentGenerator.java
...ain/java/me/mizhoux/mbgcomment/MySQLCommentGenerator.java
+51
-0
src/main/java/me/mizhoux/mbgcomment/SimpleCommentGenerator.java
...in/java/me/mizhoux/mbgcomment/SimpleCommentGenerator.java
+103
-0
No files found.
generatorConfig.xml
0 → 100644
View file @
11a56752
<?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>
<!-- 指定数据库驱动的jdbc驱动jar包的位置 -->
<!-- 不再需要,因为 jar 包已经在 classpath 中
<classPathEntry location="./mysql-connector-java-5.1.40.jar" />
-->
<context
id=
"mysql"
defaultModelType=
"hierarchical"
targetRuntime=
"MyBatis3Simple"
>
<!-- 生成的 Java 文件的编码 -->
<property
name=
"javaFileEncoding"
value=
"UTF-8"
/>
<!-- 格式化 Java 代码 -->
<property
name=
"javaFormatter"
value=
"org.mybatis.generator.api.dom.DefaultJavaFormatter"
/>
<!-- 格式化 XML 代码 -->
<property
name=
"xmlFormatter"
value=
"org.mybatis.generator.api.dom.DefaultXmlFormatter"
/>
<!-- 自定义注释生成器 -->
<commentGenerator
type=
"me.mizhoux.mbgcomment.MySQLCommentGenerator"
>
<property
name=
"author"
value=
"Michael Chow"
/>
<property
name=
"dateFormat"
value=
"yyyy/MM/dd"
/>
</commentGenerator>
<!-- 配置数据库连接 -->
<jdbcConnection
driverClass=
"com.mysql.jdbc.Driver"
connectionURL=
"jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"
userId=
"root"
password=
"123456"
>
<!-- 设置 useInformationSchema 属性为 true -->
<property
name=
"useInformationSchema"
value=
"true"
/>
</jdbcConnection>
<!-- 生成实体的位置 -->
<javaModelGenerator
targetPackage=
"me.mizhoux.model"
targetProject=
"src/main/java"
>
<property
name=
"enableSubPackages"
value=
"true"
/>
</javaModelGenerator>
<!-- 生成 Mapper 接口的位置 -->
<sqlMapGenerator
targetPackage=
"me.mizhoux.mapper"
targetProject=
"src/main/java"
>
<property
name=
"enableSubPackages"
value=
"true"
/>
</sqlMapGenerator>
<!-- 生成 Mapper XML 的位置 -->
<javaClientGenerator
targetPackage=
"me.mizhoux.mapper"
type=
"XMLMAPPER"
targetProject=
"src/main/java"
>
<property
name=
"enableSubPackages"
value=
"true"
/>
</javaClientGenerator>
<!-- 设置数据库的表名和实体类名 -->
<table
tableName=
"t_user"
domainObjectName=
"User"
>
<!-- generatedKey用于生成生成主键的方法 -->
<generatedKey
column=
"id"
sqlStatement=
"SELECT LAST_INSERT_ID()"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
pom.xml
0 → 100644
View file @
11a56752
<?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>
me.mizhoux
</groupId>
<artifactId>
mbg-comment
</artifactId>
<version>
0.0.1
</version>
<name>
mbg-comment
</name>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.source>
1.7
</maven.compiler.source>
<maven.compiler.target>
1.7
</maven.compiler.target>
</properties>
<dependencies>
<!-- MySQL Connector -->
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>
5.1.40
</version>
</dependency>
<!-- MyBatis Generator -->
<dependency>
<groupId>
org.mybatis.generator
</groupId>
<artifactId>
mybatis-generator-core
</artifactId>
<version>
1.3.7
</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.11
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
src/main/java/me/mizhoux/Generator.java
0 → 100644
View file @
11a56752
package
me
.
mizhoux
;
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.File
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Runs MBG completely with Java.
*
* @see <a href="http://www.mybatis.org/generator/running/runningWithJava.html">Running MyBatis Generator With Java</a>
*/
public
class
Generator
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
List
<
String
>
warnings
=
new
ArrayList
<>();
File
configFile
=
new
File
(
"generatorConfig.xml"
);
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
configFile
);
DefaultShellCallback
callback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
callback
,
warnings
);
myBatisGenerator
.
generate
(
null
);
}
}
src/main/java/me/mizhoux/mbgcomment/MySQLCommentGenerator.java
0 → 100644
View file @
11a56752
package
me
.
mizhoux
.
mbgcomment
;
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.TopLevelClass
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Properties
;
public
class
MySQLCommentGenerator
extends
SimpleCommentGenerator
{
private
Properties
properties
;
public
MySQLCommentGenerator
()
{
properties
=
new
Properties
();
}
@Override
public
void
addConfigurationProperties
(
Properties
properties
)
{
// 获取自定义的 properties
this
.
properties
.
putAll
(
properties
);
}
@Override
public
void
addModelClassComment
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
String
author
=
properties
.
getProperty
(
"author"
);
String
dateFormat
=
properties
.
getProperty
(
"dateFormat"
,
"yyyy-MM-dd"
);
SimpleDateFormat
dateFormatter
=
new
SimpleDateFormat
(
dateFormat
);
// 获取表注释
String
remarks
=
introspectedTable
.
getRemarks
();
topLevelClass
.
addJavaDocLine
(
"/**"
);
topLevelClass
.
addJavaDocLine
(
" * "
+
remarks
);
topLevelClass
.
addJavaDocLine
(
" *"
);
topLevelClass
.
addJavaDocLine
(
" * @author "
+
author
);
topLevelClass
.
addJavaDocLine
(
" * @date "
+
dateFormatter
.
format
(
new
Date
()));
topLevelClass
.
addJavaDocLine
(
" */"
);
}
@Override
public
void
addFieldComment
(
Field
field
,
IntrospectedTable
introspectedTable
,
IntrospectedColumn
introspectedColumn
)
{
// 获取列注释
String
remarks
=
introspectedColumn
.
getRemarks
();
field
.
addJavaDocLine
(
"/**"
);
field
.
addJavaDocLine
(
" * "
+
remarks
);
field
.
addJavaDocLine
(
" */"
);
}
}
src/main/java/me/mizhoux/mbgcomment/SimpleCommentGenerator.java
0 → 100644
View file @
11a56752
package
me
.
mizhoux
.
mbgcomment
;
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
class
SimpleCommentGenerator
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
b
)
{
}
@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
xmlElement
)
{
}
@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
)
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment