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
645a961f
Commit
645a961f
authored
Jul 03, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
插件SelectSelectivePlugin测试用例
parent
e908f149
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
197 additions
and
8 deletions
+197
-8
src/test/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePluginTest.java
.../mybatis/generator/plugins/SelectSelectivePluginTest.java
+128
-4
src/test/resources/scripts/SelectSelectivePlugin/init.sql
src/test/resources/scripts/SelectSelectivePlugin/init.sql
+5
-2
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml
...lugin/mybatis-generator-with-SelectOneByExamplePlugin.xml
+64
-0
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator.xml
...urces/scripts/SelectSelectivePlugin/mybatis-generator.xml
+0
-2
No files found.
src/test/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePluginTest.java
View file @
645a961f
...
@@ -26,9 +26,11 @@ import org.junit.AfterClass;
...
@@ -26,9 +26,11 @@ import org.junit.AfterClass;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.MyBatisGenerator
;
import
org.mybatis.generator.api.MyBatisGenerator
;
import
org.mybatis.generator.config.Configuration
;
import
org.mybatis.generator.config.Configuration
;
import
org.mybatis.generator.config.xml.ConfigurationParser
;
import
org.mybatis.generator.config.xml.ConfigurationParser
;
import
org.mybatis.generator.internal.DefaultShellCallback
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Array
;
...
@@ -63,6 +65,7 @@ public class SelectSelectivePluginTest {
...
@@ -63,6 +65,7 @@ public class SelectSelectivePluginTest {
*/
*/
@Test
@Test
public
void
testSelectByExampleSelective
()
throws
Exception
{
public
void
testSelectByExampleSelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
...
@@ -78,7 +81,7 @@ public class SelectSelectivePluginTest {
...
@@ -78,7 +81,7 @@ public class SelectSelectivePluginTest {
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbExample"
);
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdLessThan"
,
100
);
criteria
.
invoke
(
"andIdLessThan"
,
100
l
);
tbExample
.
set
(
"orderByClause"
,
"field2 asc"
);
tbExample
.
set
(
"orderByClause"
,
"field2 asc"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field1"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field1"
);
...
@@ -87,7 +90,7 @@ public class SelectSelectivePluginTest {
...
@@ -87,7 +90,7 @@ public class SelectSelectivePluginTest {
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb order by field2 asc"
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb
WHERE ( id < '100' )
order by field2 asc"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field2"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field2"
);
Object
columns2
=
Array
.
newInstance
(
columnField1
.
getCls
(),
2
);
Object
columns2
=
Array
.
newInstance
(
columnField1
.
getCls
(),
2
);
...
@@ -95,7 +98,7 @@ public class SelectSelectivePluginTest {
...
@@ -95,7 +98,7 @@ public class SelectSelectivePluginTest {
Array
.
set
(
columns2
,
1
,
columnField2
.
getObject
());
Array
.
set
(
columns2
,
1
,
columnField2
.
getObject
());
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns2
);
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns2
);
Assert
.
assertEquals
(
sql
,
"select field1 , field2 from tb order by field2 asc"
);
Assert
.
assertEquals
(
sql
,
"select field1 , field2 from tb
WHERE ( id < '100' )
order by field2 asc"
);
// 2. 执行sql
// 2. 执行sql
...
@@ -129,8 +132,129 @@ public class SelectSelectivePluginTest {
...
@@ -129,8 +132,129 @@ public class SelectSelectivePluginTest {
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
}
/**
* 测试生成的方法
* @throws Exception
*/
@Test
public
void
testSelectByPrimaryKeySelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/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
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbKeys$Column#field1"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByPrimaryKeySelective"
,
1
,
columns1
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb where id = 1"
);
// 2. 测试xxxKey
ObjectUtil
tbKeysMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbKeysMapper"
)));
ObjectUtil
tbKeysKey
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbKeysKey"
);
tbKeysKey
.
set
(
"key1"
,
1
l
);
tbKeysKey
.
set
(
"key2"
,
"2"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbKeys$Column#field2"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns2
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Array
.
set
(
columns2
,
0
,
columnField2
.
getObject
());
sql
=
SqlHelper
.
getFormatMapperSql
(
tbKeysMapper
.
getObject
(),
"selectByPrimaryKeySelective"
,
tbKeysKey
.
getObject
(),
columns2
);
Assert
.
assertEquals
(
sql
,
"select field2 from tb_keys where key1 = 1 and key2 = '2'"
);
// 3. 执行sql
Object
tbKeys
=
tbKeysMapper
.
invokeVarArgs
(
"selectByPrimaryKeySelective"
,
tbKeysKey
.
getObject
(),
columns1
);
Assert
.
assertEquals
(
new
ObjectUtil
(
tbKeys
).
get
(
"field1"
),
"fd1"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
/**
* 测试生成的方法
* @throws Exception
*/
@Test
public
void
testSelectOneByExampleSelective
()
throws
Exception
{
// 没有配置SelectOneByExamplePlugin插件时不生成对应方法
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
for
(
GeneratedJavaFile
file
:
list
)
{
if
(
file
.
getFileName
().
equals
(
"TbMapper.java"
))
{
Assert
.
assertFalse
(
file
.
getFormattedContent
().
matches
(
".*selectByExampleSelective.*"
));
}
}
// 配置了SelectOneByExamplePlugin
DBHelper
.
cleanDao
();
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml"
));
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
try
{
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
tbExample
.
set
(
"orderByClause"
,
"field2 asc"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field1"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectOneByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb WHERE ( id = '3' ) order by field2 asc limit 1"
);
// 2. 执行sql
Object
result
=
tbMapper
.
invokeVarArgs
(
"selectOneByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
ObjectUtil
tb
=
new
ObjectUtil
(
result
);
Assert
.
assertEquals
(
tb
.
get
(
"field1"
),
"fd3"
);
Assert
.
assertNull
(
tb
.
get
(
"field2"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
@AfterClass
@AfterClass
public
static
void
clean
()
{
public
static
void
clean
()
{
//
DBHelper.reset();
DBHelper
.
reset
();
}
}
}
}
src/test/resources/scripts/SelectSelectivePlugin/init.sql
View file @
645a961f
...
@@ -10,7 +10,7 @@ Target Server Type : MYSQL
...
@@ -10,7 +10,7 @@ Target Server Type : MYSQL
Target Server Version : 50617
Target Server Version : 50617
File Encoding : 65001
File Encoding : 65001
Date: 2017-0
6-30 11:15:22
Date: 2017-0
7-03 17:34:11
*/
*/
SET
FOREIGN_KEY_CHECKS
=
0
;
SET
FOREIGN_KEY_CHECKS
=
0
;
...
@@ -59,11 +59,14 @@ CREATE TABLE `tb_keys` (
...
@@ -59,11 +59,14 @@ CREATE TABLE `tb_keys` (
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`field2`
int
(
11
)
DEFAULT
NULL
,
PRIMARY
KEY
(
`key1`
,
`key2`
)
PRIMARY
KEY
(
`key1`
,
`key2`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
4
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- ----------------------------
-- Records of tb_keys
-- Records of tb_keys
-- ----------------------------
-- ----------------------------
INSERT
INTO
`tb_keys`
VALUES
(
'1'
,
'2'
,
'fd1'
,
null
);
INSERT
INTO
`tb_keys`
VALUES
(
'2'
,
'3'
,
null
,
'2'
);
INSERT
INTO
`tb_keys`
VALUES
(
'3'
,
'4'
,
'fd2'
,
'3'
);
-- ----------------------------
-- ----------------------------
-- Table structure for tb_single_blob
-- Table structure for tb_single_blob
...
...
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml
0 → 100644
View file @
645a961f
<?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"
>
<!-- 插件 -->
<!-- Select Selective插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"
/>
<!-- 数据Model属性对应Column获取插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<!-- 查询单条数据插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${db.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_keys"
/>
<table
tableName=
"tb_single_blob"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_blobs"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator.xml
View file @
645a961f
...
@@ -27,8 +27,6 @@
...
@@ -27,8 +27,6 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"
/>
<!-- 数据Model属性对应Column获取插件 -->
<!-- 数据Model属性对应Column获取插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<!-- 查询单条数据插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"
/>
<!--jdbc的数据库连接 -->
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${db.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.password}"
/>
<jdbcConnection
driverClass=
"${db.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.password}"
/>
...
...
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