Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mybatis-generator-plugin
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
mybatis-generator-plugin
Commits
0a4f6162
Commit
0a4f6162
authored
Aug 07, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ModelBuilderPlugin 增加静态builder方法
parent
e75e36af
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
0 deletions
+101
-0
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
...m/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
+25
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/InnerTypeFullyQualifiedJavaType.java
...ugins/utils/enhanced/InnerTypeFullyQualifiedJavaType.java
+76
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
View file @
0a4f6162
...
...
@@ -20,12 +20,15 @@ import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import
com.itfsw.mybatis.generator.plugins.utils.FormatTools
;
import
com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.internal.util.JavaBeansUtil
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* ---------------------------------------------------------------------------
...
...
@@ -37,6 +40,7 @@ import java.util.List;
*/
public
class
ModelBuilderPlugin
extends
BasePlugin
{
public
static
final
String
BUILDER_CLASS_NAME
=
"Builder"
;
// Builder 类名
private
Map
<
IntrospectedTable
,
InnerTypeFullyQualifiedJavaType
>
innerClasses
=
new
HashMap
<>();
/**
* Model Methods 生成
...
...
@@ -94,6 +98,27 @@ public class ModelBuilderPlugin extends BasePlugin {
InnerClass
innerClass
=
new
InnerClass
(
BUILDER_CLASS_NAME
);
innerClass
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
innerClass
.
setStatic
(
true
);
// 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
// 顺序为 key base withBLOBs
InnerTypeFullyQualifiedJavaType
builderType
=
new
InnerTypeFullyQualifiedJavaType
(
topLevelClass
.
getType
().
getFullyQualifiedName
()
+
"."
+
BUILDER_CLASS_NAME
);
if
(
innerClasses
.
get
(
introspectedTable
)
!=
null
)
{
innerClass
.
setSuperClass
(
innerClasses
.
get
(
introspectedTable
));
innerClasses
.
remove
(
introspectedTable
);
}
innerClasses
.
put
(
introspectedTable
,
builderType
);
// 增加静态builder方法实现和lombok一样
Method
builder
=
JavaElementGeneratorTools
.
generateMethod
(
"builder"
,
JavaVisibility
.
PUBLIC
,
builderType
);
commentGenerator
.
addGeneralMethodComment
(
builder
,
introspectedTable
);
builder
.
setStatic
(
true
);
builder
.
addBodyLine
(
"return new "
+
builderType
.
getShortName
()
+
"();"
);
topLevelClass
.
addMethod
(
builder
);
commentGenerator
.
addClassComment
(
innerClass
,
introspectedTable
);
logger
.
debug
(
"itfsw(数据Model链式构建插件):"
+
topLevelClass
.
getType
().
getShortName
()
+
"增加内部Builder类。"
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/InnerTypeFullyQualifiedJavaType.java
0 → 100644
View file @
0a4f6162
/*
* Copyright (c) 2017.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
enhanced
;
import
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/8/2 16:55
* ---------------------------------------------------------------------------
*/
public
class
InnerTypeFullyQualifiedJavaType
extends
FullyQualifiedJavaType
{
private
final
static
Logger
logger
=
LoggerFactory
.
getLogger
(
InnerTypeFullyQualifiedJavaType
.
class
);
private
String
outerType
;
// 内部类
/**
* Use this constructor to construct a generic type with the specified type parameters.
* @param fullTypeSpecification the full type specification
*/
public
InnerTypeFullyQualifiedJavaType
(
String
fullTypeSpecification
){
super
(
fullTypeSpecification
);
try
{
// 修正package
java
.
lang
.
reflect
.
Field
packageName
=
this
.
getClass
().
getSuperclass
().
getDeclaredField
(
"packageName"
);
packageName
.
setAccessible
(
true
);
String
oldPackageName
=
getPackageName
();
packageName
.
set
(
this
,
oldPackageName
.
substring
(
0
,
oldPackageName
.
lastIndexOf
(
"."
)));
outerType
=
oldPackageName
.
substring
(
oldPackageName
.
lastIndexOf
(
"."
)
+
1
);
}
catch
(
Exception
e
){
logger
.
error
(
"InnerTypeFullyQualifiedJavaType 赋值失败!"
,
e
);
}
}
/**
* This method returns the fully qualified name - including any generic type parameters.
*
* @return Returns the fullyQualifiedName.
*/
@Override
public
String
getFullyQualifiedName
()
{
String
fullyQualifiedName
=
super
.
getFullyQualifiedName
();
String
before
=
fullyQualifiedName
.
substring
(
0
,
fullyQualifiedName
.
lastIndexOf
(
"."
));
String
end
=
fullyQualifiedName
.
substring
(
fullyQualifiedName
.
lastIndexOf
(
"."
));
return
before
+
"."
+
outerType
+
end
;
}
/**
* Gets the short name.
*
* @return Returns the shortName - including any type arguments.
*/
@Override
public
String
getShortName
()
{
return
outerType
+
"."
+
super
.
getShortName
();
}
}
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