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
9425e882
Commit
9425e882
authored
Apr 28, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构代码插件依赖关系
parent
09065650
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
167 additions
and
141 deletions
+167
-141
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
...com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
+104
-44
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
...m/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
+4
-85
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
.../mybatis/generator/plugins/utils/hook/HookAggregator.java
+15
-12
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IModelBuilderPluginHook.java
...generator/plugins/utils/hook/IModelBuilderPluginHook.java
+44
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
View file @
9425e882
This diff is collapsed.
Click to expand it.
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
View file @
9425e882
...
...
@@ -17,10 +17,10 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
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.PluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.*
;
...
...
@@ -163,91 +163,10 @@ public class ModelBuilderPlugin extends BasePlugin {
build
.
addBodyLine
(
"return this.obj;"
);
commentGenerator
.
addGeneralMethodComment
(
build
,
introspectedTable
);
innerClass
.
addMethod
(
build
);
logger
.
debug
(
"itfsw(数据Model链式构建插件):"
+
topLevelClass
.
getType
().
getShortName
()
+
".Builder增加build方法。"
);
// ========================================== IncrementsPlugin =======================================
IncrementsPluginTools
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
if
(
incTools
.
support
())
{
// 增加枚举
InnerEnum
eIncrements
=
new
InnerEnum
(
new
FullyQualifiedJavaType
(
"Inc"
));
eIncrements
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
eIncrements
.
setStatic
(
true
);
eIncrements
.
addEnumConstant
(
"INC(\"+\")"
);
eIncrements
.
addEnumConstant
(
"DEC(\"-\")"
);
commentGenerator
.
addEnumComment
(
eIncrements
,
introspectedTable
);
// 生成属性和构造函数
Field
fValue
=
new
Field
(
"value"
,
FullyQualifiedJavaType
.
getStringInstance
());
fValue
.
setVisibility
(
JavaVisibility
.
PRIVATE
);
fValue
.
setFinal
(
true
);
commentGenerator
.
addFieldComment
(
fValue
,
introspectedTable
);
eIncrements
.
addField
(
fValue
);
Method
mInc
=
new
Method
(
"Inc"
);
mInc
.
setConstructor
(
true
);
mInc
.
addBodyLine
(
"this.value = value;"
);
mInc
.
addParameter
(
new
Parameter
(
FullyQualifiedJavaType
.
getStringInstance
(),
"value"
));
commentGenerator
.
addGeneralMethodComment
(
mInc
,
introspectedTable
);
eIncrements
.
addMethod
(
mInc
);
Method
mValue
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fValue
);
commentGenerator
.
addGeneralMethodComment
(
mValue
,
introspectedTable
);
eIncrements
.
addMethod
(
mValue
);
innerClass
.
addInnerEnum
(
eIncrements
);
// 增加field
Field
fIncrements
=
JavaElementGeneratorTools
.
generateField
(
IncrementsPlugin
.
FIELD_INC_MAP
,
JavaVisibility
.
PROTECTED
,
new
FullyQualifiedJavaType
(
"Map<String, "
+
incTools
.
getIncEnum
().
getFullyQualifiedName
()
+
">"
),
"new HashMap<String, "
+
incTools
.
getIncEnum
().
getFullyQualifiedName
()
+
">()"
);
commentGenerator
.
addFieldComment
(
fIncrements
,
introspectedTable
);
topLevelClass
.
addField
(
fIncrements
);
topLevelClass
.
addImportedType
(
"java.util.Map"
);
topLevelClass
.
addImportedType
(
"java.util.HashMap"
);
// getter&setter
Method
mGetter
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fIncrements
);
commentGenerator
.
addGetterComment
(
mGetter
,
introspectedTable
,
null
);
topLevelClass
.
addMethod
(
mGetter
);
Method
mSetter
=
JavaElementGeneratorTools
.
generateSetterMethod
(
fIncrements
);
commentGenerator
.
addSetterComment
(
mSetter
,
introspectedTable
,
null
);
topLevelClass
.
addMethod
(
mSetter
);
// 增加判断方法
Method
mHasIncsForColumn
=
JavaElementGeneratorTools
.
generateMethod
(
IncrementsPlugin
.
METHOD_INC_CHECK
,
JavaVisibility
.
PUBLIC
,
FullyQualifiedJavaType
.
getBooleanPrimitiveInstance
(),
new
Parameter
(
FullyQualifiedJavaType
.
getStringInstance
(),
"column"
)
);
commentGenerator
.
addGeneralMethodComment
(
mHasIncsForColumn
,
introspectedTable
);
mHasIncsForColumn
.
addBodyLine
(
"return "
+
IncrementsPlugin
.
FIELD_INC_MAP
+
".get(column) != null;"
);
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
mHasIncsForColumn
);
// Builder 中 添加字段支持
for
(
IntrospectedColumn
column
:
columns
)
{
if
(
incTools
.
supportColumn
(
column
))
{
Field
field
=
JavaBeansUtil
.
getJavaBeansField
(
column
,
context
,
introspectedTable
);
// 增加方法
Method
mIncrements
=
JavaElementGeneratorTools
.
generateMethod
(
field
.
getName
(),
JavaVisibility
.
PUBLIC
,
innerClass
.
getType
(),
new
Parameter
(
field
.
getType
(),
field
.
getName
()),
new
Parameter
(
incTools
.
getIncEnum
(),
"inc"
)
);
commentGenerator
.
addSetterComment
(
mIncrements
,
introspectedTable
,
column
);
Method
setterMethod
=
JavaBeansUtil
.
getJavaBeansSetter
(
column
,
context
,
introspectedTable
);
mIncrements
.
addBodyLine
(
"obj."
+
IncrementsPlugin
.
FIELD_INC_MAP
+
".put(\""
+
column
.
getActualColumnName
()
+
"\", inc);"
);
mIncrements
.
addBodyLine
(
"obj."
+
setterMethod
.
getName
()
+
"("
+
field
.
getName
()
+
");"
);
mIncrements
.
addBodyLine
(
"return this;"
);
FormatTools
.
addMethodWithBestPosition
(
innerClass
,
mIncrements
);
}
}
}
// hook
PluginTools
.
getHook
(
IModelBuilderPluginHook
.
class
).
modelBuilderClassGenerated
(
topLevelClass
,
innerClass
,
columns
,
introspectedTable
);
return
innerClass
;
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
View file @
9425e882
...
...
@@ -20,8 +20,10 @@ import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.Plugin
;
import
org.mybatis.generator.api.dom.java.InnerClass
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.config.Context
;
import
org.slf4j.Logger
;
...
...
@@ -38,7 +40,7 @@ import java.util.List;
* @time:2018/4/27 11:33
* ---------------------------------------------------------------------------
*/
public
class
HookAggregator
implements
IUpsertPluginHook
{
public
class
HookAggregator
implements
IUpsertPluginHook
,
IModelBuilderPluginHook
{
protected
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
BasePlugin
.
class
);
// 日志
private
final
static
HookAggregator
instance
=
new
HookAggregator
();
private
Context
context
;
...
...
@@ -67,16 +69,6 @@ public class HookAggregator implements IUpsertPluginHook {
this
.
context
=
context
;
}
/**
* 获取挂载
* @param clazz
* @param <T>
* @return
*/
public
<
T
>
T
getHook
(
Class
<
T
>
clazz
)
{
return
(
T
)
this
;
}
/**
* 获取插件
* @param clazz
...
...
@@ -101,7 +93,18 @@ public class HookAggregator implements IUpsertPluginHook {
return
list
;
}
// ================================================= default ===============================================
// ============================================ IModelBuilderPluginHook =============================================
@Override
public
boolean
modelBuilderClassGenerated
(
TopLevelClass
topLevelClass
,
InnerClass
builderClass
,
List
<
IntrospectedColumn
>
columns
,
IntrospectedTable
introspectedTable
)
{
for
(
IModelBuilderPluginHook
plugin
:
this
.
getPlugins
(
IModelBuilderPluginHook
.
class
))
{
if
(!
plugin
.
modelBuilderClassGenerated
(
topLevelClass
,
builderClass
,
columns
,
introspectedTable
))
{
return
false
;
}
}
return
true
;
}
// ================================================= IUpsertPluginHook ===============================================
@Override
public
boolean
clientUpsertSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
for
(
IUpsertPluginHook
plugin
:
this
.
getPlugins
(
IUpsertPluginHook
.
class
))
{
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IModelBuilderPluginHook.java
0 → 100644
View file @
9425e882
/*
* Copyright (c) 2018.
*
* 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
.
hook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.InnerClass
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/4/28 17:29
* ---------------------------------------------------------------------------
*/
public
interface
IModelBuilderPluginHook
{
/**
* Model builder class 生成
* @param topLevelClass
* @param builderClass
* @param columns
* @param introspectedTable
* @return
*/
boolean
modelBuilderClassGenerated
(
TopLevelClass
topLevelClass
,
InnerClass
builderClass
,
List
<
IntrospectedColumn
>
columns
,
IntrospectedTable
introspectedTable
);
}
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