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
24368c58
Commit
24368c58
authored
Jun 21, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增增量插件
parent
a4980ab0
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
301 additions
and
15 deletions
+301
-15
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
...com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
+257
-1
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
...m/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
+21
-13
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
...sw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+1
-1
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
...tis/generator/plugins/utils/XmlElementGeneratorTools.java
+22
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
View file @
24368c58
...
...
@@ -18,7 +18,18 @@ package com.itfsw.mybatis.generator.plugins;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.PluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.Element
;
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.StringUtility
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
...
...
@@ -31,9 +42,12 @@ import java.util.List;
*/
public
class
IncrementsPlugin
extends
BasePlugin
{
public
static
final
String
PRE_INCREMENTS_COLUMNS
=
"incrementsColumns"
;
// incrementsColumns property
private
List
<
IntrospectedColumn
>
columns
;
// 需要进行自增的字段
/**
* {@inheritDoc}
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param warnings
* @return
*/
@Override
public
boolean
validate
(
List
<
String
>
warnings
)
{
...
...
@@ -46,4 +60,246 @@ public class IncrementsPlugin extends BasePlugin {
return
super
.
validate
(
warnings
);
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param introspectedTable
*/
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
this
.
columns
=
new
ArrayList
<>();
// 获取表配置信息
String
incrementsColumns
=
introspectedTable
.
getTableConfigurationProperty
(
IncrementsPlugin
.
PRE_INCREMENTS_COLUMNS
);
if
(
StringUtility
.
stringHasValue
(
incrementsColumns
))
{
// 切分
String
[]
incrementsColumnsStrs
=
incrementsColumns
.
split
(
","
);
List
<
IntrospectedColumn
>
columns
=
introspectedTable
.
getAllColumns
();
for
(
String
incrementsColumnsStr
:
incrementsColumnsStrs
)
{
IntrospectedColumn
column
=
introspectedTable
.
getColumn
(
incrementsColumnsStr
);
if
(
column
==
null
)
{
logger
.
warn
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件没有找到column为"
+
incrementsColumnsStr
+
"的字段!"
);
}
else
if
(
columns
.
indexOf
(
column
)
!=
-
1
)
{
this
.
columns
.
add
(
column
);
}
}
}
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
// 具体实现在 ModelBuilderPlugin.generateModelBuilder
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public
boolean
modelRecordWithBLOBsClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
// 具体实现在 ModelBuilderPlugin.generateModelBuilder
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpdateByExampleSelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
generatedWithSelective
(
element
,
introspectedTable
,
true
);
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpdateByExampleWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
generatedWithoutSelective
(
element
,
introspectedTable
,
true
);
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpdateByExampleWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
generatedWithoutSelective
(
element
,
introspectedTable
,
true
);
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
generatedWithSelective
(
element
,
introspectedTable
,
false
);
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
generatedWithoutSelective
(
element
,
introspectedTable
,
false
);
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
generatedWithoutSelective
(
element
,
introspectedTable
,
false
);
return
true
;
}
/**
* 是否需要替换
* @param columnName
* @return
*/
private
boolean
needReplace
(
String
columnName
)
{
for
(
IntrospectedColumn
introspectedColumn
:
this
.
columns
)
{
if
(
introspectedColumn
.
getActualColumnName
().
equals
(
columnName
))
{
return
true
;
}
}
return
false
;
}
/**
* 有Selective代码生成
* @param element
*/
private
void
generatedWithSelective
(
XmlElement
element
,
IntrospectedTable
introspectedTable
,
boolean
hasPrefix
)
{
if
(
columns
.
size
()
>
0
)
{
// 查找 set->if->text
List
<
XmlElement
>
sets
=
XmlElementGeneratorTools
.
findXmlElements
(
element
,
"set"
);
if
(
sets
.
size
()
>
0
)
{
List
<
XmlElement
>
ifs
=
XmlElementGeneratorTools
.
findXmlElements
(
sets
.
get
(
0
),
"if"
);
if
(
ifs
.
size
()
>
0
)
{
for
(
XmlElement
xmlElement
:
ifs
)
{
// 下面为if的text节点
List
<
Element
>
textEles
=
xmlElement
.
getElements
();
TextElement
textEle
=
(
TextElement
)
textEles
.
get
(
0
);
String
[]
strs
=
textEle
.
getContent
().
split
(
"="
);
String
columnName
=
strs
[
0
].
trim
();
// 查找是否需要进行增量操作
if
(
needReplace
(
columnName
))
{
IntrospectedColumn
introspectedColumn
=
introspectedTable
.
getColumn
(
columnName
);
xmlElement
.
getElements
().
clear
();
xmlElement
.
getElements
().
addAll
(
generatedIncrementsElement
(
xmlElement
,
introspectedColumn
,
hasPrefix
,
true
));
}
}
}
}
}
}
/**
* 无Selective代码生成
* @param xmlElement
* @param introspectedTable
* @param hasPrefix
*/
private
void
generatedWithoutSelective
(
XmlElement
xmlElement
,
IntrospectedTable
introspectedTable
,
boolean
hasPrefix
)
{
if
(
columns
.
size
()
>
0
)
{
List
<
Element
>
newEles
=
new
ArrayList
<>();
for
(
Element
ele
:
xmlElement
.
getElements
())
{
// 找到text节点且格式为 set xx = xx 或者 xx = xx
if
(
ele
instanceof
TextElement
)
{
String
text
=
((
TextElement
)
ele
).
getContent
().
trim
();
if
(
text
.
matches
(
"(set\\s)?\\S+\\s?=.*"
))
{
// 清理 set 操作
text
=
text
.
replaceFirst
(
"set\\s"
,
""
).
trim
();
String
columnName
=
text
.
split
(
"="
)[
0
].
trim
();
// 查找判断是否需要进行节点替换
if
(
needReplace
(
columnName
))
{
IntrospectedColumn
introspectedColumn
=
introspectedTable
.
getColumn
(
columnName
);
newEles
.
addAll
(
generatedIncrementsElement
(
xmlElement
,
introspectedColumn
,
hasPrefix
,
text
.
endsWith
(
","
)));
continue
;
}
}
}
newEles
.
add
(
ele
);
}
// 替换节点
xmlElement
.
getElements
().
clear
();
xmlElement
.
getElements
().
addAll
(
newEles
);
}
}
/**
* 生成增量操作节点
* @param element
* @param introspectedColumn
* @param hasPrefix
* @param hasComma
*/
private
List
<
Element
>
generatedIncrementsElement
(
XmlElement
element
,
IntrospectedColumn
introspectedColumn
,
boolean
hasPrefix
,
boolean
hasComma
)
{
List
<
Element
>
list
=
new
ArrayList
<>();
// 1. column = 节点
list
.
add
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
));
// 2. 选择节点
// 条件
XmlElement
choose
=
new
XmlElement
(
"choose"
);
// 没有启用增量操作
XmlElement
when
=
new
XmlElement
(
"when"
);
when
.
addAttribute
(
new
Attribute
(
"test"
,
(
hasPrefix
?
"record"
:
"_parameter"
)
+
".incs.isEmpty()"
));
TextElement
normal
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
when
.
addElement
(
normal
);
choose
.
addElement
(
when
);
// 启用了增量操作
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
TextElement
spec
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" ${"
+
(
hasPrefix
?
"record"
:
"_parameter"
)
+
".incs."
+
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
".value} "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
otherwise
.
addElement
(
spec
);
choose
.
addElement
(
otherwise
);
list
.
add
(
choose
);
// 3. 结尾逗号
if
(
hasComma
)
{
list
.
add
(
new
TextElement
(
","
));
}
return
list
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
View file @
24368c58
...
...
@@ -40,17 +40,6 @@ public class ModelBuilderPlugin extends BasePlugin {
public
static
final
String
BUILDER_CLASS_NAME
=
"Builder"
;
// Builder 类名
private
FullyQualifiedJavaType
inc
;
// 是否支持Increments
/**
* 初始化阶段
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param introspectedTable
* @return
*/
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
this
.
inc
=
null
;
}
/**
* Model Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
...
...
@@ -153,8 +142,28 @@ public class ModelBuilderPlugin extends BasePlugin {
InnerEnum
eIncrements
=
new
InnerEnum
(
new
FullyQualifiedJavaType
(
"Inc"
));
eIncrements
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
eIncrements
.
setStatic
(
true
);
eIncrements
.
addEnumConstant
(
"INC,DEC"
);
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
);
logger
.
debug
(
"itfsw(数据Model属性对应Column获取插件):"
+
topLevelClass
.
getType
().
getShortName
()
+
".Column增加构造方法和column属性。"
);
Method
mValue
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fValue
);
commentGenerator
.
addGeneralMethodComment
(
mValue
,
introspectedTable
);
eIncrements
.
addMethod
(
mValue
);
innerClass
.
addInnerEnum
(
eIncrements
);
// 增加field
Field
fIncrements
=
JavaElementGeneratorTools
.
generateField
(
...
...
@@ -208,5 +217,4 @@ public class ModelBuilderPlugin extends BasePlugin {
return
innerClass
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
View file @
24368c58
...
...
@@ -228,7 +228,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
}
}
else
{
String
columnName
;
if
(
text
.
matches
(
".*
\\s*
=.*"
)){
if
(
text
.
matches
(
".*=.*"
)){
columnName
=
text
.
split
(
"="
)[
0
];
}
else
{
columnName
=
text
.
replaceAll
(
","
,
""
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
View file @
24368c58
...
...
@@ -25,6 +25,7 @@ import org.mybatis.generator.api.dom.xml.XmlElement;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.config.GeneratedKey
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
...
...
@@ -377,4 +378,25 @@ public class XmlElementGeneratorTools {
return
eleTrim
;
}
/**
* 查找指定xml节点下指定节点名称的元素
*
* @param xmlElement
* @param name
* @return
*/
public
static
List
<
XmlElement
>
findXmlElements
(
XmlElement
xmlElement
,
String
name
){
List
<
XmlElement
>
list
=
new
ArrayList
<>();
List
<
Element
>
elements
=
xmlElement
.
getElements
();
for
(
Element
ele
:
elements
)
{
if
(
ele
instanceof
XmlElement
)
{
XmlElement
xmlElement1
=
(
XmlElement
)
ele
;
if
(
name
.
equalsIgnoreCase
(
xmlElement1
.
getName
()))
{
list
.
add
(
xmlElement1
);
}
}
}
return
list
;
}
}
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