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
23cf1069
Commit
23cf1069
authored
May 02, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构代码插件依赖关系
parent
78e16adb
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
168 additions
and
124 deletions
+168
-124
README.md
README.md
+44
-4
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
...com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
+62
-5
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
...ybatis/generator/plugins/utils/IncrementsPluginTools.java
+2
-58
src/main/java/com/itfsw/mybatis/generator/plugins/utils/PluginTools.java
...om/itfsw/mybatis/generator/plugins/utils/PluginTools.java
+7
-35
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
...tis/generator/plugins/utils/XmlElementGeneratorTools.java
+28
-22
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
.../mybatis/generator/plugins/utils/hook/HookAggregator.java
+12
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IIncrementsPluginHook.java
...s/generator/plugins/utils/hook/IIncrementsPluginHook.java
+13
-0
No files found.
README.md
View file @
23cf1069
...
@@ -27,7 +27,7 @@ Maven引用:
...
@@ -27,7 +27,7 @@ Maven引用:
<dependency>
<dependency>
<groupId>
com.itfsw
</groupId>
<groupId>
com.itfsw
</groupId>
<artifactId>
mybatis-generator-plugin
</artifactId>
<artifactId>
mybatis-generator-plugin
</artifactId>
<version>
1.1.
2
</version>
<version>
1.1.
3
</version>
</dependency>
</dependency>
```
```
---------------------------------------
---------------------------------------
...
@@ -556,17 +556,57 @@ public class Test {
...
@@ -556,17 +556,57 @@ public class Test {
```
```
### 10. Selective选择插入更新增强插件
### 10. Selective选择插入更新增强插件
项目中往往需要指定某些字段进行插入或者更新,或者把某些字段进行设置null处理,这种情况下原生xxxSelective方法往往不能达到需求,因为它的判断条件是对象字段是否为null,这种情况下可使用该插件对xxxSelective方法进行增强。
项目中往往需要指定某些字段进行插入或者更新,或者把某些字段进行设置null处理,这种情况下原生xxxSelective方法往往不能达到需求,因为它的判断条件是对象字段是否为null,这种情况下可使用该插件对xxxSelective方法进行增强。
>warning:配置插件时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
>warning:配置老版本插件(<=1.1.2)时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
>warning:以前老版本(<=1.1.2)插件处理需要指定的列时是放入Model中指定的,但在实际使用过程中有同事反馈这个处理有点反直觉,导致某些新同事不能及时找到对应方法,而且和增强的SelectSelectivePlugin以及UpsertSelective使用方式都不一致,所以统一修改之。
插件:
插件:
```
xml
```
xml
<!-- Selective选择插入更新增强插件
!请配在所有插件末尾以便最后执行
-->
<!-- Selective选择插入更新增强插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"
/>
```
```
使用:
使用:
```
java
```
java
public
class
Test
{
public
class
Test
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
// ------------------------------ 新版本(SelectiveEnhancedPlugin)--------------------------------
// 1. 指定插入或更新字段
Tb
tb
=
new
Tb
.
Builder
()
.
field1
(
1
)
.
field2
(
"xx2"
)
.
field3
(
1
)
.
createTime
(
new
Date
())
.
build
();
// 只插入或者更新field1,field2字段
this
.
tbMapper
.
insertSelective
(
tb
,
Tb
.
Column
.
field1
,
Tb
.
Column
.
field2
);
this
.
tbMapper
.
updateByExampleSelective
(
tb
,
new
TbExample
()
.
createCriteria
()
.
andIdEqualTo
(
1
l
)
.
example
(),
Tb
.
Column
.
field1
,
Tb
.
Column
.
field2
);
this
.
tbMapper
.
updateByPrimaryKeySelective
(
tb
,
Tb
.
Column
.
field1
,
Tb
.
Column
.
field2
);
this
.
tbMapper
.
upsertSelective
(
tb
,
Tb
.
Column
.
field1
,
Tb
.
Column
.
field2
);
this
.
tbMapper
.
upsertByExampleSelective
(
tb
,
new
TbExample
()
.
createCriteria
()
.
andField3EqualTo
(
1
)
.
example
(),
Tb
.
Column
.
field1
,
Tb
.
Column
.
field2
);
// 2. 更新某些字段为null
this
.
tbMapper
.
updateByPrimaryKeySelective
(
new
Tb
.
Builder
()
.
id
(
1
l
)
.
field1
(
null
)
// 方便展示,不用设也可以
.
build
(),
Tb
.
Column
.
field1
);
// ------------------------------ 老版本(SelectiveEnhancedPlugin)--------------------------------
// 1. 指定插入或更新字段
// 1. 指定插入或更新字段
Tb
tb
=
new
Tb
.
Builder
()
Tb
tb
=
new
Tb
.
Builder
()
.
field1
(
1
)
.
field1
(
1
)
...
@@ -724,7 +764,7 @@ Mybatis Generator是原生支持自定义注释的(commentGenerator配置type
...
@@ -724,7 +764,7 @@ Mybatis Generator是原生支持自定义注释的(commentGenerator配置type
</plugin>
</plugin>
</xml>
</xml>
```
```
使用(
参考模板
):
使用(
[
参考模板
](
https://github.com/itfsw/mybatis-generator-plugin/blob/master/src/main/resources/default-comment.ftl
)
):
```
xml
```
xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<template>
<template>
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
View file @
23cf1069
...
@@ -17,13 +17,16 @@
...
@@ -17,13 +17,16 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.*
;
import
com.itfsw.mybatis.generator.plugins.utils.*
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IIncrementsPluginHook
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
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.JavaBeansUtil
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
...
@@ -37,7 +40,7 @@ import java.util.List;
...
@@ -37,7 +40,7 @@ import java.util.List;
* @time:2017/6/19 15:20
* @time:2017/6/19 15:20
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
IncrementsPlugin
extends
BasePlugin
implements
IModelBuilderPluginHook
{
public
class
IncrementsPlugin
extends
BasePlugin
implements
IModelBuilderPluginHook
,
IIncrementsPluginHook
{
public
static
final
String
PRO_INCREMENTS_COLUMNS
=
"incrementsColumns"
;
// incrementsColumns property
public
static
final
String
PRO_INCREMENTS_COLUMNS
=
"incrementsColumns"
;
// incrementsColumns property
public
static
final
String
FIELD_INC_MAP
=
"incrementsColumnsInfoMap"
;
// 为了防止和用户数据库字段冲突,特殊命名
public
static
final
String
FIELD_INC_MAP
=
"incrementsColumnsInfoMap"
;
// 为了防止和用户数据库字段冲突,特殊命名
public
static
final
String
METHOD_INC_CHECK
=
"hasIncsForColumn"
;
// inc 检查方法名称
public
static
final
String
METHOD_INC_CHECK
=
"hasIncsForColumn"
;
// inc 检查方法名称
...
@@ -234,6 +237,58 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
...
@@ -234,6 +237,58 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
return
true
;
return
true
;
}
}
// =============================================== IIncrementsPluginHook ===================================================
/**
* 生成增量操作节点
* @param introspectedColumn
* @param prefix
* @param hasComma
* @return
*/
@Override
public
List
<
Element
>
incrementElementGenerated
(
IntrospectedColumn
introspectedColumn
,
String
prefix
,
boolean
hasComma
)
{
List
<
Element
>
list
=
new
ArrayList
<>();
if
(
incTools
.
supportColumn
(
introspectedColumn
)){
// 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"
,
(
prefix
!=
null
?
prefix
:
"_parameter."
)
+
IncrementsPlugin
.
METHOD_INC_CHECK
+
"('"
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
"')"
));
TextElement
spec
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" ${"
+
(
prefix
!=
null
?
prefix
:
""
)
+
IncrementsPlugin
.
FIELD_INC_MAP
+
"."
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
".value} "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
when
.
addElement
(
spec
);
choose
.
addElement
(
when
);
// 启用了增量操作
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
TextElement
normal
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
otherwise
.
addElement
(
normal
);
choose
.
addElement
(
otherwise
);
list
.
add
(
choose
);
// 3. 结尾逗号
if
(
hasComma
)
{
list
.
add
(
new
TextElement
(
","
));
}
}
return
list
;
}
// =================================================== 原生方法的支持 ====================================================
// =================================================== 原生方法的支持 ====================================================
/**
/**
...
@@ -255,9 +310,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
...
@@ -255,9 +310,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
String
columnName
=
strs
[
0
].
trim
();
String
columnName
=
strs
[
0
].
trim
();
IntrospectedColumn
introspectedColumn
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
columnName
);
IntrospectedColumn
introspectedColumn
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
columnName
);
// 查找是否需要进行增量操作
// 查找是否需要进行增量操作
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
List
<
Element
>
incrementEles
=
PluginTools
.
getHook
(
IIncrementsPluginHook
.
class
).
incrementElementGenerated
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
,
true
);
if
(!
incrementEles
.
isEmpty
())
{
xmlElement
.
getElements
().
clear
();
xmlElement
.
getElements
().
clear
();
xmlElement
.
getElements
().
addAll
(
inc
Tools
.
generatedIncrementsElement
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
,
true
)
);
xmlElement
.
getElements
().
addAll
(
inc
rementEles
);
}
}
}
}
}
}
...
@@ -284,8 +340,9 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
...
@@ -284,8 +340,9 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
String
columnName
=
text
.
split
(
"="
)[
0
].
trim
();
String
columnName
=
text
.
split
(
"="
)[
0
].
trim
();
IntrospectedColumn
introspectedColumn
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
columnName
);
IntrospectedColumn
introspectedColumn
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
columnName
);
// 查找判断是否需要进行节点替换
// 查找判断是否需要进行节点替换
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
List
<
Element
>
incrementEles
=
PluginTools
.
getHook
(
IIncrementsPluginHook
.
class
).
incrementElementGenerated
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
,
text
.
endsWith
(
","
));
newEles
.
addAll
(
incTools
.
generatedIncrementsElement
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
,
text
.
endsWith
(
","
)));
if
(!
incrementEles
.
isEmpty
())
{
newEles
.
addAll
(
incrementEles
);
continue
;
continue
;
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
View file @
23cf1069
...
@@ -21,11 +21,6 @@ import com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin;
...
@@ -21,11 +21,6 @@ import com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType
;
import
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType
;
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.config.Context
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
org.mybatis.generator.internal.util.StringUtility
;
...
@@ -46,10 +41,9 @@ public class IncrementsPluginTools {
...
@@ -46,10 +41,9 @@ public class IncrementsPluginTools {
/**
/**
* 构造函数
* 构造函数
* @param context
* @param introspectedTable
* @param introspectedTable
*/
*/
private
IncrementsPluginTools
(
Context
context
,
IntrospectedTable
introspectedTable
)
{
private
IncrementsPluginTools
(
IntrospectedTable
introspectedTable
)
{
this
.
introspectedTable
=
introspectedTable
;
this
.
introspectedTable
=
introspectedTable
;
}
}
...
@@ -61,7 +55,7 @@ public class IncrementsPluginTools {
...
@@ -61,7 +55,7 @@ public class IncrementsPluginTools {
* @return
* @return
*/
*/
public
static
IncrementsPluginTools
getTools
(
Context
context
,
IntrospectedTable
introspectedTable
,
List
<
String
>
warnings
)
{
public
static
IncrementsPluginTools
getTools
(
Context
context
,
IntrospectedTable
introspectedTable
,
List
<
String
>
warnings
)
{
IncrementsPluginTools
tools
=
new
IncrementsPluginTools
(
context
,
introspectedTable
);
IncrementsPluginTools
tools
=
new
IncrementsPluginTools
(
introspectedTable
);
// 判断是否启用了插件
// 判断是否启用了插件
if
(
PluginTools
.
getPluginConfiguration
(
context
,
IncrementsPlugin
.
class
)
!=
null
)
{
if
(
PluginTools
.
getPluginConfiguration
(
context
,
IncrementsPlugin
.
class
)
!=
null
)
{
String
incrementsColumns
=
introspectedTable
.
getTableConfigurationProperty
(
IncrementsPlugin
.
PRO_INCREMENTS_COLUMNS
);
String
incrementsColumns
=
introspectedTable
.
getTableConfigurationProperty
(
IncrementsPlugin
.
PRO_INCREMENTS_COLUMNS
);
...
@@ -119,54 +113,4 @@ public class IncrementsPluginTools {
...
@@ -119,54 +113,4 @@ public class IncrementsPluginTools {
}
}
return
false
;
return
false
;
}
}
/**
* 生成增量操作节点
* @param introspectedColumn
* @param prefix
* @param hasComma
*/
public
List
<
Element
>
generatedIncrementsElement
(
IntrospectedColumn
introspectedColumn
,
String
prefix
,
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"
,
(
prefix
!=
null
?
prefix
:
"_parameter."
)
+
IncrementsPlugin
.
METHOD_INC_CHECK
+
"('"
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
"')"
));
TextElement
spec
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" ${"
+
(
prefix
!=
null
?
prefix
:
""
)
+
IncrementsPlugin
.
FIELD_INC_MAP
+
"."
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
".value} "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
when
.
addElement
(
spec
);
choose
.
addElement
(
when
);
// 启用了增量操作
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
TextElement
normal
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
otherwise
.
addElement
(
normal
);
choose
.
addElement
(
otherwise
);
list
.
add
(
choose
);
// 3. 结尾逗号
if
(
hasComma
)
{
list
.
add
(
new
TextElement
(
","
));
}
return
list
;
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/PluginTools.java
View file @
23cf1069
...
@@ -43,15 +43,14 @@ public class PluginTools {
...
@@ -43,15 +43,14 @@ public class PluginTools {
* @param <T>
* @param <T>
* @return
* @return
*/
*/
public
static
<
T
>
T
getHook
(
Class
<
T
>
clazz
){
public
static
<
T
>
T
getHook
(
Class
<
T
>
clazz
)
{
return
(
T
)
HookAggregator
.
getInstance
();
return
(
T
)
HookAggregator
.
getInstance
();
}
}
/**
/**
* 检查插件依赖
* 检查插件依赖
*
* @param context 上下文
* @param context 上下文
* @param plugin 插件
* @param plugin 插件
* @return
* @return
*/
*/
public
static
boolean
checkDependencyPlugin
(
Context
context
,
Class
plugin
)
{
public
static
boolean
checkDependencyPlugin
(
Context
context
,
Class
plugin
)
{
...
@@ -60,10 +59,8 @@ public class PluginTools {
...
@@ -60,10 +59,8 @@ public class PluginTools {
/**
/**
* 获取插件所在位置
* 获取插件所在位置
*
* @param context 上下文
* @param context 上下文
* @param plugin 插件
* @param plugin 插件
*
* @return -1:未找到
* @return -1:未找到
*/
*/
public
static
int
getPluginIndex
(
Context
context
,
Class
plugin
)
{
public
static
int
getPluginIndex
(
Context
context
,
Class
plugin
)
{
...
@@ -97,40 +94,15 @@ public class PluginTools {
...
@@ -97,40 +94,15 @@ public class PluginTools {
/**
/**
* 获取插件配置
* 获取插件配置
*
* @param context 上下文
* @param context 上下文
* @param plugin 插件
* @param plugin
插件
* @return
* @return
*/
*/
public
static
PluginConfiguration
getPluginConfiguration
(
Context
context
,
Class
plugin
){
public
static
PluginConfiguration
getPluginConfiguration
(
Context
context
,
Class
plugin
)
{
int
index
=
getPluginIndex
(
context
,
plugin
);
int
index
=
getPluginIndex
(
context
,
plugin
);
if
(
index
>
-
1
){
if
(
index
>
-
1
)
{
return
getConfigPlugins
(
context
).
get
(
index
);
return
getConfigPlugins
(
context
).
get
(
index
);
}
}
return
null
;
return
null
;
}
}
/**
* 插件位置需要配置在某些插件后面
*
* @param context
* @param plugin
* @param warnings
* @param plugins
* @return
*/
public
static
boolean
shouldAfterPlugins
(
Context
context
,
Class
plugin
,
List
<
String
>
warnings
,
Class
...
plugins
){
int
index
=
getPluginIndex
(
context
,
plugin
);
if
(
plugins
!=
null
){
for
(
Class
cls
:
plugins
){
int
index1
=
getPluginIndex
(
context
,
cls
);
if
(
index1
!=
-
1
&&
index1
>=
index
){
warnings
.
add
(
"itfsw:插件"
+
plugin
.
getTypeName
()
+
"插件建议配置在插件"
+
cls
.
getTypeName
()+
"后面,否则某些功能可能得不到增强!"
);
return
false
;
}
}
return
true
;
}
return
false
;
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
View file @
23cf1069
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IIncrementsPluginHook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.OutputUtilities
;
import
org.mybatis.generator.api.dom.OutputUtilities
;
...
@@ -133,20 +134,6 @@ public class XmlElementGeneratorTools {
...
@@ -133,20 +134,6 @@ public class XmlElementGeneratorTools {
}
}
}
}
/**
* 移除 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
* @param element
* @param introspectedTable
*/
public
static
void
removeUseGeneratedKeys
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
GeneratedKey
gk
=
introspectedTable
.
getGeneratedKey
();
if
(
gk
!=
null
)
{
removeAttribute
(
element
,
"useGeneratedKeys"
);
removeAttribute
(
element
,
"keyProperty"
);
removeAttribute
(
element
,
"keyColumn"
);
}
}
/**
/**
* 移除属性
* 移除属性
* @param element
* @param element
...
@@ -211,7 +198,7 @@ public class XmlElementGeneratorTools {
...
@@ -211,7 +198,7 @@ public class XmlElementGeneratorTools {
* @param bracket
* @param bracket
* @return
* @return
*/
*/
public
static
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
public
static
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
return
generateCommColumns
(
columns
,
prefix
,
bracket
,
1
);
return
generateCommColumns
(
columns
,
prefix
,
bracket
,
1
);
}
}
...
@@ -396,9 +383,20 @@ public class XmlElementGeneratorTools {
...
@@ -396,9 +383,20 @@ public class XmlElementGeneratorTools {
switch
(
type
)
{
switch
(
type
)
{
case
3
:
case
3
:
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
List
<
Element
>
incrementEles
=
PluginTools
.
getHook
(
IIncrementsPluginHook
.
class
).
incrementElementGenerated
(
introspectedColumn
,
prefix
,
columnIterator
.
hasNext
());
sb
.
append
(
" = "
);
if
(
incrementEles
.
isEmpty
())
{
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
// 增量插件支持
if
(
sb
.
length
()
>
0
)
{
list
.
add
(
new
TextElement
(
sb
.
toString
()));
sb
.
setLength
(
0
);
}
list
.
addAll
(
incrementEles
);
}
else
{
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
}
break
;
break
;
case
2
:
case
2
:
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
...
@@ -453,7 +451,7 @@ public class XmlElementGeneratorTools {
...
@@ -453,7 +451,7 @@ public class XmlElementGeneratorTools {
* @param bracket
* @param bracket
* @return
* @return
*/
*/
private
static
XmlElement
generateTrim
(
boolean
bracket
){
private
static
XmlElement
generateTrim
(
boolean
bracket
)
{
XmlElement
trimEle
=
new
XmlElement
(
"trim"
);
XmlElement
trimEle
=
new
XmlElement
(
"trim"
);
if
(
bracket
)
{
if
(
bracket
)
{
trimEle
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
trimEle
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
...
@@ -495,12 +493,20 @@ public class XmlElementGeneratorTools {
...
@@ -495,12 +493,20 @@ public class XmlElementGeneratorTools {
* @param element
* @param element
* @param introspectedColumn
* @param introspectedColumn
* @param prefix
* @param prefix
* @param type 1:key,2:value,3:set
* @param type
1:key,2:value,3:set
*/
*/
private
static
void
generateSelectiveCommColumnTo
(
XmlElement
element
,
IntrospectedColumn
introspectedColumn
,
String
prefix
,
int
type
){
private
static
void
generateSelectiveCommColumnTo
(
XmlElement
element
,
IntrospectedColumn
introspectedColumn
,
String
prefix
,
int
type
)
{
switch
(
type
)
{
switch
(
type
)
{
case
3
:
case
3
:
element
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
List
<
Element
>
incrementEles
=
PluginTools
.
getHook
(
IIncrementsPluginHook
.
class
).
incrementElementGenerated
(
introspectedColumn
,
prefix
,
true
);
if
(
incrementEles
.
isEmpty
())
{
// 增量插件支持
for
(
Element
ele
:
incrementEles
)
{
element
.
addElement
(
ele
);
}
}
else
{
element
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
}
break
;
break
;
case
2
:
case
2
:
element
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
element
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
View file @
23cf1069
...
@@ -24,6 +24,7 @@ import org.mybatis.generator.api.dom.java.InnerClass;
...
@@ -24,6 +24,7 @@ import org.mybatis.generator.api.dom.java.InnerClass;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.Context
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
...
@@ -95,7 +96,17 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
...
@@ -95,7 +96,17 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
// ============================================= IIncrementsPluginHook ==============================================
// ============================================= IIncrementsPluginHook ==============================================
@Override
public
List
<
Element
>
incrementElementGenerated
(
IntrospectedColumn
introspectedColumn
,
String
prefix
,
boolean
hasComma
)
{
if
(
this
.
getPlugins
(
IIncrementsPluginHook
.
class
).
isEmpty
())
{
return
new
ArrayList
<>();
}
else
{
return
this
.
getPlugins
(
IIncrementsPluginHook
.
class
).
get
(
0
).
incrementElementGenerated
(
introspectedColumn
,
prefix
,
hasComma
);
}
}
// ============================================ IModelBuilderPluginHook =============================================
// ============================================ IModelBuilderPluginHook =============================================
@Override
@Override
public
boolean
modelBuilderClassGenerated
(
TopLevelClass
topLevelClass
,
InnerClass
builderClass
,
List
<
IntrospectedColumn
>
columns
,
IntrospectedTable
introspectedTable
)
{
public
boolean
modelBuilderClassGenerated
(
TopLevelClass
topLevelClass
,
InnerClass
builderClass
,
List
<
IntrospectedColumn
>
columns
,
IntrospectedTable
introspectedTable
)
{
for
(
IModelBuilderPluginHook
plugin
:
this
.
getPlugins
(
IModelBuilderPluginHook
.
class
))
{
for
(
IModelBuilderPluginHook
plugin
:
this
.
getPlugins
(
IModelBuilderPluginHook
.
class
))
{
...
@@ -107,6 +118,7 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
...
@@ -107,6 +118,7 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
}
}
// ================================================= IUpsertPluginHook ===============================================
// ================================================= IUpsertPluginHook ===============================================
@Override
@Override
public
boolean
clientUpsertSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
public
boolean
clientUpsertSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
for
(
IUpsertPluginHook
plugin
:
this
.
getPlugins
(
IUpsertPluginHook
.
class
))
{
for
(
IUpsertPluginHook
plugin
:
this
.
getPlugins
(
IUpsertPluginHook
.
class
))
{
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IIncrementsPluginHook.java
View file @
23cf1069
...
@@ -16,6 +16,11 @@
...
@@ -16,6 +16,11 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
java.util.List
;
/**
/**
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*
*
...
@@ -25,4 +30,12 @@ package com.itfsw.mybatis.generator.plugins.utils.hook;
...
@@ -25,4 +30,12 @@ package com.itfsw.mybatis.generator.plugins.utils.hook;
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
interface
IIncrementsPluginHook
{
public
interface
IIncrementsPluginHook
{
/**
* 生成增量操作节点
* @param introspectedColumn
* @param prefix
* @param hasComma
* @return
*/
List
<
Element
>
incrementElementGenerated
(
IntrospectedColumn
introspectedColumn
,
String
prefix
,
boolean
hasComma
);
}
}
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