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
d0233d96
Commit
d0233d96
authored
Nov 06, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
LombokPlugin 配合 IncrementsPlugin 测试用例
parent
5cbb85bd
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
293 additions
and
224 deletions
+293
-224
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
...com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
+233
-102
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
...ybatis/generator/plugins/utils/IncrementsPluginTools.java
+0
-116
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
...itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
+58
-4
src/test/resources/scripts/IncrementsPlugin/init-lombok.sql
src/test/resources/scripts/IncrementsPlugin/init-lombok.sql
+1
-1
src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-LombokPlugin.xml
.../IncrementsPlugin/mybatis-generator-with-LombokPlugin.xml
+1
-1
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
View file @
d0233d96
...
...
@@ -30,6 +30,7 @@ import org.mybatis.generator.api.dom.xml.TextElement;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.internal.util.JavaBeansUtil
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -46,7 +47,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
public
static
final
String
PRO_INCREMENTS_COLUMNS
=
"incrementsColumns"
;
// incrementsColumns property
public
static
final
String
FIELD_INC_MAP
=
"incrementsColumnsInfoMap"
;
// 为了防止和用户数据库字段冲突,特殊命名
public
static
final
String
METHOD_INC_CHECK
=
"hasIncsForColumn"
;
// inc 检查方法名称
private
IncrementsPluginTools
incTools
;
// 增量插件工具
private
List
<
IntrospectedColumn
>
incColumns
;
// 表启用增量操作的字段
private
InnerEnum
incEnum
;
// 增量Enum
private
InnerClass
incEnumBuilder
;
// 添加了增量Enum的builder
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
...
...
@@ -72,7 +76,23 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
super
.
initialized
(
introspectedTable
);
this
.
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
this
.
incColumns
=
new
ArrayList
<>();
this
.
incEnum
=
null
;
this
.
incEnumBuilder
=
null
;
String
incrementsColumns
=
introspectedTable
.
getTableConfigurationProperty
(
IncrementsPlugin
.
PRO_INCREMENTS_COLUMNS
);
if
(
StringUtility
.
stringHasValue
(
incrementsColumns
))
{
// 切分
String
[]
incrementsColumnsStrs
=
incrementsColumns
.
split
(
","
);
for
(
String
incrementsColumnsStr
:
incrementsColumnsStrs
)
{
IntrospectedColumn
column
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
incrementsColumnsStr
);
if
(
column
==
null
)
{
warnings
.
add
(
"itfsw:插件"
+
IncrementsPlugin
.
class
.
getTypeName
()
+
"插件没有找到column为"
+
incrementsColumnsStr
.
trim
()
+
"的字段!"
);
}
else
{
incColumns
.
add
(
column
);
}
}
}
}
/**
...
...
@@ -172,10 +192,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
* @return
*/
private
boolean
lombokBuilderClassGenerated
(
TopLevelClass
topLevelClass
,
List
<
IntrospectedColumn
>
columns
,
IntrospectedTable
introspectedTable
)
{
if
(
incTool
s
.
support
())
{
if
(
thi
s
.
support
())
{
boolean
find
=
false
;
for
(
IntrospectedColumn
column
:
columns
)
{
if
(
incTool
s
.
supportColumn
(
column
))
{
if
(
thi
s
.
supportColumn
(
column
))
{
find
=
true
;
break
;
}
...
...
@@ -212,40 +232,6 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
builderMethod
.
addBodyLine
(
"return new "
+
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"BuilderImpl();"
);
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
builderMethod
);
// ------------------------------ builderImpl Class ----------------------------------
InnerClass
builderImplCls
=
new
InnerClass
(
topLevelClass
.
getType
().
getShortName
()
+
"BuilderImpl"
);
commentGenerator
.
addClassComment
(
builderImplCls
,
introspectedTable
);
FullyQualifiedJavaType
builderType1
=
new
FullyQualifiedJavaType
(
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"Builder"
);
builderType1
.
addTypeArgument
(
new
SpecTypeArgumentsFullyQualifiedJavaType
(
"<"
+
topLevelClass
.
getType
().
getShortName
()
+
", "
+
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"BuilderImpl"
+
">"
));
builderImplCls
.
setSuperClass
(
builderType1
);
builderImplCls
.
setVisibility
(
JavaVisibility
.
PRIVATE
);
builderImplCls
.
setFinal
(
true
);
builderImplCls
.
setStatic
(
true
);
topLevelClass
.
addInnerClass
(
builderImplCls
);
// self 方法
Method
selfMethod
=
JavaElementGeneratorTools
.
generateMethod
(
"self"
,
JavaVisibility
.
PROTECTED
,
new
FullyQualifiedJavaType
(
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"BuilderImpl"
)
);
commentGenerator
.
addGeneralMethodComment
(
selfMethod
,
introspectedTable
);
selfMethod
.
addBodyLine
(
"return this;"
);
FormatTools
.
addMethodWithBestPosition
(
builderImplCls
,
selfMethod
);
// build 方法
Method
buildMethod
=
JavaElementGeneratorTools
.
generateMethod
(
"build"
,
JavaVisibility
.
PUBLIC
,
topLevelClass
.
getType
()
);
commentGenerator
.
addGeneralMethodComment
(
buildMethod
,
introspectedTable
);
buildMethod
.
addBodyLine
(
"return new "
+
topLevelClass
.
getType
().
getShortName
()
+
"(this);"
);
FormatTools
.
addMethodWithBestPosition
(
builderImplCls
,
buildMethod
);
// ------------------------------ builder Class ----------------------------------
InnerClass
builderCls
=
new
InnerClass
(
topLevelClass
.
getType
().
getShortName
()
+
"Builder"
);
commentGenerator
.
addClassComment
(
builderCls
,
introspectedTable
);
...
...
@@ -259,7 +245,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"Builder<C, B>>"
)
);
if
(
topLevelClass
.
getSuperClass
()
!=
null
){
if
(
topLevelClass
.
getSuperClass
()
!=
null
)
{
FullyQualifiedJavaType
superBuilderCls
=
new
FullyQualifiedJavaType
(
topLevelClass
.
getSuperClass
().
getShortName
()
+
"Builder"
);
superBuilderCls
.
addTypeArgument
(
new
SpecTypeArgumentsFullyQualifiedJavaType
(
"<C, B>"
));
builderCls
.
setSuperClass
(
superBuilderCls
);
...
...
@@ -270,7 +256,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
builderCls
.
addAnnotation
(
LombokPlugin
.
EnumLombokAnnotations
.
SETTER
.
getAnnotation
());
topLevelClass
.
addImportedType
(
LombokPlugin
.
EnumLombokAnnotations
.
ACCESSORS_FLUENT_TRUE
.
getClazz
());
builderCls
.
addAnnotation
(
LombokPlugin
.
EnumLombokAnnotations
.
ACCESSORS_FLUENT_TRUE
.
getAnnotation
());
if
(
topLevelClass
.
getSuperClass
()
!=
null
){
if
(
topLevelClass
.
getSuperClass
()
!=
null
)
{
topLevelClass
.
addImportedType
(
LombokPlugin
.
EnumLombokAnnotations
.
TO_STRING_CALL_SUPER
.
getClazz
());
builderCls
.
addAnnotation
(
LombokPlugin
.
EnumLombokAnnotations
.
TO_STRING_CALL_SUPER
.
getAnnotation
());
}
else
{
...
...
@@ -308,6 +294,86 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
topLevelClass
.
addInnerClass
(
builderCls
);
// --------------------------------- 生成自增插件需要的方法 --------------------------------------
// 增加枚举
if
(
this
.
incEnum
==
null
)
{
this
.
incEnum
=
this
.
generatedIncEnum
(
introspectedTable
);
this
.
incEnumBuilder
=
builderCls
;
builderCls
.
addInnerEnum
(
this
.
incEnum
);
// topLevel class 添加必要的操作方法
this
.
addIncMethodToTopLevelClass
(
topLevelClass
,
builderCls
,
introspectedTable
,
true
);
// Builder 中 添加字段支持
Field
fIncrements
=
JavaElementGeneratorTools
.
generateField
(
IncrementsPlugin
.
FIELD_INC_MAP
,
JavaVisibility
.
PROTECTED
,
new
FullyQualifiedJavaType
(
"Map<String, "
+
this
.
getIncEnum
(
builderCls
,
introspectedTable
).
getFullyQualifiedName
()
+
">"
),
"new HashMap<String, "
+
this
.
getIncEnum
(
builderCls
,
introspectedTable
).
getFullyQualifiedName
()
+
">()"
);
commentGenerator
.
addFieldComment
(
fIncrements
,
introspectedTable
);
builderCls
.
addField
(
fIncrements
);
// Builder 构造函数增加 自增Map
constructor
.
addBodyLine
(
"this."
+
IncrementsPlugin
.
FIELD_INC_MAP
+
" = builder."
+
IncrementsPlugin
.
FIELD_INC_MAP
+
";"
);
}
FullyQualifiedJavaType
builderType2
=
new
FullyQualifiedJavaType
(
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"Builder"
);
builderType2
.
addTypeArgument
(
new
SpecTypeArgumentsFullyQualifiedJavaType
(
"<C, B>"
));
for
(
IntrospectedColumn
column
:
columns
)
{
if
(
this
.
supportColumn
(
column
))
{
Field
field
=
JavaBeansUtil
.
getJavaBeansField
(
column
,
context
,
introspectedTable
);
// 增加方法
Method
mIncrements
=
JavaElementGeneratorTools
.
generateMethod
(
field
.
getName
(),
JavaVisibility
.
PUBLIC
,
builderType2
,
new
Parameter
(
field
.
getType
(),
field
.
getName
()),
new
Parameter
(
this
.
getIncEnum
(
builderCls
,
introspectedTable
),
"inc"
)
);
commentGenerator
.
addSetterComment
(
mIncrements
,
introspectedTable
,
column
);
mIncrements
.
addBodyLine
(
"this."
+
IncrementsPlugin
.
FIELD_INC_MAP
+
".put(\""
+
column
.
getActualColumnName
()
+
"\", inc);"
);
mIncrements
.
addBodyLine
(
"return this."
+
field
.
getName
()
+
"("
+
field
.
getName
()
+
");"
);
FormatTools
.
addMethodWithBestPosition
(
builderCls
,
mIncrements
);
}
}
// ------------------------------ builderImpl Class ----------------------------------
InnerClass
builderImplCls
=
new
InnerClass
(
topLevelClass
.
getType
().
getShortName
()
+
"BuilderImpl"
);
commentGenerator
.
addClassComment
(
builderImplCls
,
introspectedTable
);
FullyQualifiedJavaType
builderType1
=
new
FullyQualifiedJavaType
(
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"Builder"
);
builderType1
.
addTypeArgument
(
new
SpecTypeArgumentsFullyQualifiedJavaType
(
"<"
+
topLevelClass
.
getType
().
getShortName
()
+
", "
+
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"BuilderImpl"
+
">"
));
builderImplCls
.
setSuperClass
(
builderType1
);
builderImplCls
.
setVisibility
(
JavaVisibility
.
PRIVATE
);
builderImplCls
.
setFinal
(
true
);
builderImplCls
.
setStatic
(
true
);
topLevelClass
.
addInnerClass
(
builderImplCls
);
// self 方法
Method
selfMethod
=
JavaElementGeneratorTools
.
generateMethod
(
"self"
,
JavaVisibility
.
PROTECTED
,
new
FullyQualifiedJavaType
(
topLevelClass
.
getType
().
getShortName
()
+
"."
+
topLevelClass
.
getType
().
getShortName
()
+
"BuilderImpl"
)
);
commentGenerator
.
addGeneralMethodComment
(
selfMethod
,
introspectedTable
);
selfMethod
.
addBodyLine
(
"return this;"
);
FormatTools
.
addMethodWithBestPosition
(
builderImplCls
,
selfMethod
);
// build 方法
Method
buildMethod
=
JavaElementGeneratorTools
.
generateMethod
(
"build"
,
JavaVisibility
.
PUBLIC
,
topLevelClass
.
getType
()
);
commentGenerator
.
addGeneralMethodComment
(
buildMethod
,
introspectedTable
);
buildMethod
.
addBodyLine
(
"return new "
+
topLevelClass
.
getType
().
getShortName
()
+
"(this);"
);
FormatTools
.
addMethodWithBestPosition
(
builderImplCls
,
buildMethod
);
return
false
;
}
}
...
...
@@ -326,65 +392,20 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
*/
@Override
public
boolean
modelBuilderClassGenerated
(
TopLevelClass
topLevelClass
,
InnerClass
builderClass
,
List
<
IntrospectedColumn
>
columns
,
IntrospectedTable
introspectedTable
)
{
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
);
FormatTools
.
addMethodWithBestPosition
(
eIncrements
,
mInc
);
Method
mValue
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fValue
);
commentGenerator
.
addGeneralMethodComment
(
mValue
,
introspectedTable
);
FormatTools
.
addMethodWithBestPosition
(
eIncrements
,
mValue
);
builderClass
.
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
);
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
mGetter
);
Method
mSetter
=
JavaElementGeneratorTools
.
generateSetterMethod
(
fIncrements
);
commentGenerator
.
addSetterComment
(
mSetter
,
introspectedTable
,
null
);
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
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
);
if
(
this
.
support
())
{
if
(
this
.
incEnum
==
null
)
{
this
.
incEnum
=
this
.
generatedIncEnum
(
introspectedTable
);
this
.
incEnumBuilder
=
builderClass
;
// 增加枚举
builderClass
.
addInnerEnum
(
this
.
incEnum
);
// topLevel class 添加必要的操作方法
this
.
addIncMethodToTopLevelClass
(
topLevelClass
,
builderClass
,
introspectedTable
,
false
);
}
// Builder 中 添加字段支持
for
(
IntrospectedColumn
column
:
columns
)
{
if
(
incTool
s
.
supportColumn
(
column
))
{
if
(
thi
s
.
supportColumn
(
column
))
{
Field
field
=
JavaBeansUtil
.
getJavaBeansField
(
column
,
context
,
introspectedTable
);
// 增加方法
Method
mIncrements
=
JavaElementGeneratorTools
.
generateMethod
(
...
...
@@ -392,7 +413,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
JavaVisibility
.
PUBLIC
,
builderClass
.
getType
(),
new
Parameter
(
field
.
getType
(),
field
.
getName
()),
new
Parameter
(
incTools
.
getIncEnum
(
),
"inc"
)
new
Parameter
(
this
.
getIncEnum
(
builderClass
,
introspectedTable
),
"inc"
)
);
commentGenerator
.
addSetterComment
(
mIncrements
,
introspectedTable
,
column
);
...
...
@@ -435,7 +456,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
public
List
<
Element
>
incrementSetElementGenerated
(
IntrospectedColumn
introspectedColumn
,
String
prefix
,
boolean
hasComma
)
{
List
<
Element
>
list
=
new
ArrayList
<>();
if
(
incTool
s
.
supportColumn
(
introspectedColumn
))
{
if
(
thi
s
.
supportColumn
(
introspectedColumn
))
{
// 1. column = 节点
list
.
add
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
));
...
...
@@ -482,10 +503,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
*/
@Override
public
Element
incrementSetsWithSelectiveEnhancedPluginElementGenerated
(
IntrospectedColumn
versionColumn
)
{
if
(
incTool
s
.
support
())
{
if
(
thi
s
.
support
())
{
XmlElement
choose
=
new
XmlElement
(
"choose"
);
for
(
IntrospectedColumn
introspectedColumn
:
incTools
.
getColumns
()
)
{
for
(
IntrospectedColumn
introspectedColumn
:
this
.
incColumns
)
{
if
(
versionColumn
==
null
||
!
introspectedColumn
.
getActualColumnName
().
equals
(
versionColumn
.
getActualColumnName
()))
{
XmlElement
when
=
new
XmlElement
(
"when"
);
...
...
@@ -525,12 +546,86 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
// =================================================== 原生方法的支持 ====================================================
/**
* 向topLevelClass 添加必要的操作函数
* @param topLevelClass
* @param builderCls
* @param introspectedTable
* @param withLombok
*/
private
void
addIncMethodToTopLevelClass
(
TopLevelClass
topLevelClass
,
InnerClass
builderCls
,
IntrospectedTable
introspectedTable
,
boolean
withLombok
)
{
// 增加field
Field
fIncrements
=
JavaElementGeneratorTools
.
generateField
(
IncrementsPlugin
.
FIELD_INC_MAP
,
JavaVisibility
.
PROTECTED
,
new
FullyQualifiedJavaType
(
"Map<String, "
+
this
.
getIncEnum
(
builderCls
,
introspectedTable
).
getFullyQualifiedName
()
+
">"
),
"new HashMap<String, "
+
this
.
getIncEnum
(
builderCls
,
introspectedTable
).
getFullyQualifiedName
()
+
">()"
);
commentGenerator
.
addFieldComment
(
fIncrements
,
introspectedTable
);
topLevelClass
.
addField
(
fIncrements
);
topLevelClass
.
addImportedType
(
"java.util.Map"
);
topLevelClass
.
addImportedType
(
"java.util.HashMap"
);
// getter&setter
if
(!
withLombok
)
{
Method
mGetter
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fIncrements
);
commentGenerator
.
addGetterComment
(
mGetter
,
introspectedTable
,
null
);
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
mGetter
);
Method
mSetter
=
JavaElementGeneratorTools
.
generateSetterMethod
(
fIncrements
);
commentGenerator
.
addSetterComment
(
mSetter
,
introspectedTable
,
null
);
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
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
);
}
/**
* 生成Inc enum
* @param introspectedTable
* @return
*/
private
InnerEnum
generatedIncEnum
(
IntrospectedTable
introspectedTable
)
{
// 增加枚举
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
);
FormatTools
.
addMethodWithBestPosition
(
eIncrements
,
mInc
);
Method
mValue
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fValue
);
commentGenerator
.
addGeneralMethodComment
(
mValue
,
introspectedTable
);
FormatTools
.
addMethodWithBestPosition
(
eIncrements
,
mValue
);
return
eIncrements
;
}
/**
* 有Selective代码生成
* @param element
*/
private
void
generatedWithSelective
(
XmlElement
element
,
IntrospectedTable
introspectedTable
,
boolean
hasPrefix
)
{
if
(
incTool
s
.
support
())
{
if
(
thi
s
.
support
())
{
// 查找 set->if->text
List
<
XmlElement
>
sets
=
XmlElementTools
.
findXmlElements
(
element
,
"set"
);
if
(
sets
.
size
()
>
0
)
{
...
...
@@ -562,7 +657,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
* @param hasPrefix
*/
private
void
generatedWithoutSelective
(
XmlElement
xmlElement
,
IntrospectedTable
introspectedTable
,
boolean
hasPrefix
)
{
if
(
incTool
s
.
support
())
{
if
(
thi
s
.
support
())
{
List
<
Element
>
newEles
=
new
ArrayList
<>();
for
(
Element
ele
:
xmlElement
.
getElements
())
{
// 找到text节点且格式为 set xx = xx 或者 xx = xx
...
...
@@ -590,4 +685,40 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
xmlElement
.
getElements
().
addAll
(
newEles
);
}
}
/**
* 获取INC Enum
* @param builderCls
* @return
*/
private
FullyQualifiedJavaType
getIncEnum
(
InnerClass
builderCls
,
IntrospectedTable
introspectedTable
)
{
try
{
return
new
FullyQualifiedJavaType
(
BeanUtils
.
getProperty
(
this
.
incEnumBuilder
.
getType
(),
"baseShortName"
).
toString
()
+
"."
+
this
.
incEnum
.
getType
().
getShortName
());
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取Inc enum 失败!"
,
e
);
}
return
null
;
}
/**
* 是否启用了
* @return
*/
private
boolean
support
()
{
return
this
.
incColumns
.
size
()
>
0
;
}
/**
* 判断是否为需要进行增量操作的column
* @param searchColumn
* @return
*/
private
boolean
supportColumn
(
IntrospectedColumn
searchColumn
)
{
for
(
IntrospectedColumn
column
:
this
.
incColumns
)
{
if
(
column
.
getActualColumnName
().
equals
(
searchColumn
.
getActualColumnName
()))
{
return
true
;
}
}
return
false
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
deleted
100644 → 0
View file @
5cbb85bd
/*
* 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
;
import
com.itfsw.mybatis.generator.plugins.IncrementsPlugin
;
import
com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
* 增量插件工具
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/21 16:12
* ---------------------------------------------------------------------------
*/
public
class
IncrementsPluginTools
{
private
IntrospectedTable
introspectedTable
;
// 表
private
List
<
IntrospectedColumn
>
columns
=
new
ArrayList
<>();
// 表启用增量操作的字段
/**
* 构造函数
* @param introspectedTable
*/
private
IncrementsPluginTools
(
IntrospectedTable
introspectedTable
)
{
this
.
introspectedTable
=
introspectedTable
;
}
/**
* 获取工具
* @param context
* @param introspectedTable
* @param warnings
* @return
*/
public
static
IncrementsPluginTools
getTools
(
Context
context
,
IntrospectedTable
introspectedTable
,
List
<
String
>
warnings
)
{
IncrementsPluginTools
tools
=
new
IncrementsPluginTools
(
introspectedTable
);
// 判断是否启用了插件
if
(
PluginTools
.
getPluginConfiguration
(
context
,
IncrementsPlugin
.
class
)
!=
null
)
{
String
incrementsColumns
=
introspectedTable
.
getTableConfigurationProperty
(
IncrementsPlugin
.
PRO_INCREMENTS_COLUMNS
);
if
(
StringUtility
.
stringHasValue
(
incrementsColumns
))
{
// 切分
String
[]
incrementsColumnsStrs
=
incrementsColumns
.
split
(
","
);
for
(
String
incrementsColumnsStr
:
incrementsColumnsStrs
)
{
IntrospectedColumn
column
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
incrementsColumnsStr
);
if
(
column
==
null
)
{
warnings
.
add
(
"itfsw:插件"
+
IncrementsPlugin
.
class
.
getTypeName
()
+
"插件没有找到column为"
+
incrementsColumnsStr
.
trim
()
+
"的字段!"
);
}
else
{
tools
.
columns
.
add
(
column
);
}
}
}
}
return
tools
;
}
/**
* 获取INC Enum
* @return
*/
public
FullyQualifiedJavaType
getIncEnum
()
{
return
new
FullyQualifiedJavaType
(
this
.
introspectedTable
.
getFullyQualifiedTable
().
getDomainObjectName
()
+
"."
+
ModelBuilderPlugin
.
BUILDER_CLASS_NAME
+
".Inc"
);
}
/**
* 是否启用了
* @return
*/
public
boolean
support
()
{
return
this
.
columns
.
size
()
>
0
;
}
/**
* Getter method for property <tt>columns</tt>.
* @return property value of columns
* @author hewei
*/
public
List
<
IntrospectedColumn
>
getColumns
()
{
return
columns
;
}
/**
* 判断是否为需要进行增量操作的column
* @param searchColumn
* @return
*/
public
boolean
supportColumn
(
IntrospectedColumn
searchColumn
)
{
for
(
IntrospectedColumn
column
:
this
.
columns
)
{
if
(
column
.
getActualColumnName
().
equals
(
searchColumn
.
getActualColumnName
()))
{
return
true
;
}
}
return
false
;
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
View file @
d0233d96
...
...
@@ -602,15 +602,69 @@ public class IncrementsPluginTest {
tbBuilder
=
new
ObjectUtil
(
tbBuilder
.
invoke
(
"id"
,
1L
));
tbBuilder
.
invoke
(
"field1"
,
"ts1"
);
ObjectUtil
tb
=
new
ObjectUtil
(
tbBuilder
.
invoke
(
"build"
));
Assert
.
assertEquals
(
tb
.
invoke
(
"toString"
),
"Tb(id=1, field1=ts1, field2=null)"
);
Assert
.
assertEquals
(
tb
.
invoke
(
"toString"
),
"Tb(id=1, field1=ts1, field2=null
, incrementsColumnsInfoMap={}
)"
);
// super
ObjectUtil
tbLombokWithBLOBsBuilder
=
new
ObjectUtil
(
loader
.
loadClass
(
packagz
+
".TbLombokWithBLOBs"
).
getMethod
(
"builder"
).
invoke
(
null
));
tbLombokWithBLOBsBuilder
.
invoke
(
"field3"
,
"ts3"
);
Assert
.
assertEquals
(
tbLombokWithBLOBsBuilder
.
invoke
(
"toString"
),
"TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null), field1=null, incF1=null), field3=ts3, field4=null)"
);
Assert
.
assertEquals
(
tbLombokWithBLOBsBuilder
.
invoke
(
"toString"
),
"TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null
, incrementsColumnsInfoMap={}
), field1=null, incF1=null), field3=ts3, field4=null)"
);
tbLombokWithBLOBsBuilder
.
invoke
(
"field1"
,
"ts1"
);
Assert
.
assertEquals
(
tbLombokWithBLOBsBuilder
.
invoke
(
"toString"
),
"TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null), field1=ts1, incF1=null), field3=ts3, field4=null)"
);
Assert
.
assertEquals
(
tbLombokWithBLOBsBuilder
.
invoke
(
"toString"
),
"TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null
, incrementsColumnsInfoMap={}
), field1=ts1, incF1=null), field3=ts3, field4=null)"
);
tbLombokWithBLOBsBuilder
.
invoke
(
"id"
,
100L
);
Assert
.
assertEquals
(
tbLombokWithBLOBsBuilder
.
invoke
(
"toString"
),
"TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=100, key1=null), field1=ts1, incF1=null), field3=ts3, field4=null)"
);
Assert
.
assertEquals
(
tbLombokWithBLOBsBuilder
.
invoke
(
"toString"
),
"TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=100, key1=null, incrementsColumnsInfoMap={}), field1=ts1, incF1=null), field3=ts3, field4=null)"
);
// ------------------------------------- 测试 sql 执行 ----------------------------------------
// 1. 测试updateByExample、updateByExampleSelective
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
tbBuilder
=
new
ObjectUtil
(
loader
.
loadClass
(
packagz
+
".Tb"
).
getMethod
(
"builder"
).
invoke
(
null
));
ObjectUtil
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$TbBuilder$Inc#INC"
);
tbBuilder
.
invoke
(
"field2"
,
100
,
tbBuilderInc
.
getObject
());
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateByExample"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb set id = null, field1 = 'null', field2 = field2 + 100 WHERE ( id = '3' )"
);
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET field2 = field2 + 100 WHERE ( id = '3' )"
);
// 执行
// inc_f1 增加100
Object
result
=
tbMapper
.
invoke
(
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select field2 from tb where id = 3"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"field2"
),
103
);
// 2. 测试有SuperBuilder的情况
ObjectUtil
tbLombokMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbLombokMapper"
)));
ObjectUtil
tbLombokExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbLombokExample"
);
criteria
=
new
ObjectUtil
(
tbLombokExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andKey1EqualTo"
,
"key1"
);
tbLombokWithBLOBsBuilder
=
new
ObjectUtil
(
loader
.
loadClass
(
packagz
+
".TbLombokWithBLOBs"
).
getMethod
(
"builder"
).
invoke
(
null
));
tbLombokWithBLOBsBuilder
.
invoke
(
"field3"
,
"f3"
);
ObjectUtil
tbLombokKeyBuilderIncINC
=
new
ObjectUtil
(
loader
,
packagz
+
".TbLombokKey$TbLombokKeyBuilder$Inc#INC"
);
tbLombokWithBLOBsBuilder
.
invoke
(
"incF1"
,
(
short
)
1
,
tbLombokKeyBuilderIncINC
.
getObject
());
tbLombokWithBLOBsBuilder
.
invoke
(
"field1"
,
"ts33"
);
ObjectUtil
tbLombokKeyBuilderIncDEC
=
new
ObjectUtil
(
loader
,
packagz
+
".TbLombokKey$TbLombokKeyBuilder$Inc#DEC"
);
tbLombokWithBLOBsBuilder
.
invoke
(
"id"
,
100L
,
tbLombokKeyBuilderIncDEC
.
getObject
());
tbLombokWithBLOBsBuilder
.
invoke
(
"key1"
,
"key100"
);
// sql
sql
=
SqlHelper
.
getFormatMapperSql
(
tbLombokMapper
.
getObject
(),
"updateByExampleSelective"
,
tbLombokWithBLOBsBuilder
.
invoke
(
"build"
),
tbLombokExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb_lombok SET id = id - 100 , key1 = 'key100', field1 = 'ts33', inc_f1 = inc_f1 + 1 , field3 = 'f3' WHERE ( key1 = 'key1' )"
);
// 执行
result
=
tbLombokMapper
.
invoke
(
"updateByExampleSelective"
,
tbLombokWithBLOBsBuilder
.
invoke
(
"build"
),
tbLombokExample
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select * from tb_lombok where key1 = 'key100'"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
1
);
Assert
.
assertEquals
(
rs
.
getInt
(
"id"
),
-
99
);
}
});
}
...
...
src/test/resources/scripts/IncrementsPlugin/init-lombok.sql
View file @
d0233d96
...
...
@@ -61,7 +61,7 @@ CREATE TABLE `tb_lombok` (
`id`
bigint
(
20
)
NOT
NULL
COMMENT
'注释1'
,
`key1`
varchar
(
20
)
NOT
NULL
,
`field1`
varchar
(
10
)
COMMENT
'注释2'
,
`inc_f1`
tinyint
(
1
),
`inc_f1`
smallint
(
3
),
`field3`
longtext
,
`field4`
longtext
,
PRIMARY
KEY
(
`id`
,
`key1`
)
...
...
src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-LombokPlugin.xml
View file @
d0233d96
...
...
@@ -57,7 +57,7 @@
<property
name=
"incrementsColumns"
value=
"inc_f1, field2"
/>
</table>
<table
tableName=
"tb_lombok"
>
<property
name=
"incrementsColumns"
value=
"inc_f1"
/>
<property
name=
"incrementsColumns"
value=
"inc_f1
,id
"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
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