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
caa5144d
Commit
caa5144d
authored
May 22, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TableConfigurationPlugin
parent
8588c991
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
503 additions
and
3 deletions
+503
-3
src/main/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPlugin.java
.../itfsw/mybatis/generator/plugins/ExampleTargetPlugin.java
+1
-0
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
.../itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
+1
-0
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
...fsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
+1
-0
src/main/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPlugin.java
...w/mybatis/generator/plugins/TableConfigurationPlugin.java
+201
-0
src/main/java/com/itfsw/mybatis/generator/plugins/TablePrefixPlugin.java
...om/itfsw/mybatis/generator/plugins/TablePrefixPlugin.java
+2
-0
src/main/java/com/itfsw/mybatis/generator/plugins/TableRenamePlugin.java
...om/itfsw/mybatis/generator/plugins/TableRenamePlugin.java
+2
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
...com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
+12
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BeanUtils.java
.../com/itfsw/mybatis/generator/plugins/utils/BeanUtils.java
+3
-2
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
.../mybatis/generator/plugins/utils/hook/HookAggregator.java
+10
-1
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ITableConfigurationHook.java
...generator/plugins/utils/hook/ITableConfigurationHook.java
+35
-0
src/test/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPluginTest.java
...batis/generator/plugins/TableConfigurationPluginTest.java
+50
-0
src/test/resources/scripts/TableConfigurationPlugin/init.sql
src/test/resources/scripts/TableConfigurationPlugin/init.sql
+120
-0
src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator.xml
...es/scripts/TableConfigurationPlugin/mybatis-generator.xml
+65
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPlugin.java
View file @
caa5144d
...
@@ -61,6 +61,7 @@ public class ExampleTargetPlugin extends BasePlugin {
...
@@ -61,6 +61,7 @@ public class ExampleTargetPlugin extends BasePlugin {
*/
*/
@Override
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
super
.
initialized
(
introspectedTable
);
String
exampleType
=
introspectedTable
.
getExampleType
();
String
exampleType
=
introspectedTable
.
getExampleType
();
// 修改包名
// 修改包名
Context
context
=
getContext
();
Context
context
=
getContext
();
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
View file @
caa5144d
...
@@ -75,6 +75,7 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -75,6 +75,7 @@ public class LogicalDeletePlugin extends BasePlugin {
*/
*/
@Override
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
super
.
initialized
(
introspectedTable
);
// 1. 首先获取全局配置
// 1. 首先获取全局配置
Properties
properties
=
getProperties
();
Properties
properties
=
getProperties
();
String
logicalDeleteColumn
=
properties
.
getProperty
(
PRO_LOGICAL_DELETE_COLUMN
);
String
logicalDeleteColumn
=
properties
.
getProperty
(
PRO_LOGICAL_DELETE_COLUMN
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
View file @
caa5144d
...
@@ -59,6 +59,7 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
...
@@ -59,6 +59,7 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
@Override
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
super
.
initialized
(
introspectedTable
);
sqlMaps
.
put
(
introspectedTable
,
new
ArrayList
<>());
sqlMaps
.
put
(
introspectedTable
,
new
ArrayList
<>());
// 读取并验证版本列
// 读取并验证版本列
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPlugin.java
0 → 100644
View file @
caa5144d
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.BeanUtils
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.ITableConfigurationHook
;
import
org.mybatis.generator.api.FullyQualifiedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.config.ColumnRenamingRule
;
import
org.mybatis.generator.config.DomainObjectRenamingRule
;
import
org.mybatis.generator.config.TableConfiguration
;
import
java.util.List
;
import
java.util.Properties
;
import
static
org
.
mybatis
.
generator
.
internal
.
util
.
StringUtility
.
stringHasValue
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/21 11:23
* ---------------------------------------------------------------------------
*/
public
class
TableConfigurationPlugin
extends
BasePlugin
implements
ITableConfigurationHook
{
public
static
final
String
PRO_TABLE_SEARCH_STRING
=
"domainObjectRenamingRule.searchString"
;
// 查找 property
public
static
final
String
PRO_TABLE_REPLACE_STRING
=
"domainObjectRenamingRule.replaceString"
;
// 替换 property
public
static
final
String
PRO_COLUMN_SEARCH_STRING
=
"columnRenamingRule.searchString"
;
// 查找 property
public
static
final
String
PRO_COLUMN_REPLACE_STRING
=
"columnRenamingRule.replaceString"
;
// 替换 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 结尾
private
String
tableSearchString
;
private
String
tableReplaceString
;
private
String
columnSearchString
;
private
String
columnReplaceString
;
private
String
clientSuffix
;
// client 结尾
private
String
sqlMapSuffix
;
// sqlMap 结尾
private
String
exampleSuffix
;
// example 结尾
private
String
modelSuffix
;
// model 结尾
/**
* {@inheritDoc}
* @param warnings
* @return
*/
@Override
public
boolean
validate
(
List
<
String
>
warnings
)
{
Properties
properties
=
this
.
getProperties
();
// 如果配置了searchString 或者 replaceString,二者不允许单独存在
if
((
properties
.
getProperty
(
PRO_TABLE_SEARCH_STRING
)
==
null
&&
properties
.
getProperty
(
PRO_TABLE_REPLACE_STRING
)
!=
null
)
||
(
properties
.
getProperty
(
PRO_TABLE_SEARCH_STRING
)
!=
null
&&
properties
.
getProperty
(
PRO_TABLE_REPLACE_STRING
)
==
null
)
||
(
properties
.
getProperty
(
PRO_COLUMN_SEARCH_STRING
)
==
null
&&
properties
.
getProperty
(
PRO_COLUMN_REPLACE_STRING
)
!=
null
)
||
(
properties
.
getProperty
(
PRO_COLUMN_SEARCH_STRING
)
!=
null
&&
properties
.
getProperty
(
PRO_COLUMN_REPLACE_STRING
)
==
null
))
{
warnings
.
add
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件的searchString、replaceString属性需配合使用,不能单独存在!"
);
return
false
;
}
this
.
tableSearchString
=
properties
.
getProperty
(
PRO_TABLE_SEARCH_STRING
);
this
.
tableReplaceString
=
properties
.
getProperty
(
PRO_TABLE_REPLACE_STRING
);
this
.
columnSearchString
=
properties
.
getProperty
(
PRO_COLUMN_SEARCH_STRING
);
this
.
columnReplaceString
=
properties
.
getProperty
(
PRO_COLUMN_REPLACE_STRING
);
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
);
}
/**
* 表配置
* @param introspectedTable
*/
@Override
public
void
tableConfiguration
(
IntrospectedTable
introspectedTable
)
{
try
{
TableConfiguration
tableConfiguration
=
introspectedTable
.
getTableConfiguration
();
FullyQualifiedTable
fullyQualifiedTable
=
introspectedTable
.
getFullyQualifiedTable
();
String
javaClientInterfacePackage
=
(
String
)
BeanUtils
.
invoke
(
introspectedTable
,
IntrospectedTable
.
class
,
"calculateJavaClientInterfacePackage"
);
String
sqlMapPackage
=
(
String
)
BeanUtils
.
invoke
(
introspectedTable
,
IntrospectedTable
.
class
,
"calculateSqlMapPackage"
);
String
javaModelPackage
=
(
String
)
BeanUtils
.
invoke
(
introspectedTable
,
IntrospectedTable
.
class
,
"calculateJavaModelPackage"
);
// --------------------- table 重命名 ----------------------------
if
(
tableConfiguration
.
getDomainObjectRenamingRule
()
==
null
&&
this
.
tableSearchString
!=
null
&&
this
.
tableReplaceString
!=
null
)
{
DomainObjectRenamingRule
rule
=
new
DomainObjectRenamingRule
();
rule
.
setSearchString
(
this
.
tableSearchString
);
rule
.
setReplaceString
(
this
.
tableReplaceString
);
tableConfiguration
.
setDomainObjectRenamingRule
(
rule
);
}
// --------------------- column 重命名 ---------------------------
if
(
tableConfiguration
.
getColumnRenamingRule
()
==
null
&&
this
.
columnSearchString
!=
null
&&
this
.
columnReplaceString
!=
null
)
{
ColumnRenamingRule
rule
=
new
ColumnRenamingRule
();
rule
.
setSearchString
(
this
.
columnSearchString
);
rule
.
setReplaceString
(
this
.
columnReplaceString
);
tableConfiguration
.
setColumnRenamingRule
(
rule
);
}
// ---------------------- 后缀修正 -------------------------------
// 1. client
if
(
this
.
clientSuffix
!=
null
)
{
// mapper
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
javaClientInterfacePackage
);
sb
.
append
(
'.'
);
if
(
stringHasValue
(
tableConfiguration
.
getMapperName
()))
{
sb
.
append
(
tableConfiguration
.
getMapperName
());
}
else
{
if
(
stringHasValue
(
fullyQualifiedTable
.
getDomainObjectSubPackage
()))
{
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectSubPackage
());
sb
.
append
(
'.'
);
}
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectName
());
sb
.
append
(
this
.
clientSuffix
);
}
introspectedTable
.
setMyBatis3JavaMapperType
(
sb
.
toString
());
// xml mapper namespace
sb
.
setLength
(
0
);
sb
.
append
(
sqlMapPackage
);
sb
.
append
(
'.'
);
if
(
stringHasValue
(
tableConfiguration
.
getMapperName
()))
{
sb
.
append
(
tableConfiguration
.
getMapperName
());
}
else
{
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectName
());
sb
.
append
(
this
.
clientSuffix
);
}
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
());
}
// 2. example
if
(
this
.
exampleSuffix
!=
null
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
javaModelPackage
);
sb
.
append
(
'.'
);
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectName
());
sb
.
append
(
this
.
exampleSuffix
);
introspectedTable
.
setExampleType
(
sb
.
toString
());
}
// 3. model
if
(
this
.
modelSuffix
!=
null
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
javaModelPackage
);
sb
.
append
(
'.'
);
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectName
());
sb
.
append
(
"Key"
);
sb
.
append
(
this
.
modelSuffix
);
introspectedTable
.
setPrimaryKeyType
(
sb
.
toString
());
sb
.
setLength
(
0
);
sb
.
append
(
javaModelPackage
);
sb
.
append
(
'.'
);
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectName
());
sb
.
append
(
this
.
modelSuffix
);
introspectedTable
.
setBaseRecordType
(
sb
.
toString
());
sb
.
setLength
(
0
);
sb
.
append
(
javaModelPackage
);
sb
.
append
(
'.'
);
sb
.
append
(
fullyQualifiedTable
.
getDomainObjectName
());
sb
.
append
(
"WithBLOBs"
);
sb
.
append
(
this
.
modelSuffix
);
introspectedTable
.
setRecordWithBLOBsType
(
sb
.
toString
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/TablePrefixPlugin.java
View file @
caa5144d
...
@@ -31,6 +31,7 @@ import java.util.List;
...
@@ -31,6 +31,7 @@ import java.util.List;
* @time:2017/5/18 13:54
* @time:2017/5/18 13:54
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
@Deprecated
public
class
TablePrefixPlugin
extends
BasePlugin
{
public
class
TablePrefixPlugin
extends
BasePlugin
{
public
static
final
String
PRO_PREFIX
=
"prefix"
;
// 前缀 property
public
static
final
String
PRO_PREFIX
=
"prefix"
;
// 前缀 property
...
@@ -78,5 +79,6 @@ public class TablePrefixPlugin extends BasePlugin {
...
@@ -78,5 +79,6 @@ public class TablePrefixPlugin extends BasePlugin {
logger
.
error
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"使用prefix替换时异常!"
,
e
);
logger
.
error
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"使用prefix替换时异常!"
,
e
);
}
}
}
}
super
.
initialized
(
introspectedTable
);
}
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/TableRenamePlugin.java
View file @
caa5144d
...
@@ -33,6 +33,7 @@ import java.util.regex.Pattern;
...
@@ -33,6 +33,7 @@ import java.util.regex.Pattern;
* @time:2017/6/6 15:18
* @time:2017/6/6 15:18
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
@Deprecated
public
class
TableRenamePlugin
extends
BasePlugin
{
public
class
TableRenamePlugin
extends
BasePlugin
{
public
static
final
String
PRO_SEARCH_STRING
=
"searchString"
;
// 查找 property
public
static
final
String
PRO_SEARCH_STRING
=
"searchString"
;
// 查找 property
public
static
final
String
PRO_REPLACE_STRING
=
"replaceString"
;
// 替换 property
public
static
final
String
PRO_REPLACE_STRING
=
"replaceString"
;
// 替换 property
...
@@ -94,6 +95,7 @@ public class TableRenamePlugin extends BasePlugin {
...
@@ -94,6 +95,7 @@ public class TableRenamePlugin extends BasePlugin {
logger
.
error
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"使用searchString、replaceString替换时异常!"
,
e
);
logger
.
error
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"使用searchString、replaceString替换时异常!"
,
e
);
}
}
}
}
super
.
initialized
(
introspectedTable
);
}
}
/**
/**
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
View file @
caa5144d
...
@@ -19,7 +19,9 @@ package com.itfsw.mybatis.generator.plugins.utils;
...
@@ -19,7 +19,9 @@ package com.itfsw.mybatis.generator.plugins.utils;
import
com.itfsw.mybatis.generator.plugins.CommentPlugin
;
import
com.itfsw.mybatis.generator.plugins.CommentPlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.enhanced.TemplateCommentGenerator
;
import
com.itfsw.mybatis.generator.plugins.utils.enhanced.TemplateCommentGenerator
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.HookAggregator
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.HookAggregator
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.ITableConfigurationHook
;
import
org.mybatis.generator.api.CommentGenerator
;
import
org.mybatis.generator.api.CommentGenerator
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.PluginAdapter
;
import
org.mybatis.generator.api.PluginAdapter
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.PluginConfiguration
;
import
org.mybatis.generator.config.PluginConfiguration
;
...
@@ -97,4 +99,14 @@ public class BasePlugin extends PluginAdapter {
...
@@ -97,4 +99,14 @@ public class BasePlugin extends PluginAdapter {
return
true
;
return
true
;
}
}
/**
* {@inheritDoc}
* @param introspectedTable
*/
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
super
.
initialized
(
introspectedTable
);
PluginTools
.
getHook
(
ITableConfigurationHook
.
class
).
tableConfiguration
(
introspectedTable
);
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BeanUtils.java
View file @
caa5144d
...
@@ -58,14 +58,15 @@ public class BeanUtils {
...
@@ -58,14 +58,15 @@ public class BeanUtils {
/**
/**
* 执行无参方法
* 执行无参方法
* @param bean
* @param bean
* @param clazz
* @param name
* @param name
* @return
* @return
* @throws NoSuchMethodException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalAccessException
*/
*/
public
static
Object
invoke
(
final
Object
bean
,
final
String
name
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
public
static
Object
invoke
(
final
Object
bean
,
Class
clazz
,
final
String
name
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
Method
method
=
bean
.
getClass
()
.
getDeclaredMethod
(
name
);
Method
method
=
clazz
.
getDeclaredMethod
(
name
);
method
.
setAccessible
(
true
);
method
.
setAccessible
(
true
);
return
method
.
invoke
(
bean
);
return
method
.
invoke
(
bean
);
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
View file @
caa5144d
...
@@ -43,7 +43,7 @@ import java.util.List;
...
@@ -43,7 +43,7 @@ import java.util.List;
* @time:2018/4/27 11:33
* @time:2018/4/27 11:33
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
HookAggregator
implements
IUpsertPluginHook
,
IModelBuilderPluginHook
,
IIncrementsPluginHook
,
IOptimisticLockerPluginHook
,
ISelectOneByExamplePluginHook
{
public
class
HookAggregator
implements
IUpsertPluginHook
,
IModelBuilderPluginHook
,
IIncrementsPluginHook
,
IOptimisticLockerPluginHook
,
ISelectOneByExamplePluginHook
,
ITableConfigurationHook
{
protected
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
BasePlugin
.
class
);
// 日志
protected
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
BasePlugin
.
class
);
// 日志
private
final
static
HookAggregator
instance
=
new
HookAggregator
();
private
final
static
HookAggregator
instance
=
new
HookAggregator
();
private
Context
context
;
private
Context
context
;
...
@@ -250,4 +250,13 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
...
@@ -250,4 +250,13 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
}
}
return
true
;
return
true
;
}
}
// ============================================= ITableConfigurationHook ==============================================
@Override
public
void
tableConfiguration
(
IntrospectedTable
introspectedTable
)
{
if
(!
this
.
getPlugins
(
ITableConfigurationHook
.
class
).
isEmpty
())
{
this
.
getPlugins
(
ITableConfigurationHook
.
class
).
get
(
0
).
tableConfiguration
(
introspectedTable
);
}
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ITableConfigurationHook.java
0 → 100644
View file @
caa5144d
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
import
org.mybatis.generator.api.IntrospectedTable
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/21 11:24
* ---------------------------------------------------------------------------
*/
public
interface
ITableConfigurationHook
{
/**
* 表配置
* @param introspectedTable
*/
void
tableConfiguration
(
IntrospectedTable
introspectedTable
);
}
src/test/java/com/itfsw/mybatis/generator/plugins/TableConfigurationPluginTest.java
0 → 100644
View file @
caa5144d
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/22 13:22
* ---------------------------------------------------------------------------
*/
public
class
TableConfigurationPluginTest
{
@BeforeClass
public
static
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/TableConfigurationPlugin/init.sql"
);
}
@Test
public
void
test
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/TableConfigurationPlugin/mybatis-generator.xml"
);
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"scripts/TableConfigurationPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
}
});
}
}
\ No newline at end of file
src/test/resources/scripts/TableConfigurationPlugin/init.sql
0 → 100644
View file @
caa5144d
/*
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-07-05 17:21:41
*/
SET
FOREIGN_KEY_CHECKS
=
0
;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb`
;
CREATE
TABLE
`tb`
(
`id`
bigint
(
20
)
NOT
NULL
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
MyISAM
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb
-- ----------------------------
INSERT
INTO
`tb`
VALUES
(
'1'
,
'fd1'
,
'0'
,
'0'
,
'0'
);
INSERT
INTO
`tb`
VALUES
(
'2'
,
'fd2'
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb`
VALUES
(
'3'
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb`
VALUES
(
'4'
,
'fd3'
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_blobs
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_blobs`
;
CREATE
TABLE
`tb_blobs`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
,
`field2`
longtext
COMMENT
'注释2'
,
`field3`
longtext
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
MyISAM
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_blobs
-- ----------------------------
INSERT
INTO
`tb_blobs`
VALUES
(
'1'
,
'fd1'
,
null
,
null
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'2'
,
null
,
'fd2'
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'3'
,
null
,
null
,
'fd3'
,
'1'
,
'1'
,
'1'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'4'
,
'fd4'
,
'fd5'
,
'fd6'
,
'0'
,
'0'
,
'0'
);
-- ----------------------------
-- Table structure for tb_keys
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_keys`
;
CREATE
TABLE
`tb_keys`
(
`key1`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`key2`
varchar
(
255
)
NOT
NULL
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`key1`
,
`key2`
)
)
ENGINE
=
MyISAM
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_keys
-- ----------------------------
INSERT
INTO
`tb_keys`
VALUES
(
'1'
,
'k1'
,
'fd1'
,
null
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_keys`
VALUES
(
'2'
,
'k2'
,
null
,
'2'
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_keys`
VALUES
(
'3'
,
'k3'
,
null
,
null
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_single_blob
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_single_blob`
;
CREATE
TABLE
`tb_single_blob`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
longtext
COMMENT
'注释2'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
MyISAM
AUTO_INCREMENT
=
4
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_single_blob
-- ----------------------------
INSERT
INTO
`tb_single_blob`
VALUES
(
'1'
,
'fd1'
,
'0'
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_single_blob`
VALUES
(
'2'
,
null
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_single_blob`
VALUES
(
'3'
,
null
,
null
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_key_word
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_key_word`
;
CREATE
TABLE
`tb_key_word`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`update`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
MyISAM
AUTO_INCREMENT
=
4
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_key_word
-- ----------------------------
INSERT
INTO
`tb_key_word`
VALUES
(
'1'
,
'0'
,
'0'
,
'1'
);
\ No newline at end of file
src/test/resources/scripts/TableConfigurationPlugin/mybatis-generator.xml
0 → 100644
View file @
caa5144d
<?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=
"searchString"
value=
"^"
/>
<property
name=
"replaceString"
value=
"DB1"
/>
<property
name=
"suffixForMapper"
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"
>
<property
name=
"incrementsColumns"
value=
"inc_f1"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_keys"
>
<property
name=
"incrementsColumns"
value=
" inc_f1, inc_f2, inc_f3 "
/>
</table>
<table
tableName=
"tb_blobs"
>
<property
name=
"incrementsColumns"
value=
"inc_f1,inc_f3"
/>
</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