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
87519edc
Commit
87519edc
authored
May 28, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TableConfigurationPlugin 测试用例
parent
e0614069
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
198 additions
and
16 deletions
+198
-16
src/main/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPlugin.java
...w/mybatis/generator/plugins/TableConfigurationPlugin.java
+2
-16
src/test/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPluginTest.java
...batis/generator/plugins/TableConfigurationPluginTest.java
+84
-0
src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-clientSuffix.xml
...nfigurationPlugin/mybatis-generator-with-clientSuffix.xml
+56
-0
src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml
...figurationPlugin/mybatis-generator-with-exampleSuffix.xml
+56
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPlugin.java
View file @
87519edc
...
...
@@ -55,7 +55,6 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig
public
static
final
String
PRO_COLUMN_REPLACE_DISABLE
=
"columnRenamingRule.disable"
;
// 替换 property
public
static
final
String
PRO_CLIENT_SUFFIX
=
"clientSuffix"
;
// client 结尾
public
static
final
String
PRO_SQL_MAP_SUFFIX
=
"sqlMapSuffix"
;
// sqlMap 结尾
public
static
final
String
PRO_EXAMPLE_SUFFIX
=
"exampleSuffix"
;
// example 结尾
public
static
final
String
PRO_MODEL_SUFFIX
=
"modelSuffix"
;
// model 结尾
...
...
@@ -68,7 +67,6 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig
private
String
clientSuffix
;
// client 结尾
private
String
sqlMapSuffix
;
// sqlMap 结尾
private
String
exampleSuffix
;
// example 结尾
private
String
modelSuffix
;
// model 结尾
...
...
@@ -88,7 +86,6 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig
this
.
exampleSuffix
=
properties
.
getProperty
(
PRO_EXAMPLE_SUFFIX
);
this
.
clientSuffix
=
properties
.
getProperty
(
PRO_CLIENT_SUFFIX
);
this
.
sqlMapSuffix
=
properties
.
getProperty
(
PRO_SQL_MAP_SUFFIX
);
this
.
modelSuffix
=
properties
.
getProperty
(
PRO_MODEL_SUFFIX
);
return
super
.
validate
(
warnings
);
...
...
@@ -168,18 +165,7 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig
}
introspectedTable
.
setMyBatis3FallbackSqlMapNamespace
(
sb
.
toString
());
}
// 2. sqlMap
if
(!
stringHasValue
(
tableConfiguration
.
getMapperName
())
&&
(
this
.
sqlMapSuffix
!=
null
||
this
.
clientSuffix
!=
null
))
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectName
());
sb
.
append
(
this
.
sqlMapSuffix
!=
null
?
this
.
sqlMapSuffix
:
this
.
clientSuffix
);
sb
.
append
(
".xml"
);
introspectedTable
.
setMyBatis3XmlMapperFileName
(
sb
.
toString
());
}
// 3. example
// 2. example
if
(
this
.
exampleSuffix
!=
null
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
javaModelPackage
);
...
...
@@ -188,7 +174,7 @@ public class TableConfigurationPlugin extends BasePlugin implements ITableConfig
sb
.
append
(
this
.
exampleSuffix
);
introspectedTable
.
setExampleType
(
sb
.
toString
());
}
//
4
. model
//
3
. model
if
(
this
.
modelSuffix
!=
null
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
javaModelPackage
);
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPluginTest.java
View file @
87519edc
...
...
@@ -22,6 +22,7 @@ import org.junit.Assert;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.GeneratedXmlFile
;
import
org.mybatis.generator.api.MyBatisGenerator
;
import
org.mybatis.generator.api.dom.java.Field
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
...
...
@@ -124,4 +125,87 @@ public class TableConfigurationPluginTest {
}
});
}
/**
* 测试clientSuffix
*/
@Test
public
void
testClientSuffix
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/TableConfigurationPlugin/mybatis-generator-with-clientSuffix.xml"
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
boolean
find
=
false
;
for
(
GeneratedJavaFile
file
:
myBatisGenerator
.
getGeneratedJavaFiles
())
{
String
name
=
file
.
getCompilationUnit
().
getType
().
getShortName
();
if
(
name
.
equals
(
"TbDao"
)){
find
=
true
;
}
}
Assert
.
assertTrue
(
find
);
find
=
false
;
for
(
GeneratedXmlFile
file
:
myBatisGenerator
.
getGeneratedXmlFiles
())
{
String
name
=
file
.
getFileName
();
if
(
name
.
equals
(
"TbDao.xml"
)){
find
=
true
;
}
}
Assert
.
assertTrue
(
find
);
// 执行一条语句确认其可用
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"scripts/TableConfigurationPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
ObjectUtil
tbDao
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbDao"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdLessThan"
,
4L
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbDao
.
getObject
(),
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"select id, field1, inc_f1, inc_f2, inc_f3 from tb WHERE ( id < '4' )"
);
// 执行
List
list
=
(
List
)
tbDao
.
invoke
(
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
list
.
size
(),
3
);
}
});
}
/**
* 测试exampleSuffix
*/
@Test
public
void
testExampleSuffix
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/TableConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml"
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
boolean
find
=
false
;
for
(
GeneratedJavaFile
file
:
myBatisGenerator
.
getGeneratedJavaFiles
())
{
String
name
=
file
.
getCompilationUnit
().
getType
().
getShortName
();
if
(
name
.
equals
(
"TbQuery"
)){
find
=
true
;
}
}
Assert
.
assertTrue
(
find
);
// 执行一条语句确认其可用
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"scripts/TableConfigurationPlugin/init.sql"
),
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
tbQuery
=
new
ObjectUtil
(
loader
,
packagz
+
".TbQuery"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbQuery
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdLessThan"
,
4L
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExample"
,
tbQuery
.
getObject
());
Assert
.
assertEquals
(
sql
,
"select id, field1, inc_f1, inc_f2, inc_f3 from tb WHERE ( id < '4' )"
);
// 执行
List
list
=
(
List
)
tbMapper
.
invoke
(
"selectByExample"
,
tbQuery
.
getObject
());
Assert
.
assertEquals
(
list
.
size
(),
3
);
}
});
}
}
\ No newline at end of file
src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-clientSuffix.xml
0 → 100644
View file @
87519edc
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ 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.TableConfigurationPlugin"
>
<property
name=
"clientSuffix"
value=
"Dao"
/>
</plugin>
<!--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>
<table
tableName=
"tb_keys"
/>
</context>
</generatorConfiguration>
\ No newline at end of file
src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator-with-exampleSuffix.xml
0 → 100644
View file @
87519edc
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ 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.TableConfigurationPlugin"
>
<property
name=
"exampleSuffix"
value=
"Query"
/>
</plugin>
<!--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>
<table
tableName=
"tb_keys"
/>
</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