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
3191da4c
Commit
3191da4c
authored
Jun 13, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bugfix[issues#7]:修正TablePrefixPlugin和TableRenamePlugin的实现逻辑
parent
df7be169
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
136 additions
and
162 deletions
+136
-162
src/main/java/com/itfsw/mybatis/generator/plugins/TablePrefixPlugin.java
...om/itfsw/mybatis/generator/plugins/TablePrefixPlugin.java
+30
-52
src/main/java/com/itfsw/mybatis/generator/plugins/TableRenamePlugin.java
...om/itfsw/mybatis/generator/plugins/TableRenamePlugin.java
+25
-110
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java
...batis/generator/plugins/utils/IntrospectedTableTools.java
+81
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/TablePrefixPlugin.java
View file @
3191da4c
...
@@ -17,7 +17,11 @@
...
@@ -17,7 +17,11 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.config.TableConfiguration
;
import
java.util.List
;
/**
/**
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
...
@@ -32,10 +36,26 @@ public class TablePrefixPlugin extends BasePlugin {
...
@@ -32,10 +36,26 @@ public class TablePrefixPlugin extends BasePlugin {
public
static
final
String
PRE_PREFIX
=
"prefix"
;
// 前缀 property
public
static
final
String
PRE_PREFIX
=
"prefix"
;
// 前缀 property
private
String
prefix
;
// 前缀
private
String
prefix
;
// 前缀
/**
* {@inheritDoc}
*/
@Override
public
boolean
validate
(
List
<
String
>
warnings
)
{
// 如果table配置了domainObjectName或者mapperName就不要再启动该插件了
for
(
TableConfiguration
tableConfiguration
:
context
.
getTableConfigurations
())
{
if
(
tableConfiguration
.
getDomainObjectName
()
!=
null
||
tableConfiguration
.
getMapperName
()
!=
null
)
{
logger
.
warn
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件请不要配合table的domainObjectName或者mapperName一起使用!"
);
return
false
;
}
}
return
super
.
validate
(
warnings
);
}
/**
/**
* 初始化阶段
* 初始化阶段
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param introspectedTable
* @param introspectedTable
* @return
* @return
*/
*/
...
@@ -44,61 +64,19 @@ public class TablePrefixPlugin extends BasePlugin {
...
@@ -44,61 +64,19 @@ public class TablePrefixPlugin extends BasePlugin {
// 1. 首先获取全局配置
// 1. 首先获取全局配置
this
.
prefix
=
getProperties
().
getProperty
(
PRE_PREFIX
);
this
.
prefix
=
getProperties
().
getProperty
(
PRE_PREFIX
);
// 2. 获取每个table 具体的
// 2. 获取每个table 具体的
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRE_PREFIX
)
!=
null
){
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRE_PREFIX
)
!=
null
)
{
this
.
prefix
=
introspectedTable
.
getTableConfigurationProperty
(
PRE_PREFIX
);
this
.
prefix
=
introspectedTable
.
getTableConfigurationProperty
(
PRE_PREFIX
);
}
}
// 3. 判断是否配置了前缀
// 3. 判断是否配置了前缀
if
(
this
.
prefix
!=
null
){
// !!! TableRenamePlugin 插件的 tableOverride 优先级最高
// 3.1. 为Model增加前缀
if
(
this
.
prefix
!=
null
&&
introspectedTable
.
getTableConfigurationProperty
(
TableRenamePlugin
.
PRE_TABLE_OVERRIDE
)
==
null
)
{
if
(
introspectedTable
.
getBaseRecordType
()
!=
null
){
String
domainObjectName
=
introspectedTable
.
getFullyQualifiedTable
().
getDomainObjectName
();
introspectedTable
.
setBaseRecordType
(
this
.
renameJavaType
(
introspectedTable
.
getBaseRecordType
()));
domainObjectName
=
prefix
+
domainObjectName
;
}
try
{
IntrospectedTableTools
.
setDomainObjectName
(
introspectedTable
,
getContext
(),
domainObjectName
);
// 3.2. 为ModelKey添加前缀
}
catch
(
Exception
e
)
{
if
(
introspectedTable
.
getPrimaryKeyType
()
!=
null
){
logger
.
error
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"使用prefix替换时异常!"
,
e
);
introspectedTable
.
setPrimaryKeyType
(
this
.
renameJavaType
(
introspectedTable
.
getPrimaryKeyType
()));
}
// 3.3. WithBLOBs Model 添加前缀
if
(
introspectedTable
.
getRecordWithBLOBsType
()
!=
null
){
introspectedTable
.
setRecordWithBLOBsType
(
this
.
renameJavaType
(
introspectedTable
.
getRecordWithBLOBsType
()));
}
// 3.4. mapper 添加前缀
if
(
introspectedTable
.
getMyBatis3JavaMapperType
()
!=
null
){
introspectedTable
.
setMyBatis3JavaMapperType
(
this
.
renameJavaType
(
introspectedTable
.
getMyBatis3JavaMapperType
()));
}
// 3.5. example 添加前缀
if
(
introspectedTable
.
getExampleType
()
!=
null
){
introspectedTable
.
setExampleType
(
this
.
renameJavaType
(
introspectedTable
.
getExampleType
()));
}
// 3.6. Dao 添加前缀
if
(
introspectedTable
.
getDAOInterfaceType
()
!=
null
){
introspectedTable
.
setDAOInterfaceType
(
this
.
renameJavaType
(
introspectedTable
.
getDAOInterfaceType
()));
}
// 3.7. DAOImpl 添加前缀
if
(
introspectedTable
.
getDAOImplementationType
()
!=
null
){
introspectedTable
.
setDAOImplementationType
(
this
.
renameJavaType
(
introspectedTable
.
getDAOImplementationType
()));
}
// 3.8. 修正xml文件前缀
if
(
introspectedTable
.
getMyBatis3XmlMapperFileName
()
!=
null
){
introspectedTable
.
setMyBatis3XmlMapperFileName
(
this
.
prefix
+
introspectedTable
.
getMyBatis3XmlMapperFileName
());
}
}
}
}
}
}
/**
* 为类型添加前缀
*
* @param type
* @return
*/
private
String
renameJavaType
(
String
type
){
int
lastDot
=
type
.
lastIndexOf
(
"."
)
+
1
;
return
type
.
substring
(
0
,
lastDot
)
+
this
.
prefix
+
type
.
substring
(
lastDot
);
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/TableRenamePlugin.java
View file @
3191da4c
...
@@ -17,7 +17,10 @@
...
@@ -17,7 +17,10 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.config.TableConfiguration
;
import
org.mybatis.generator.internal.util.JavaBeansUtil
;
import
java.util.List
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
...
@@ -49,6 +52,14 @@ public class TableRenamePlugin extends BasePlugin {
...
@@ -49,6 +52,14 @@ public class TableRenamePlugin extends BasePlugin {
return
false
;
return
false
;
}
}
// 如果table配置了domainObjectName或者mapperName就不要再启动该插件了
for
(
TableConfiguration
tableConfiguration:
context
.
getTableConfigurations
())
{
if
(
tableConfiguration
.
getDomainObjectName
()
!=
null
||
tableConfiguration
.
getMapperName
()
!=
null
){
logger
.
warn
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件请不要配合table的domainObjectName或者mapperName一起使用!"
);
return
false
;
}
}
return
super
.
validate
(
warnings
);
return
super
.
validate
(
warnings
);
}
}
...
@@ -63,122 +74,26 @@ public class TableRenamePlugin extends BasePlugin {
...
@@ -63,122 +74,26 @@ public class TableRenamePlugin extends BasePlugin {
// 1. 获取表单独配置
// 1. 获取表单独配置
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRE_TABLE_OVERRIDE
)
!=
null
)
{
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRE_TABLE_OVERRIDE
)
!=
null
)
{
String
override
=
introspectedTable
.
getTableConfigurationProperty
(
PRE_TABLE_OVERRIDE
);
String
override
=
introspectedTable
.
getTableConfigurationProperty
(
PRE_TABLE_OVERRIDE
);
try
{
// 3.1. Model修正名称
IntrospectedTableTools
.
setDomainObjectName
(
introspectedTable
,
getContext
(),
override
);
if
(
introspectedTable
.
getBaseRecordType
()
!=
null
){
}
catch
(
Exception
e
)
{
introspectedTable
.
setBaseRecordType
(
this
.
renameJavaType
(
introspectedTable
.
getBaseRecordType
(),
override
,
""
));
logger
.
error
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"使用tableOverride替换时异常!"
,
e
);
}
// 3.2. ModelKey修正名称
if
(
introspectedTable
.
getPrimaryKeyType
()
!=
null
){
introspectedTable
.
setPrimaryKeyType
(
this
.
renameJavaType
(
introspectedTable
.
getPrimaryKeyType
(),
override
,
"Key"
));
}
// 3.3. WithBLOBs Model 修正名称
if
(
introspectedTable
.
getRecordWithBLOBsType
()
!=
null
){
introspectedTable
.
setRecordWithBLOBsType
(
this
.
renameJavaType
(
introspectedTable
.
getRecordWithBLOBsType
(),
override
,
"WithBLOBs"
));
}
// 3.4. mapper 修正名称
if
(
introspectedTable
.
getMyBatis3JavaMapperType
()
!=
null
){
introspectedTable
.
setMyBatis3JavaMapperType
(
this
.
renameJavaType
(
introspectedTable
.
getMyBatis3JavaMapperType
(),
override
,
"Mapper"
));
}
// 3.5. example 修正名称
if
(
introspectedTable
.
getExampleType
()
!=
null
){
introspectedTable
.
setExampleType
(
this
.
renameJavaType
(
introspectedTable
.
getExampleType
(),
override
,
"Example"
));
}
// 3.6. Dao 添加前缀
if
(
introspectedTable
.
getDAOInterfaceType
()
!=
null
){
introspectedTable
.
setDAOInterfaceType
(
this
.
renameJavaType
(
introspectedTable
.
getDAOInterfaceType
(),
override
,
"Dao"
));
}
// 3.7. DAOImpl 添加前缀
if
(
introspectedTable
.
getDAOImplementationType
()
!=
null
){
introspectedTable
.
setDAOImplementationType
(
this
.
renameJavaType
(
introspectedTable
.
getDAOImplementationType
(),
override
,
"DAOImpl"
));
}
// 3.8. 修正xml文件名称
if
(
introspectedTable
.
getMyBatis3XmlMapperFileName
()
!=
null
){
introspectedTable
.
setMyBatis3XmlMapperFileName
(
override
+
"Mapper.xml"
);
}
}
}
else
if
(
getProperties
().
getProperty
(
PRE_SEARCH_STRING
)
!=
null
){
}
else
if
(
getProperties
().
getProperty
(
PRE_SEARCH_STRING
)
!=
null
){
String
searchString
=
getProperties
().
getProperty
(
PRE_SEARCH_STRING
);
String
searchString
=
getProperties
().
getProperty
(
PRE_SEARCH_STRING
);
String
replaceString
=
getProperties
().
getProperty
(
PRE_REPLACE_STRING
);
String
replaceString
=
getProperties
().
getProperty
(
PRE_REPLACE_STRING
);
// 3.1. Model修正名称
String
domainObjectName
=
introspectedTable
.
getFullyQualifiedTable
().
getDomainObjectName
();
if
(
introspectedTable
.
getBaseRecordType
()
!=
null
){
Pattern
pattern
=
Pattern
.
compile
(
searchString
);
introspectedTable
.
setBaseRecordType
(
this
.
renameJavaType
(
introspectedTable
.
getBaseRecordType
(),
searchString
,
replaceString
,
""
));
Matcher
matcher
=
pattern
.
matcher
(
domainObjectName
);
}
domainObjectName
=
matcher
.
replaceAll
(
replaceString
);
// 命名规范化
// 3.2. ModelKey修正名称
domainObjectName
=
JavaBeansUtil
.
getCamelCaseString
(
domainObjectName
,
true
);
if
(
introspectedTable
.
getPrimaryKeyType
()
!=
null
){
try
{
introspectedTable
.
setPrimaryKeyType
(
this
.
renameJavaType
(
introspectedTable
.
getPrimaryKeyType
(),
searchString
,
replaceString
,
"Key"
));
IntrospectedTableTools
.
setDomainObjectName
(
introspectedTable
,
getContext
(),
domainObjectName
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"使用searchString、replaceString替换时异常!"
,
e
);
// 3.3. WithBLOBs Model 修正名称
if
(
introspectedTable
.
getRecordWithBLOBsType
()
!=
null
){
introspectedTable
.
setRecordWithBLOBsType
(
this
.
renameJavaType
(
introspectedTable
.
getRecordWithBLOBsType
(),
searchString
,
replaceString
,
"WithBLOBs"
));
}
// 3.4. mapper 修正名称
if
(
introspectedTable
.
getMyBatis3JavaMapperType
()
!=
null
){
introspectedTable
.
setMyBatis3JavaMapperType
(
this
.
renameJavaType
(
introspectedTable
.
getMyBatis3JavaMapperType
(),
searchString
,
replaceString
,
"Mapper"
));
}
// 3.5. example 修正名称
if
(
introspectedTable
.
getExampleType
()
!=
null
){
introspectedTable
.
setExampleType
(
this
.
renameJavaType
(
introspectedTable
.
getExampleType
(),
searchString
,
replaceString
,
"Example"
));
}
// 3.6. Dao 添加前缀
if
(
introspectedTable
.
getDAOInterfaceType
()
!=
null
){
introspectedTable
.
setDAOInterfaceType
(
this
.
renameJavaType
(
introspectedTable
.
getDAOInterfaceType
(),
searchString
,
replaceString
,
"Dao"
));
}
// 3.7. DAOImpl 添加前缀
if
(
introspectedTable
.
getDAOImplementationType
()
!=
null
){
introspectedTable
.
setDAOImplementationType
(
this
.
renameJavaType
(
introspectedTable
.
getDAOImplementationType
(),
searchString
,
replaceString
,
"DAOImpl"
));
}
// 3.8. 修正xml文件名称
if
(
introspectedTable
.
getMyBatis3XmlMapperFileName
()
!=
null
){
Pattern
pattern
=
Pattern
.
compile
(
searchString
);
Matcher
matcher
=
pattern
.
matcher
(
introspectedTable
.
getMyBatis3XmlMapperFileName
());
String
fileName
=
matcher
.
replaceAll
(
replaceString
);
introspectedTable
.
setMyBatis3XmlMapperFileName
(
fileName
);
}
}
}
}
}
}
/**
* 重命名类型
*
* @param type
* @param override
* @return
*/
private
String
renameJavaType
(
String
type
,
String
override
,
String
suffix
)
{
int
lastDot
=
type
.
lastIndexOf
(
"."
);
return
type
.
substring
(
0
,
lastDot
)
+
"."
+
override
+
suffix
;
}
/**
* 重命名类型
*
* @param type
* @param searchString
* @param replaceString
* @return
*/
private
String
renameJavaType
(
String
type
,
String
searchString
,
String
replaceString
,
String
suffix
)
{
int
lastDot
=
type
.
lastIndexOf
(
"."
);
String
shortName
=
type
.
substring
(
lastDot
+
1
,
type
.
length
()
-
1
);
Pattern
pattern
=
Pattern
.
compile
(
searchString
);
Matcher
matcher
=
pattern
.
matcher
(
shortName
);
return
type
.
substring
(
0
,
lastDot
)
+
"."
+
matcher
.
replaceAll
(
replaceString
)
+
suffix
;
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java
0 → 100644
View file @
3191da4c
/*
* 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
.
utils
;
import
com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin
;
import
org.mybatis.generator.api.FullyQualifiedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.JavaModelGeneratorConfiguration
;
import
org.mybatis.generator.config.PluginConfiguration
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
/**
* ---------------------------------------------------------------------------
* IntrospectedTable 的一些拓展增强
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/13 13:48
* ---------------------------------------------------------------------------
*/
public
class
IntrospectedTableTools
{
/**
* 设置DomainObjectName和MapperName
*
* @param introspectedTable
* @param context
* @param domainObjectName
*/
public
static
void
setDomainObjectName
(
IntrospectedTable
introspectedTable
,
Context
context
,
String
domainObjectName
)
throws
NoSuchFieldException
,
IllegalAccessException
,
NoSuchMethodException
,
InvocationTargetException
{
// 配置信息(没啥用)
introspectedTable
.
getTableConfiguration
().
setDomainObjectName
(
domainObjectName
);
// FullyQualifiedTable修正
Field
domainObjectNameField
=
FullyQualifiedTable
.
class
.
getDeclaredField
(
"domainObjectName"
);
domainObjectNameField
.
setAccessible
(
true
);
domainObjectNameField
.
set
(
introspectedTable
.
getFullyQualifiedTable
(),
domainObjectName
);
// 重新修正introspectedTable属性信息
Method
calculateJavaClientAttributes
=
IntrospectedTable
.
class
.
getDeclaredMethod
(
"calculateJavaClientAttributes"
);
calculateJavaClientAttributes
.
setAccessible
(
true
);
calculateJavaClientAttributes
.
invoke
(
introspectedTable
);
Method
calculateModelAttributes
=
IntrospectedTable
.
class
.
getDeclaredMethod
(
"calculateModelAttributes"
);
calculateModelAttributes
.
setAccessible
(
true
);
calculateModelAttributes
.
invoke
(
introspectedTable
);
Method
calculateXmlAttributes
=
IntrospectedTable
.
class
.
getDeclaredMethod
(
"calculateXmlAttributes"
);
calculateXmlAttributes
.
setAccessible
(
true
);
calculateXmlAttributes
.
invoke
(
introspectedTable
);
// 注意!! 如果配置了ExampleTargetPlugin插件,要修正Example 位置
PluginConfiguration
configuration
=
PluginTools
.
getPluginConfiguration
(
ExampleTargetPlugin
.
class
,
context
);
if
(
configuration
!=
null
&&
configuration
.
getProperty
(
ExampleTargetPlugin
.
TARGET_PACKAGE_KEY
)
!=
null
){
String
exampleType
=
introspectedTable
.
getExampleType
();
// 修改包名
JavaModelGeneratorConfiguration
javaModelGeneratorConfiguration
=
context
.
getJavaModelGeneratorConfiguration
();
String
targetPackage
=
javaModelGeneratorConfiguration
.
getTargetPackage
();
String
newExampleType
=
exampleType
.
replace
(
targetPackage
,
configuration
.
getProperty
(
ExampleTargetPlugin
.
TARGET_PACKAGE_KEY
));
introspectedTable
.
setExampleType
(
newExampleType
);
}
}
}
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