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
f1e583c9
Commit
f1e583c9
authored
Jul 07, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ExampleEnhancedPlugin 测试用例
parent
9564a085
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
254 additions
and
0 deletions
+254
-0
src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java
.../mybatis/generator/plugins/ExampleEnhancedPluginTest.java
+162
-0
src/test/resources/scripts/ExampleEnhancedPlugin/init.sql
src/test/resources/scripts/ExampleEnhancedPlugin/init.sql
+40
-0
src/test/resources/scripts/ExampleEnhancedPlugin/mybatis-generator.xml
...urces/scripts/ExampleEnhancedPlugin/mybatis-generator.xml
+52
-0
No files found.
src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java
0 → 100644
View file @
f1e583c9
/*
* 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
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.mybatis.generator.exception.InvalidConfigurationException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.InvocationHandler
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Proxy
;
import
java.sql.SQLException
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/7/7 15:38
* ---------------------------------------------------------------------------
*/
public
class
ExampleEnhancedPluginTest
{
/**
* 初始化数据库
*/
@BeforeClass
public
static
void
init
()
throws
SQLException
,
IOException
,
ClassNotFoundException
{
DBHelper
.
createDB
(
"scripts/ExampleEnhancedPlugin/init.sql"
);
}
/**
* 测试生成的example方法
*/
@Test
public
void
testExample
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/ExampleEnhancedPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
tbExampleCriteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
// 调用example方法能正常返回
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExample"
,
tbExampleCriteria
.
invoke
(
"example"
));
Assert
.
assertEquals
(
sql
,
"select id, field1 from tb"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
}
});
}
/**
* 测试生成的orderBy方法
*/
@Test
public
void
testOrderBy
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/ExampleEnhancedPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
Object
order
=
Array
.
newInstance
(
String
.
class
,
2
);
Array
.
set
(
order
,
0
,
"id desc"
);
Array
.
set
(
order
,
1
,
"field1 asc"
);
tbExample
.
invoke
(
"orderBy"
,
order
);
// 可变参数方法直接设置order by
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"select id, field1 from tb order by id desc , field1 asc"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
}
});
}
/**
* 测试andIf方法
*/
@Test
public
void
testAndIf
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/ExampleEnhancedPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
// 1. andIf true
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
tbExampleCriteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
// 代理实现接口
Object
criteriaAdd
=
Proxy
.
newProxyInstance
(
loader
,
new
Class
[]{
loader
.
loadClass
(
packagz
+
".TbExample$Criteria$ICriteriaAdd"
)
},
new
TestInvocationHandler
());
Method
method
=
tbExampleCriteria
.
getMethods
(
"andIf"
).
get
(
0
);
method
.
invoke
(
tbExampleCriteria
.
getObject
(),
true
,
criteriaAdd
);
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"select id, field1 from tb WHERE ( id = '5' )"
);
// 2. andIf false
ObjectUtil
tbExample1
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
tbExampleCriteria1
=
new
ObjectUtil
(
tbExample1
.
invoke
(
"createCriteria"
));
method
=
tbExampleCriteria1
.
getMethods
(
"andIf"
).
get
(
0
);
method
.
invoke
(
tbExampleCriteria1
.
getObject
(),
false
,
criteriaAdd
);
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExample"
,
tbExample1
.
getObject
());
Assert
.
assertEquals
(
sql
,
"select id, field1 from tb"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
}
});
}
/**
* 代理实现
*/
private
class
TestInvocationHandler
implements
InvocationHandler
{
@Override
public
Object
invoke
(
Object
proxy
,
Method
method
,
Object
[]
args
)
throws
Throwable
{
if
(
method
.
getName
().
equals
(
"add"
)){
ObjectUtil
tbExampleCriteria
=
new
ObjectUtil
(
args
[
0
]);
tbExampleCriteria
.
invoke
(
"andIdEqualTo"
,
5
l
);
return
tbExampleCriteria
.
getObject
();
}
return
null
;
}
}
}
src/test/resources/scripts/ExampleEnhancedPlugin/init.sql
0 → 100644
View file @
f1e583c9
/*
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-06-26 17:30:13
*/
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'
,
PRIMARY
KEY
(
`id`
)
);
-- ----------------------------
-- Records of tb
-- ----------------------------
INSERT
INTO
`tb`
VALUES
(
'1'
,
'f1'
);
INSERT
INTO
`tb`
VALUES
(
'2'
,
'f2'
);
INSERT
INTO
`tb`
VALUES
(
'3'
,
'f3'
);
INSERT
INTO
`tb`
VALUES
(
'4'
,
'f4'
);
INSERT
INTO
`tb`
VALUES
(
'5'
,
'f5'
);
INSERT
INTO
`tb`
VALUES
(
'6'
,
'f6'
);
INSERT
INTO
`tb`
VALUES
(
'7'
,
'f7'
);
INSERT
INTO
`tb`
VALUES
(
'8'
,
'f8'
);
INSERT
INTO
`tb`
VALUES
(
'9'
,
'f9'
);
INSERT
INTO
`tb`
VALUES
(
'10'
,
'f10'
);
\ No newline at end of file
src/test/resources/scripts/ExampleEnhancedPlugin/mybatis-generator.xml
0 → 100644
View file @
f1e583c9
<?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.ExampleEnhancedPlugin"
/>
<!--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
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