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
df27bb11
Commit
df27bb11
authored
Jul 28, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
测试用例完善:logicalDeletePlugin
parent
d0afad6f
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
119 additions
and
57 deletions
+119
-57
src/test/java/com/itfsw/mybatis/generator/plugins/DBHelperTest.java
...ava/com/itfsw/mybatis/generator/plugins/DBHelperTest.java
+1
-5
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
...itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
+3
-10
src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java
...sw/mybatis/generator/plugins/LogicalDeletePluginTest.java
+61
-1
src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
...a/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
+16
-4
src/test/java/com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
...com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
+22
-14
src/test/java/com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
.../com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
+15
-17
src/test/resources/scripts/LogicalDeletePlugin/mybatis-generator.xml
...sources/scripts/LogicalDeletePlugin/mybatis-generator.xml
+1
-6
No files found.
src/test/java/com/itfsw/mybatis/generator/plugins/DBHelperTest.java
View file @
df27bb11
...
...
@@ -50,16 +50,12 @@ public class DBHelperTest {
Class
.
forName
(
driver
);
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
Statement
statement
=
connection
.
createStatement
();
// 执行查询
statement
.
execute
(
"SELECT COUNT(*) as total FROM tb"
);
ResultSet
resultSet
=
statement
.
getResultSet
();
ResultSet
resultSet
=
DBHelper
.
execute
(
connection
,
"SELECT COUNT(*) as total FROM tb"
);
resultSet
.
first
();
Assert
.
assertEquals
(
resultSet
.
getInt
(
"total"
),
4
);
statement
.
close
();
connection
.
close
();
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
View file @
df27bb11
...
...
@@ -23,7 +23,6 @@ import org.junit.BeforeClass;
import
org.junit.Test
;
import
java.lang.reflect.Method
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.util.List
;
...
...
@@ -118,9 +117,7 @@ public class IncrementsPluginTest {
// inc_f1 增加100
Object
result
=
tbMapper
.
invoke
(
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
PreparedStatement
preparedStatement
=
sqlSession
.
getConnection
().
prepareStatement
(
"select inc_f1 from tb where id = 3"
);
preparedStatement
.
execute
();
ResultSet
rs
=
preparedStatement
.
getResultSet
();
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 3"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
103
);
...
...
@@ -130,9 +127,7 @@ public class IncrementsPluginTest {
result
=
tbMapper
.
invoke
(
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
// 验证执行结果
preparedStatement
=
sqlSession
.
getConnection
().
prepareStatement
(
"select inc_f1 from tb where id = 3"
);
preparedStatement
.
execute
();
rs
=
preparedStatement
.
getResultSet
();
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 3"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
53
);
...
...
@@ -157,9 +152,7 @@ public class IncrementsPluginTest {
result
=
tbKeysMapper
.
invoke
(
"updateByPrimaryKeySelective"
,
tbKeysBuilder
.
invoke
(
"build"
));
Assert
.
assertEquals
(
result
,
1
);
// 验证执行结果
preparedStatement
=
sqlSession
.
getConnection
().
prepareStatement
(
"select inc_f1, inc_f3 from tb_keys where key1 = 1 and key2 = 'k1'"
);
preparedStatement
.
execute
();
rs
=
preparedStatement
.
getResultSet
();
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1, inc_f3 from tb_keys where key1 = 1 and key2 = 'k1'"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
11
);
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f3"
),
33
);
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java
View file @
df27bb11
...
...
@@ -25,7 +25,9 @@ import org.mybatis.generator.exception.InvalidConfigurationException;
import
org.mybatis.generator.exception.XMLParserException
;
import
java.io.IOException
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
...
...
@@ -83,8 +85,66 @@ public class LogicalDeletePluginTest {
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
1
l
);
// 验证sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"logicalDeleteByExample"
,
tbExample
.
getObject
());
tbMapper
.
invoke
(
"logicalDeleteByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb set del_flag = 1 WHERE ( id = '1' )"
);
// 验证执行
Object
result
=
tbMapper
.
invoke
(
"logicalDeleteByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select del_flag from tb where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"del_flag"
),
1
);
}
});
}
/**
* 测试 logicalDeleteByPrimaryKey
*/
@Test
public
void
testLogicalDeleteByPrimaryKey
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/LogicalDeletePlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
// 验证sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"logicalDeleteByPrimaryKey"
,
2
l
);
Assert
.
assertEquals
(
sql
,
"update tb set del_flag = 1 where id = 2"
);
// 验证执行
Object
result
=
tbMapper
.
invoke
(
"logicalDeleteByPrimaryKey"
,
2
l
);
Assert
.
assertEquals
(
result
,
1
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select del_flag from tb where id = 2"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"del_flag"
),
1
);
}
});
}
/**
* 测试关联生成的方法和常量
*/
@Test
public
void
testOtherMethods
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/LogicalDeletePlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
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
(
"andDeleted"
,
true
);
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
// 验证sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"select id, del_flag, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '1' and id = '3' )"
);
// 验证执行
Object
result
=
tbMapper
.
invoke
(
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(((
List
)
result
).
size
(),
1
);
}
});
}
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
View file @
df27bb11
...
...
@@ -22,10 +22,7 @@ import java.io.BufferedReader;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.*
;
import
java.util.Properties
;
/**
...
...
@@ -90,4 +87,19 @@ public class DBHelper {
connection
.
close
();
}
/**
* 执行sql
*
* @param connection
* @param sql
* @return
* @throws SQLException
*/
public
static
ResultSet
execute
(
Connection
connection
,
String
sql
)
throws
SQLException
{
Statement
statement
=
connection
.
createStatement
();
statement
.
execute
(
sql
);
ResultSet
resultSet
=
statement
.
getResultSet
();
return
resultSet
;
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java
View file @
df27bb11
...
...
@@ -41,17 +41,17 @@ public class ObjectUtil {
* @param cls
*/
public
ObjectUtil
(
ClassLoader
loader
,
String
cls
)
throws
ClassNotFoundException
,
IllegalAccessException
,
InstantiationException
{
if
(
cls
.
indexOf
(
"#"
)
==
-
1
){
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
()){
if
(
this
.
cls
.
isEnum
())
{
Object
[]
constants
=
this
.
cls
.
getEnumConstants
();
for
(
Object
object
:
constants
){
for
(
Object
object
:
constants
)
{
ObjectUtil
eObject
=
new
ObjectUtil
(
object
);
if
(
strs
[
1
].
equals
(
eObject
.
get
(
"name"
))){
if
(
strs
[
1
].
equals
(
eObject
.
get
(
"name"
)))
{
this
.
object
=
object
;
break
;
}
...
...
@@ -89,7 +89,6 @@ public class ObjectUtil {
/**
* 获取值
*
* @param filedName
* @return
* @throws IllegalAccessException
...
...
@@ -128,10 +127,21 @@ public class ObjectUtil {
// 暂时只检查前几位相同就假设为可变参数
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
())))
{
Class
parameterType
=
parameterTypes
[
i
];
if
(!(
parameterType
.
isAssignableFrom
(
args
[
i
].
getClass
())))
{
flag
=
false
;
}
// 基础类型
if
(
parameterType
.
isPrimitive
())
{
switch
(
parameterType
.
getTypeName
())
{
case
"boolean"
:
flag
=
args
[
i
]
instanceof
Boolean
;
break
;
default
:
flag
=
false
;
}
}
}
if
(
flag
)
{
return
method
.
invoke
(
this
.
object
,
args
);
...
...
@@ -143,16 +153,15 @@ public class ObjectUtil {
/**
* 获取指定名称的方法
*
* @param name
* @return
*/
public
List
<
Method
>
getMethods
(
String
name
){
public
List
<
Method
>
getMethods
(
String
name
)
{
List
<
Method
>
list
=
new
ArrayList
<>();
Class
clazz
=
this
.
cls
;
for
(;
clazz
!=
Object
.
class
;
clazz
=
clazz
.
getSuperclass
()){
for
(
Method
method
:
clazz
.
getDeclaredMethods
()){
if
(
method
.
getName
().
equals
(
name
)){
for
(;
clazz
!=
Object
.
class
;
clazz
=
clazz
.
getSuperclass
())
{
for
(
Method
method
:
clazz
.
getDeclaredMethods
())
{
if
(
method
.
getName
().
equals
(
name
))
{
list
.
add
(
method
);
}
}
...
...
@@ -171,16 +180,15 @@ public class ObjectUtil {
/**
* 递归获取所有属性
*
* @param name
* @return
*/
private
Field
getDeclaredField
(
String
name
)
{
Class
<?>
clazz
=
this
.
cls
;
for
(;
clazz
!=
Object
.
class
;
clazz
=
clazz
.
getSuperclass
())
{
for
(;
clazz
!=
Object
.
class
;
clazz
=
clazz
.
getSuperclass
())
{
try
{
return
clazz
.
getDeclaredField
(
name
);
}
catch
(
NoSuchFieldException
e
){
}
catch
(
NoSuchFieldException
e
)
{
// 不能操作,递归父类
}
}
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/tools/SqlHelper.java
View file @
df27bb11
...
...
@@ -126,11 +126,10 @@ public class SqlHelper {
params
.
put
(
paramName
,
i
>=
args
.
length
?
null
:
args
[
i
]);
}
}
if
(
args
!=
null
&&
args
.
length
==
1
)
{
if
(
useParameter
(
method
,
args
)
)
{
return
getNamespaceSql
(
session
,
fullMapperMethodName
,
args
[
0
]);
}
else
{
return
getNamespaceSql
(
session
,
fullMapperMethodName
,
params
);
}
return
getNamespaceSql
(
session
,
fullMapperMethodName
,
params
);
}
...
...
@@ -152,7 +151,6 @@ public class SqlHelper {
* @return
*/
public
static
String
getNamespaceSql
(
SqlSession
session
,
String
namespace
,
Object
params
)
{
params
=
wrapCollection
(
params
);
Configuration
configuration
=
session
.
getConfiguration
();
MappedStatement
mappedStatement
=
configuration
.
getMappedStatement
(
namespace
);
TypeHandlerRegistry
typeHandlerRegistry
=
mappedStatement
.
getConfiguration
().
getTypeHandlerRegistry
();
...
...
@@ -266,21 +264,21 @@ public class SqlHelper {
}
/**
* 简单包装参数
* @param object
* 只有一个参数且没有使用注解
* @param method
* @param args
* @return
*/
private
static
Object
wrapCollection
(
final
Object
object
)
{
if
(
object
instanceof
List
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"list"
,
object
);
return
map
;
}
else
if
(
object
!=
null
&&
object
.
getClass
().
isArray
())
{
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"array"
,
object
);
return
map
;
private
static
boolean
useParameter
(
Method
method
,
Object
...
args
){
if
(
args
!=
null
&&
args
.
length
==
1
){
final
Object
[]
paramAnnos
=
method
.
getParameterAnnotations
()[
0
];
for
(
Object
paramAnno
:
paramAnnos
)
{
if
(
paramAnno
instanceof
Param
)
{
return
false
;
}
return
object
;
}
return
true
;
}
return
false
;
}
}
src/test/resources/scripts/LogicalDeletePlugin/mybatis-generator.xml
View file @
df27bb11
...
...
@@ -34,12 +34,7 @@
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<javaModelGenerator
targetPackage=
""
targetProject=
""
/>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
""
targetProject=
""
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
...
...
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