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
91f9537b
Commit
91f9537b
authored
Jun 28, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BatchInsertPlugin 测试用例
parent
5e3e9880
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
273 additions
and
17 deletions
+273
-17
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
...tfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
+73
-14
src/test/java/com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
...com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
+185
-0
src/test/java/com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
.../com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
+12
-0
src/test/resources/scripts/BatchInsertPlugin/init.sql
src/test/resources/scripts/BatchInsertPlugin/init.sql
+3
-3
No files found.
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
View file @
91f9537b
...
...
@@ -16,10 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
com.itfsw.mybatis.generator.plugins.tools.SqlHelper
;
import
com.itfsw.mybatis.generator.plugins.tools.Util
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.AfterClass
;
...
...
@@ -34,6 +31,7 @@ import org.mybatis.generator.exception.XMLParserException;
import
org.mybatis.generator.internal.DefaultShellCallback
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Method
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
...
...
@@ -168,7 +166,7 @@ public class BatchInsertPluginTest {
* 测试生成的sql
*/
@Test
public
void
test
Sql
()
throws
Exception
{
public
void
test
BatchInsert
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
...
...
@@ -177,23 +175,84 @@ public class BatchInsertPluginTest {
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
try
{
SqlSession
sqlSession
=
helper
.
getSqlSession
();
Object
obj
=
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
));
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
String
sql
=
SqlHelper
.
getMapperSql
(
sqlSession
,
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
),
"batchInsert"
,
params
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.Tb"
)
.
set
(
"field1"
,
"test"
)
.
getObject
()
);
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.Tb"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field2"
,
1
)
.
getObject
()
);
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"batchInsert"
,
params
);
Assert
.
assertEquals
(
sql
,
"insert into tb (field1, field2) values ('test', null) , ('test', 1)"
);
// 2. 执行sql
Object
count
=
tbMapper
.
invoke
(
"batchInsert"
,
params
);
Assert
.
assertEquals
(
count
,
2
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
System
.
out
.
println
(
sql
);
/**
* 测试生成的sql
*/
@Test
public
void
testBatchInsertSelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
try
{
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbBlobsMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbBlobsMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs"
)
.
set
(
"field1"
,
"test"
)
.
getObject
()
);
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field2"
,
"test123"
)
.
getObject
()
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs$Column#field2"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Array
.
set
(
columns
,
0
,
columnField2
.
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbBlobsMapper
.
getObject
(),
"batchInsertSelective"
,
params
,
columns
);
Assert
.
assertEquals
(
sql
,
"insert into tb_blobs ( field2 ) values ( 'null' ) , ( 'test123' )"
);
// 2. 执行sql
Object
count
=
tbBlobsMapper
.
invokeVarArgs
(
"batchInsertSelective"
,
params
,
columns
);
Assert
.
assertEquals
(
count
,
2
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
},
warnings
);
...
...
@@ -202,6 +261,6 @@ public class BatchInsertPluginTest {
@AfterClass
public
static
void
clean
()
{
//
DBHelper.reset();
DBHelper
.
reset
();
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
0 → 100644
View file @
91f9537b
/*
* 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
.
tools
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Parameter
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/28 14:26
* ---------------------------------------------------------------------------
*/
public
class
ObjectUtil
{
private
Object
object
;
// 对象
private
Class
cls
;
// 类
/**
* 构造函数(枚举#分隔)
* @param loader
* @param cls
*/
public
ObjectUtil
(
ClassLoader
loader
,
String
cls
)
throws
ClassNotFoundException
,
IllegalAccessException
,
InstantiationException
{
if
(
cls
.
indexOf
(
"#"
)
==
-
1
){
this
.
cls
=
loader
.
loadClass
(
cls
);
this
.
object
=
this
.
cls
.
newInstance
();
}
else
{
String
[]
strs
=
cls
.
split
(
"#"
);
this
.
cls
=
loader
.
loadClass
(
strs
[
0
]);
if
(
this
.
cls
.
isEnum
()){
Object
[]
constants
=
this
.
cls
.
getEnumConstants
();
for
(
Object
object
:
constants
){
ObjectUtil
eObject
=
new
ObjectUtil
(
object
);
if
(
strs
[
1
].
equals
(
eObject
.
get
(
"name"
))){
this
.
object
=
object
;
break
;
}
}
System
.
out
.
println
(
""
);
}
else
{
throw
new
ClassNotFoundException
(
"没有找到对应枚举"
+
strs
[
0
]);
}
}
}
/**
* 构造函数
* @param object
*/
public
ObjectUtil
(
Object
object
)
{
this
.
object
=
object
;
this
.
cls
=
object
.
getClass
();
}
/**
* 设置值
* @param filedName
* @param value
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public
ObjectUtil
set
(
String
filedName
,
Object
value
)
throws
NoSuchFieldException
,
IllegalAccessException
{
Field
field
=
this
.
getDeclaredField
(
filedName
);
field
.
setAccessible
(
true
);
field
.
set
(
this
.
object
,
value
);
return
this
;
}
/**
* 获取值
*
* @param filedName
* @return
* @throws IllegalAccessException
*/
public
Object
get
(
String
filedName
)
throws
IllegalAccessException
{
Field
field
=
this
.
getDeclaredField
(
filedName
);
field
.
setAccessible
(
true
);
return
field
.
get
(
this
.
object
);
}
/**
* Getter method for property <tt>object</tt>.
* @return property value of object
* @author hewei
*/
public
Object
getObject
()
{
return
object
;
}
/**
* 执行方法
* @param methodName
* @param args
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public
Object
invoke
(
String
methodName
,
Object
...
args
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
Method
[]
methods
=
this
.
cls
.
getDeclaredMethods
();
for
(
Method
method
:
methods
)
{
if
(
method
.
getName
().
equals
(
methodName
)
&&
method
.
getParameterTypes
().
length
==
args
.
length
)
{
boolean
flag
=
true
;
Class
[]
parameterTypes
=
method
.
getParameterTypes
();
Parameter
[]
pps
=
method
.
getParameters
();
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
if
(!(
parameterTypes
[
i
].
isAssignableFrom
(
args
[
i
].
getClass
())))
{
flag
=
false
;
}
}
if
(
flag
)
{
return
method
.
invoke
(
this
.
object
,
args
);
}
}
}
return
null
;
}
/**
* 执行方法(mapper动态代理后VarArgs检查有问题)
* @param methodName
* @param args
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public
Object
invokeVarArgs
(
String
methodName
,
Object
...
args
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
Method
[]
methods
=
this
.
cls
.
getDeclaredMethods
();
for
(
Method
method
:
methods
)
{
if
(
method
.
getName
().
equals
(
methodName
))
{
return
method
.
invoke
(
this
.
object
,
args
);
}
}
return
null
;
}
/**
* Getter method for property <tt>cls</tt>.
* @return property value of cls
* @author hewei
*/
public
Class
getCls
()
{
return
cls
;
}
/**
* 递归获取所有属性
*
* @param name
* @return
*/
private
Field
getDeclaredField
(
String
name
)
{
Class
<?>
clazz
=
this
.
cls
;
for
(;
clazz
!=
Object
.
class
;
clazz
=
clazz
.
getSuperclass
())
{
try
{
return
clazz
.
getDeclaredField
(
name
);
}
catch
(
NoSuchFieldException
e
){
// 不能操作,递归父类
}
}
return
null
;
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
View file @
91f9537b
...
...
@@ -51,6 +51,18 @@ public class SqlHelper {
private
static
final
ObjectFactory
DEFAULT_OBJECT_FACTORY
=
new
DefaultObjectFactory
();
private
static
final
ObjectWrapperFactory
DEFAULT_OBJECT_WRAPPER_FACTORY
=
new
DefaultObjectWrapperFactory
();
/**
* 通过Mapper接口和方法名(过滤掉回车多余空格信息,便于比对)
* @param mapper
* @param methodName
* @param args
* @return
*/
public
static
String
getFormatMapperSql
(
Object
mapper
,
String
methodName
,
Object
...
args
)
{
String
sql
=
getMapperSql
(
mapper
,
methodName
,
args
);
return
sql
==
null
?
null
:
sql
.
replaceAll
(
"\n\\s*"
,
" "
);
}
/**
* 通过接口获取sql
* @param mapper
...
...
src/test/resources/scripts/BatchInsertPlugin/init.sql
View file @
91f9537b
...
...
@@ -22,7 +22,7 @@ DROP TABLE IF EXISTS `tb`;
CREATE
TABLE
`tb`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`field2`
floa
t
DEFAULT
NULL
,
`field2`
in
t
DEFAULT
NULL
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
...
...
@@ -54,7 +54,7 @@ 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`
floa
t
DEFAULT
NULL
,
`field2`
in
t
DEFAULT
NULL
,
PRIMARY
KEY
(
`key1`
,
`key2`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
...
...
@@ -69,7 +69,7 @@ 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`
floa
t
DEFAULT
NULL
,
`field2`
in
t
DEFAULT
NULL
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
...
...
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