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
706fad7e
Commit
706fad7e
authored
Jul 06, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bugfix:IncrementsPlugin插件自增参数配置字段取名不特殊,会和用户数据库表中字段存在冲突可能。同时补充该插件测试用例
parent
e6f607d3
Changes
12
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
531 additions
and
91 deletions
+531
-91
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
...com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
+2
-0
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
...m/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
+12
-3
src/main/java/com/itfsw/mybatis/generator/plugins/utils/FormatTools.java
...om/itfsw/mybatis/generator/plugins/utils/FormatTools.java
+44
-47
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
...ybatis/generator/plugins/utils/IncrementsPluginTools.java
+17
-15
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
...tfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
+1
-1
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
...itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
+208
-0
src/test/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePluginTest.java
.../mybatis/generator/plugins/SelectSelectivePluginTest.java
+3
-3
src/test/java/com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
...com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
+23
-17
src/test/java/com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
.../com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
+3
-5
src/test/resources/scripts/IncrementsPlugin/init.sql
src/test/resources/scripts/IncrementsPlugin/init.sql
+103
-0
src/test/resources/scripts/IncrementsPlugin/mybatis-generator-without-model-builder-plugin.xml
...Plugin/mybatis-generator-without-model-builder-plugin.xml
+53
-0
src/test/resources/scripts/IncrementsPlugin/mybatis-generator.xml
.../resources/scripts/IncrementsPlugin/mybatis-generator.xml
+62
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
View file @
706fad7e
...
@@ -40,6 +40,8 @@ import java.util.List;
...
@@ -40,6 +40,8 @@ import java.util.List;
*/
*/
public
class
IncrementsPlugin
extends
BasePlugin
{
public
class
IncrementsPlugin
extends
BasePlugin
{
public
static
final
String
PRE_INCREMENTS_COLUMNS
=
"incrementsColumns"
;
// incrementsColumns property
public
static
final
String
PRE_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
IncrementsPluginTools
incTools
;
// 增量插件工具
/**
/**
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
View file @
706fad7e
...
@@ -154,7 +154,6 @@ public class ModelBuilderPlugin extends BasePlugin {
...
@@ -154,7 +154,6 @@ public class ModelBuilderPlugin extends BasePlugin {
mInc
.
addParameter
(
new
Parameter
(
FullyQualifiedJavaType
.
getStringInstance
(),
"value"
));
mInc
.
addParameter
(
new
Parameter
(
FullyQualifiedJavaType
.
getStringInstance
(),
"value"
));
commentGenerator
.
addGeneralMethodComment
(
mInc
,
introspectedTable
);
commentGenerator
.
addGeneralMethodComment
(
mInc
,
introspectedTable
);
eIncrements
.
addMethod
(
mInc
);
eIncrements
.
addMethod
(
mInc
);
logger
.
debug
(
"itfsw(数据Model属性对应Column获取插件):"
+
topLevelClass
.
getType
().
getShortName
()
+
".Column增加构造方法和column属性。"
);
Method
mValue
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fValue
);
Method
mValue
=
JavaElementGeneratorTools
.
generateGetterMethod
(
fValue
);
commentGenerator
.
addGeneralMethodComment
(
mValue
,
introspectedTable
);
commentGenerator
.
addGeneralMethodComment
(
mValue
,
introspectedTable
);
...
@@ -163,7 +162,7 @@ public class ModelBuilderPlugin extends BasePlugin {
...
@@ -163,7 +162,7 @@ public class ModelBuilderPlugin extends BasePlugin {
innerClass
.
addInnerEnum
(
eIncrements
);
innerClass
.
addInnerEnum
(
eIncrements
);
// 增加field
// 增加field
Field
fIncrements
=
JavaElementGeneratorTools
.
generateField
(
Field
fIncrements
=
JavaElementGeneratorTools
.
generateField
(
"incs"
,
IncrementsPlugin
.
FIELD_INC_MAP
,
JavaVisibility
.
PROTECTED
,
JavaVisibility
.
PROTECTED
,
new
FullyQualifiedJavaType
(
"Map<String, "
+
incTools
.
getIncEnum
().
getFullyQualifiedName
()
+
">"
),
new
FullyQualifiedJavaType
(
"Map<String, "
+
incTools
.
getIncEnum
().
getFullyQualifiedName
()
+
">"
),
"new HashMap<String, "
+
incTools
.
getIncEnum
().
getFullyQualifiedName
()
+
">()"
"new HashMap<String, "
+
incTools
.
getIncEnum
().
getFullyQualifiedName
()
+
">()"
...
@@ -179,6 +178,16 @@ public class ModelBuilderPlugin extends BasePlugin {
...
@@ -179,6 +178,16 @@ public class ModelBuilderPlugin extends BasePlugin {
Method
mSetter
=
JavaElementGeneratorTools
.
generateSetterMethod
(
fIncrements
);
Method
mSetter
=
JavaElementGeneratorTools
.
generateSetterMethod
(
fIncrements
);
commentGenerator
.
addSetterComment
(
mSetter
,
introspectedTable
,
null
);
commentGenerator
.
addSetterComment
(
mSetter
,
introspectedTable
,
null
);
topLevelClass
.
addMethod
(
mSetter
);
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 中 添加字段支持
// Builder 中 添加字段支持
...
@@ -196,7 +205,7 @@ public class ModelBuilderPlugin extends BasePlugin {
...
@@ -196,7 +205,7 @@ public class ModelBuilderPlugin extends BasePlugin {
commentGenerator
.
addSetterComment
(
mIncrements
,
introspectedTable
,
column
);
commentGenerator
.
addSetterComment
(
mIncrements
,
introspectedTable
,
column
);
Method
setterMethod
=
JavaBeansUtil
.
getJavaBeansSetter
(
column
,
context
,
introspectedTable
);
Method
setterMethod
=
JavaBeansUtil
.
getJavaBeansSetter
(
column
,
context
,
introspectedTable
);
mIncrements
.
addBodyLine
(
"obj.
incs
.put(\""
+
column
.
getActualColumnName
()
+
"\", inc);"
);
mIncrements
.
addBodyLine
(
"obj.
"
+
IncrementsPlugin
.
FIELD_INC_MAP
+
"
.put(\""
+
column
.
getActualColumnName
()
+
"\", inc);"
);
mIncrements
.
addBodyLine
(
"obj."
+
setterMethod
.
getName
()
+
"("
+
field
.
getName
()
+
");"
);
mIncrements
.
addBodyLine
(
"obj."
+
setterMethod
.
getName
()
+
"("
+
field
.
getName
()
+
");"
);
mIncrements
.
addBodyLine
(
"return this;"
);
mIncrements
.
addBodyLine
(
"return this;"
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/FormatTools.java
View file @
706fad7e
...
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins.utils;
...
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins.utils;
import
org.mybatis.generator.api.dom.java.InnerClass
;
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.xml.Attribute
;
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.XmlElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
...
@@ -41,30 +42,7 @@ public class FormatTools {
...
@@ -41,30 +42,7 @@ public class FormatTools {
* @param method
* @param method
*/
*/
public
static
void
addMethodWithBestPosition
(
InnerClass
innerClass
,
Method
method
){
public
static
void
addMethodWithBestPosition
(
InnerClass
innerClass
,
Method
method
){
List
<
Method
>
methods
=
innerClass
.
getMethods
();
addMethodWithBestPosition
(
method
,
innerClass
.
getMethods
());
int
index
=
-
1
;
for
(
int
i
=
0
;
i
<
methods
.
size
();
i
++){
Method
m
=
methods
.
get
(
i
);
if
(
m
.
getName
().
equals
(
method
.
getName
())){
if
(
m
.
getParameters
().
size
()
<=
method
.
getParameters
().
size
()){
index
=
i
+
1
;
}
else
{
index
=
i
;
}
}
else
if
(
m
.
getName
().
startsWith
(
method
.
getName
())){
if
(
index
==
-
1
){
index
=
i
;
}
}
else
if
(
method
.
getName
().
startsWith
(
m
.
getName
())){
index
=
i
+
1
;
}
}
if
(
index
==
-
1
||
index
>=
methods
.
size
()){
innerClass
.
addMethod
(
method
);
}
else
{
methods
.
add
(
index
,
method
);
}
}
}
/**
/**
...
@@ -74,30 +52,17 @@ public class FormatTools {
...
@@ -74,30 +52,17 @@ public class FormatTools {
* @param method
* @param method
*/
*/
public
static
void
addMethodWithBestPosition
(
Interface
interfacz
,
Method
method
){
public
static
void
addMethodWithBestPosition
(
Interface
interfacz
,
Method
method
){
List
<
Method
>
methods
=
interfacz
.
getMethods
();
addMethodWithBestPosition
(
method
,
interfacz
.
getMethods
());
int
index
=
-
1
;
for
(
int
i
=
0
;
i
<
methods
.
size
();
i
++){
Method
m
=
methods
.
get
(
i
);
if
(
m
.
getName
().
equals
(
method
.
getName
())){
if
(
m
.
getParameters
().
size
()
<=
method
.
getParameters
().
size
()){
index
=
i
+
1
;
}
else
{
index
=
i
;
}
}
else
if
(
m
.
getName
().
startsWith
(
method
.
getName
())){
if
(
index
==
-
1
){
index
=
i
;
}
}
else
if
(
method
.
getName
().
startsWith
(
m
.
getName
())){
index
=
i
+
1
;
}
}
}
if
(
index
==
-
1
||
index
>=
methods
.
size
()){
/**
interfacz
.
addMethod
(
method
);
* 在最佳位置添加方法
}
else
{
*
methods
.
add
(
index
,
method
);
* @param topLevelClass
}
* @param method
*/
public
static
void
addMethodWithBestPosition
(
TopLevelClass
topLevelClass
,
Method
method
){
addMethodWithBestPosition
(
method
,
topLevelClass
.
getMethods
());
}
}
/**
/**
...
@@ -151,4 +116,36 @@ public class FormatTools {
...
@@ -151,4 +116,36 @@ public class FormatTools {
}
}
return
null
;
return
null
;
}
}
/**
* 获取最佳添加位置
*
* @param method
* @param methods
* @return
*/
private
static
void
addMethodWithBestPosition
(
Method
method
,
List
<
Method
>
methods
){
int
index
=
-
1
;
for
(
int
i
=
0
;
i
<
methods
.
size
();
i
++){
Method
m
=
methods
.
get
(
i
);
if
(
m
.
getName
().
equals
(
method
.
getName
())){
if
(
m
.
getParameters
().
size
()
<=
method
.
getParameters
().
size
()){
index
=
i
+
1
;
}
else
{
index
=
i
;
}
}
else
if
(
m
.
getName
().
startsWith
(
method
.
getName
())){
if
(
index
==
-
1
){
index
=
i
;
}
}
else
if
(
method
.
getName
().
startsWith
(
m
.
getName
())){
index
=
i
+
1
;
}
}
if
(
index
==
-
1
||
index
>=
methods
.
size
()){
methods
.
add
(
methods
.
size
(),
method
);
}
else
{
methods
.
add
(
index
,
method
);
}
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
View file @
706fad7e
...
@@ -96,10 +96,9 @@ public class IncrementsPluginTools {
...
@@ -96,10 +96,9 @@ public class IncrementsPluginTools {
/**
/**
* 是否启用了
* 是否启用了
*
* @return
* @return
*/
*/
public
boolean
support
(){
public
boolean
support
()
{
return
this
.
columns
.
size
()
>
0
;
return
this
.
columns
.
size
()
>
0
;
}
}
...
@@ -114,13 +113,12 @@ public class IncrementsPluginTools {
...
@@ -114,13 +113,12 @@ public class IncrementsPluginTools {
/**
/**
* 判断是否为需要进行增量操作的column
* 判断是否为需要进行增量操作的column
*
* @param searchColumn
* @param searchColumn
* @return
* @return
*/
*/
public
boolean
supportColumn
(
IntrospectedColumn
searchColumn
){
public
boolean
supportColumn
(
IntrospectedColumn
searchColumn
)
{
for
(
IntrospectedColumn
column
:
this
.
columns
)
{
for
(
IntrospectedColumn
column
:
this
.
columns
)
{
if
(
column
.
getActualColumnName
().
equals
(
searchColumn
.
getActualColumnName
())){
if
(
column
.
getActualColumnName
().
equals
(
searchColumn
.
getActualColumnName
()))
{
return
true
;
return
true
;
}
}
}
}
...
@@ -129,7 +127,6 @@ public class IncrementsPluginTools {
...
@@ -129,7 +127,6 @@ public class IncrementsPluginTools {
/**
/**
* 生成增量操作节点
* 生成增量操作节点
*
* @param introspectedColumn
* @param introspectedColumn
* @param hasPrefix
* @param hasPrefix
* @param hasComma
* @param hasComma
...
@@ -146,18 +143,23 @@ public class IncrementsPluginTools {
...
@@ -146,18 +143,23 @@ public class IncrementsPluginTools {
// 没有启用增量操作
// 没有启用增量操作
XmlElement
when
=
new
XmlElement
(
"when"
);
XmlElement
when
=
new
XmlElement
(
"when"
);
when
.
addAttribute
(
new
Attribute
(
"test"
,
(
hasPrefix
?
"record"
:
"_parameter"
)
+
".incs.isEmpty()"
));
when
.
addAttribute
(
new
Attribute
(
TextElement
normal
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
"test"
,
when
.
addElement
(
normal
);
(
hasPrefix
?
"record."
:
"_parameter."
)
+
IncrementsPlugin
.
METHOD_INC_CHECK
+
"('"
+
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
"')"
));
TextElement
spec
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" ${"
+
(
hasPrefix
?
"record."
:
""
)
+
IncrementsPlugin
.
FIELD_INC_MAP
+
"."
+
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
".value} "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
when
.
addElement
(
spec
);
choose
.
addElement
(
when
);
choose
.
addElement
(
when
);
// 启用了增量操作
// 启用了增量操作
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
TextElement
spec
=
new
TextElement
(
TextElement
normal
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
otherwise
.
addElement
(
normal
);
+
" ${"
+
(
hasPrefix
?
"record"
:
"_parameter"
)
+
".incs."
+
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
".value} "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
otherwise
.
addElement
(
spec
);
choose
.
addElement
(
otherwise
);
choose
.
addElement
(
otherwise
);
list
.
add
(
choose
);
list
.
add
(
choose
);
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
View file @
706fad7e
...
@@ -210,7 +210,7 @@ public class BatchInsertPluginTest {
...
@@ -210,7 +210,7 @@ public class BatchInsertPluginTest {
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbBlobsMapper
.
getObject
(),
"batchInsertSelective"
,
params
,
columns
);
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbBlobsMapper
.
getObject
(),
"batchInsertSelective"
,
params
,
columns
);
Assert
.
assertEquals
(
sql
,
"insert into tb_blobs ( field2 ) values ( 'null' ) , ( 'test123' )"
);
Assert
.
assertEquals
(
sql
,
"insert into tb_blobs ( field2 ) values ( 'null' ) , ( 'test123' )"
);
// 2. 执行sql
// 2. 执行sql
Object
count
=
tbBlobsMapper
.
invoke
VarArgs
(
"batchInsertSelective"
,
params
,
columns
);
Object
count
=
tbBlobsMapper
.
invoke
(
"batchInsertSelective"
,
params
,
columns
);
Assert
.
assertEquals
(
count
,
2
);
Assert
.
assertEquals
(
count
,
2
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
0 → 100644
View file @
706fad7e
This diff is collapsed.
Click to expand it.
src/test/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePluginTest.java
View file @
706fad7e
...
@@ -79,7 +79,7 @@ public class SelectSelectivePluginTest {
...
@@ -79,7 +79,7 @@ public class SelectSelectivePluginTest {
// 2. 执行sql
// 2. 执行sql
List
list
=
(
List
)
tbMapper
.
invoke
VarArgs
(
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
List
list
=
(
List
)
tbMapper
.
invoke
(
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
Assert
.
assertEquals
(
list
.
size
(),
3
);
Assert
.
assertEquals
(
list
.
size
(),
3
);
int
index
=
0
;
int
index
=
0
;
for
(
Object
obj
:
list
)
{
for
(
Object
obj
:
list
)
{
...
@@ -143,7 +143,7 @@ public class SelectSelectivePluginTest {
...
@@ -143,7 +143,7 @@ public class SelectSelectivePluginTest {
Assert
.
assertEquals
(
sql
,
"select field2 from tb_keys where key1 = 1 and key2 = '2'"
);
Assert
.
assertEquals
(
sql
,
"select field2 from tb_keys where key1 = 1 and key2 = '2'"
);
// 3. 执行sql
// 3. 执行sql
Object
tbKeys
=
tbKeysMapper
.
invoke
VarArgs
(
"selectByPrimaryKeySelective"
,
tbKeysKey
.
getObject
(),
columns1
);
Object
tbKeys
=
tbKeysMapper
.
invoke
(
"selectByPrimaryKeySelective"
,
tbKeysKey
.
getObject
(),
columns1
);
Assert
.
assertEquals
(
new
ObjectUtil
(
tbKeys
).
get
(
"field1"
),
"fd1"
);
Assert
.
assertEquals
(
new
ObjectUtil
(
tbKeys
).
get
(
"field1"
),
"fd1"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
@@ -195,7 +195,7 @@ public class SelectSelectivePluginTest {
...
@@ -195,7 +195,7 @@ public class SelectSelectivePluginTest {
Assert
.
assertEquals
(
sql
,
"select field1 from tb WHERE ( id = '3' ) order by field2 asc limit 1"
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb WHERE ( id = '3' ) order by field2 asc limit 1"
);
// 2. 执行sql
// 2. 执行sql
Object
result
=
tbMapper
.
invoke
VarArgs
(
"selectOneByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
Object
result
=
tbMapper
.
invoke
(
"selectOneByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
ObjectUtil
tb
=
new
ObjectUtil
(
result
);
ObjectUtil
tb
=
new
ObjectUtil
(
result
);
Assert
.
assertEquals
(
tb
.
get
(
"field1"
),
"fd3"
);
Assert
.
assertEquals
(
tb
.
get
(
"field1"
),
"fd3"
);
Assert
.
assertNull
(
tb
.
get
(
"field2"
));
Assert
.
assertNull
(
tb
.
get
(
"field2"
));
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
View file @
706fad7e
...
@@ -16,9 +16,12 @@
...
@@ -16,9 +16,12 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
/**
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
...
@@ -107,7 +110,7 @@ public class ObjectUtil {
...
@@ -107,7 +110,7 @@ public class ObjectUtil {
}
}
/**
/**
* 执行方法
* 执行方法
(mapper动态代理后VarArgs检查有问题)
* @param methodName
* @param methodName
* @param args
* @param args
* @return
* @return
...
@@ -116,12 +119,15 @@ public class ObjectUtil {
...
@@ -116,12 +119,15 @@ public class ObjectUtil {
* @throws IllegalAccessException
* @throws IllegalAccessException
*/
*/
public
Object
invoke
(
String
methodName
,
Object
...
args
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
public
Object
invoke
(
String
methodName
,
Object
...
args
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
Method
[]
methods
=
this
.
cls
.
getDeclaredMethods
(
);
List
<
Method
>
methods
=
getMethods
(
methodName
);
for
(
Method
method
:
methods
)
{
for
(
Method
method
:
methods
)
{
if
(
method
.
get
Name
().
equals
(
methodName
)
&&
method
.
get
ParameterTypes
().
length
==
args
.
length
)
{
if
(
method
.
getParameterTypes
().
length
==
args
.
length
)
{
boolean
flag
=
true
;
boolean
flag
=
true
;
Class
[]
parameterTypes
=
method
.
getParameterTypes
();
Class
[]
parameterTypes
=
method
.
getParameterTypes
();
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
// !! mapper动态代理后VarArgs检查有问题
// 暂时只检查前几位相同就假设为可变参数
int
check
=
args
.
length
>
0
?
(
args
[
args
.
length
-
1
]
instanceof
Array
?
args
.
length
-
1
:
args
.
length
)
:
0
;
for
(
int
i
=
0
;
i
<
check
;
i
++)
{
if
(!(
parameterTypes
[
i
].
isAssignableFrom
(
args
[
i
].
getClass
())))
{
if
(!(
parameterTypes
[
i
].
isAssignableFrom
(
args
[
i
].
getClass
())))
{
flag
=
false
;
flag
=
false
;
}
}
...
@@ -132,26 +138,26 @@ public class ObjectUtil {
...
@@ -132,26 +138,26 @@ public class ObjectUtil {
}
}
}
}
}
}
return
null
;
throw
new
NoSuchMethodError
(
"没有找到方法:"
+
methodName
)
;
}
}
/**
/**
*
执行方法(mapper动态代理后VarArgs检查有问题)
*
获取指定名称的方法
*
@param methodName
*
* @param
args
* @param
name
* @return
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
*/
public
Object
invokeVarArgs
(
String
methodName
,
Object
...
args
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
public
List
<
Method
>
getMethods
(
String
name
){
Method
[]
methods
=
this
.
cls
.
getDeclaredMethods
();
List
<
Method
>
list
=
new
ArrayList
<>();
for
(
Method
method
:
methods
)
{
Class
clazz
=
this
.
cls
;
if
(
method
.
getName
().
equals
(
methodName
))
{
for
(;
clazz
!=
Object
.
class
;
clazz
=
clazz
.
getSuperclass
()){
return
method
.
invoke
(
this
.
object
,
args
);
for
(
Method
method
:
clazz
.
getDeclaredMethods
()){
if
(
method
.
getName
().
equals
(
name
)){
list
.
add
(
method
);
}
}
}
}
return
null
;
}
return
list
;
}
}
/**
/**
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
View file @
706fad7e
...
@@ -127,13 +127,11 @@ public class SqlHelper {
...
@@ -127,13 +127,11 @@ public class SqlHelper {
}
}
}
}
if
(
args
!=
null
&&
args
.
length
==
1
)
{
if
(
args
!=
null
&&
args
.
length
==
1
)
{
Object
_params
=
wrapCollection
(
args
[
0
]);
return
getNamespaceSql
(
session
,
fullMapperMethodName
,
args
[
0
]);
if
(
_params
instanceof
Map
)
{
}
else
{
params
.
putAll
((
Map
)
_params
);
}
}
return
getNamespaceSql
(
session
,
fullMapperMethodName
,
params
);
return
getNamespaceSql
(
session
,
fullMapperMethodName
,
params
);
}
}
}
/**
/**
...
...
src/test/resources/scripts/IncrementsPlugin/init.sql
0 → 100644
View file @
706fad7e
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-07-05 17:21:41
*/
SET
FOREIGN_KEY_CHECKS
=
0
;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb`
;
CREATE
TABLE
`tb`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb
-- ----------------------------
INSERT
INTO
`tb`
VALUES
(
'1'
,
'fd1'
,
'0'
,
'0'
,
'0'
);
INSERT
INTO
`tb`
VALUES
(
'2'
,
'fd2'
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb`
VALUES
(
'3'
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb`
VALUES
(
'4'
,
'fd3'
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_blobs
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_blobs`
;
CREATE
TABLE
`tb_blobs`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
,
`field2`
longtext
COMMENT
'注释2'
,
`field3`
longtext
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_blobs
-- ----------------------------
INSERT
INTO
`tb_blobs`
VALUES
(
'1'
,
'fd1'
,
null
,
null
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'2'
,
null
,
'fd2'
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'3'
,
null
,
null
,
'fd3'
,
'1'
,
'1'
,
'1'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'4'
,
'fd4'
,
'fd5'
,
'fd6'
,
'0'
,
'0'
,
'0'
);
-- ----------------------------
-- Table structure for tb_keys
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_keys`
;
CREATE
TABLE
`tb_keys`
(
`key1`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`key2`
varchar
(
255
)
NOT
NULL
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`key1`
,
`key2`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_keys
-- ----------------------------
INSERT
INTO
`tb_keys`
VALUES
(
'1'
,
'k1'
,
'fd1'
,
null
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_keys`
VALUES
(
'2'
,
'k2'
,
null
,
'2'
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_keys`
VALUES
(
'3'
,
'k3'
,
null
,
null
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_single_blob
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_single_blob`
;
CREATE
TABLE
`tb_single_blob`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
longtext
COMMENT
'注释2'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
4
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_single_blob
-- ----------------------------
INSERT
INTO
`tb_single_blob`
VALUES
(
'1'
,
'fd1'
,
'0'
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_single_blob`
VALUES
(
'2'
,
null
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_single_blob`
VALUES
(
'3'
,
null
,
null
,
'1'
,
'1'
,
'1'
);
src/test/resources/scripts/IncrementsPlugin/mybatis-generator-without-model-builder-plugin.xml
0 → 100644
View file @
706fad7e
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties
resource=
"db.properties"
/>
<!--导入属性配置 -->
<context
id=
"default"
targetRuntime=
"MyBatis3"
>
<!-- 插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.IncrementsPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
""
targetProject=
""
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
""
targetProject=
""
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/test/resources/scripts/IncrementsPlugin/mybatis-generator.xml
0 → 100644
View file @
706fad7e
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties
resource=
"db.properties"
/>
<!--导入属性配置 -->
<context
id=
"default"
targetRuntime=
"MyBatis3"
>
<!-- 插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.IncrementsPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
""
targetProject=
""
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
""
targetProject=
""
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
<property
name=
"incrementsColumns"
value=
"inc_f1"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_keys"
>
<property
name=
"incrementsColumns"
value=
" inc_f1, inc_f2, inc_f3 "
/>
</table>
<table
tableName=
"tb_blobs"
>
<property
name=
"incrementsColumns"
value=
"inc_f1,inc_f3"
/>
</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