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
e4095dab
Commit
e4095dab
authored
Aug 17, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bugfix[issues#11]:修复逻辑删除插件的常量命名
parent
c485d7be
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
55 deletions
+150
-55
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
.../itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
+70
-55
src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java
...sw/mybatis/generator/plugins/LogicalDeletePluginTest.java
+27
-0
src/test/resources/scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml
...alDeletePlugin/mybatis-generator-with-customConstName.xml
+53
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
View file @
e4095dab
...
@@ -47,19 +47,22 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -47,19 +47,22 @@ public class LogicalDeletePlugin extends BasePlugin {
public
static
final
String
PRO_LOGICAL_DELETE_VALUE
=
"logicalDeleteValue"
;
// 逻辑删除值-Key
public
static
final
String
PRO_LOGICAL_DELETE_VALUE
=
"logicalDeleteValue"
;
// 逻辑删除值-Key
public
static
final
String
PRO_LOGICAL_UN_DELETE_VALUE
=
"logicalUnDeleteValue"
;
// 逻辑删除未删除值-Key
public
static
final
String
PRO_LOGICAL_UN_DELETE_VALUE
=
"logicalUnDeleteValue"
;
// 逻辑删除未删除值-Key
public
static
final
String
DEL_FLAG_NAME
=
"DEL_FLAG_OFF"
;
// 逻辑删除标志位常量名称
public
static
final
String
PRO_LOGICAL_DELETE_CONST_NAME
=
"logicalDeleteConstName"
;
// 逻辑删除常量
public
static
final
String
UN_DEL_FLAG_NAME
=
"DEL_FLAG_ON"
;
// 逻辑删除标志位常量名称(未删除)
public
static
final
String
PRO_LOGICAL_UN_DELETE_CONST_NAME
=
"logicalUnDeleteConstName"
;
// 逻辑删除常量
public
static
final
String
DEFAULT_LOGICAL_DELETE_CONST_NAME
=
"IS_DELETED"
;
// 逻辑删除标志位常量名称
public
static
final
String
DEFAULT_LOGICAL_UN_DELETE_CONST_NAME
=
"NOT_DELETED"
;
// 逻辑删除标志位常量名称(未删除)
public
static
final
String
METHOD_LOGICAL_DELETE
=
"andDeleted"
;
// 逻辑删除查询方法
public
static
final
String
METHOD_LOGICAL_DELETE
=
"andDeleted"
;
// 逻辑删除查询方法
private
IntrospectedColumn
logicalDeleteColumn
;
// 逻辑删除列
private
IntrospectedColumn
logicalDeleteColumn
;
// 逻辑删除列
private
String
logicalDeleteValue
;
// 逻辑删除值
private
String
logicalDeleteValue
;
// 逻辑删除值
private
String
logicalUnDeleteValue
;
// 逻辑删除值(未删除)
private
String
logicalUnDeleteValue
;
// 逻辑删除值(未删除)
private
String
logicalDeleteConstName
;
// 逻辑删除常量
private
String
logicalUnDeleteConstName
;
// 逻辑删除常量(未删除)
/**
/**
* 初始化阶段
* 初始化阶段
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param introspectedTable
* @param introspectedTable
* @return
* @return
*/
*/
...
@@ -71,20 +74,20 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -71,20 +74,20 @@ public class LogicalDeletePlugin extends BasePlugin {
this
.
logicalDeleteValue
=
properties
.
getProperty
(
PRO_LOGICAL_DELETE_VALUE
);
this
.
logicalDeleteValue
=
properties
.
getProperty
(
PRO_LOGICAL_DELETE_VALUE
);
this
.
logicalUnDeleteValue
=
properties
.
getProperty
(
PRO_LOGICAL_UN_DELETE_VALUE
);
this
.
logicalUnDeleteValue
=
properties
.
getProperty
(
PRO_LOGICAL_UN_DELETE_VALUE
);
// 2. 获取表单独配置,如果有则覆盖全局配置
// 2. 获取表单独配置,如果有则覆盖全局配置
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)
!=
null
){
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)
!=
null
)
{
logicalDeleteColumn
=
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
);
logicalDeleteColumn
=
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
);
}
}
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_VALUE
)
!=
null
){
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_VALUE
)
!=
null
)
{
this
.
logicalDeleteValue
=
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_VALUE
);
this
.
logicalDeleteValue
=
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_VALUE
);
}
}
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_UN_DELETE_VALUE
)
!=
null
){
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_UN_DELETE_VALUE
)
!=
null
)
{
this
.
logicalUnDeleteValue
=
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_UN_DELETE_VALUE
);
this
.
logicalUnDeleteValue
=
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_UN_DELETE_VALUE
);
}
}
// 3. 判断该表是否存在逻辑删除列
// 3. 判断该表是否存在逻辑删除列
this
.
logicalDeleteColumn
=
null
;
this
.
logicalDeleteColumn
=
null
;
List
<
IntrospectedColumn
>
columns
=
introspectedTable
.
getAllColumns
();
List
<
IntrospectedColumn
>
columns
=
introspectedTable
.
getAllColumns
();
for
(
IntrospectedColumn
column
:
columns
)
{
for
(
IntrospectedColumn
column
:
columns
)
{
if
(
column
.
getActualColumnName
().
equalsIgnoreCase
(
logicalDeleteColumn
)){
if
(
column
.
getActualColumnName
().
equalsIgnoreCase
(
logicalDeleteColumn
))
{
// 判断字段类型
// 判断字段类型
JDBCType
type
=
JDBCType
.
valueOf
(
column
.
getJdbcType
());
JDBCType
type
=
JDBCType
.
valueOf
(
column
.
getJdbcType
());
if
(
JDBCType
.
BIGINT
==
type
if
(
JDBCType
.
BIGINT
==
type
...
@@ -102,28 +105,31 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -102,28 +105,31 @@ public class LogicalDeletePlugin extends BasePlugin {
||
JDBCType
.
NVARCHAR
==
type
||
JDBCType
.
NVARCHAR
==
type
||
JDBCType
.
SMALLINT
==
type
||
JDBCType
.
SMALLINT
==
type
||
JDBCType
.
TINYINT
==
type
||
JDBCType
.
TINYINT
==
type
||
JDBCType
.
VARCHAR
==
type
){
||
JDBCType
.
VARCHAR
==
type
)
{
this
.
logicalDeleteColumn
=
column
;
this
.
logicalDeleteColumn
=
column
;
}
else
{
}
else
{
warnings
.
add
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getFullyQualifiedTable
()+
"逻辑删除列("
+
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)+
")的类型不在支持范围(请使用数字列,字符串列,布尔列)!"
);
warnings
.
add
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getFullyQualifiedTable
()
+
"逻辑删除列("
+
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)
+
")的类型不在支持范围(请使用数字列,字符串列,布尔列)!"
);
}
}
}
}
}
}
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)
!=
null
&&
this
.
logicalDeleteColumn
==
null
){
if
(
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)
!=
null
&&
this
.
logicalDeleteColumn
==
null
)
{
warnings
.
add
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getFullyQualifiedTable
()+
"没有找到您配置的逻辑删除列("
+
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)+
")!"
);
warnings
.
add
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getFullyQualifiedTable
()
+
"没有找到您配置的逻辑删除列("
+
introspectedTable
.
getTableConfigurationProperty
(
PRO_LOGICAL_DELETE_COLUMN
)
+
")!"
);
}
}
// 4. 判断逻辑删除值是否配置了
// 4. 判断逻辑删除值是否配置了
if
(
this
.
logicalDeleteColumn
!=
null
&&
(
this
.
logicalDeleteValue
==
null
||
this
.
logicalUnDeleteValue
==
null
)){
if
(
this
.
logicalDeleteColumn
!=
null
&&
(
this
.
logicalDeleteValue
==
null
||
this
.
logicalUnDeleteValue
==
null
))
{
warnings
.
add
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getFullyQualifiedTable
()+
"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!"
);
warnings
.
add
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getFullyQualifiedTable
()
+
"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!"
);
}
}
// 5. 获取逻辑删除常量值
this
.
logicalDeleteConstName
=
properties
.
getProperty
(
PRO_LOGICAL_DELETE_CONST_NAME
)
!=
null
?
properties
.
getProperty
(
PRO_LOGICAL_DELETE_CONST_NAME
).
toUpperCase
()
:
DEFAULT_LOGICAL_DELETE_CONST_NAME
;
this
.
logicalUnDeleteConstName
=
properties
.
getProperty
(
PRO_LOGICAL_UN_DELETE_CONST_NAME
)
!=
null
?
properties
.
getProperty
(
PRO_LOGICAL_UN_DELETE_CONST_NAME
).
toUpperCase
()
:
DEFAULT_LOGICAL_UN_DELETE_CONST_NAME
;
}
}
/**
/**
* Java Client Methods 生成
* Java Client Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param interfaze
* @param interfaze
* @param topLevelClass
* @param topLevelClass
* @param introspectedTable
* @param introspectedTable
...
@@ -131,7 +137,7 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -131,7 +137,7 @@ public class LogicalDeletePlugin extends BasePlugin {
*/
*/
@Override
@Override
public
boolean
clientGenerated
(
Interface
interfaze
,
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
public
boolean
clientGenerated
(
Interface
interfaze
,
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
logicalDeleteColumn
!=
null
){
if
(
this
.
logicalDeleteColumn
!=
null
)
{
// 1. 逻辑删除ByExample
// 1. 逻辑删除ByExample
Method
mLogicalDeleteByExample
=
JavaElementGeneratorTools
.
generateMethod
(
Method
mLogicalDeleteByExample
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_LOGICAL_DELETE_BY_EXAMPLE
,
METHOD_LOGICAL_DELETE_BY_EXAMPLE
,
...
@@ -144,10 +150,10 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -144,10 +150,10 @@ public class LogicalDeletePlugin extends BasePlugin {
commentGenerator
.
addGeneralMethodComment
(
mLogicalDeleteByExample
,
introspectedTable
);
commentGenerator
.
addGeneralMethodComment
(
mLogicalDeleteByExample
,
introspectedTable
);
// interface 增加方法
// interface 增加方法
interfaze
.
addMethod
(
mLogicalDeleteByExample
);
interfaze
.
addMethod
(
mLogicalDeleteByExample
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()+
"增加方法logicalDeleteByExample。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加方法logicalDeleteByExample。"
);
// 2. 判断是否有主键,生成主键删除方法
// 2. 判断是否有主键,生成主键删除方法
if
(
introspectedTable
.
hasPrimaryKeyColumns
()){
if
(
introspectedTable
.
hasPrimaryKeyColumns
())
{
// 1. 逻辑删除ByExample
// 1. 逻辑删除ByExample
Method
mLogicalDeleteByPrimaryKey
=
new
Method
(
METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY
);
Method
mLogicalDeleteByPrimaryKey
=
new
Method
(
METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY
);
// 返回值类型
// 返回值类型
...
@@ -190,7 +196,7 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -190,7 +196,7 @@ public class LogicalDeletePlugin extends BasePlugin {
// interface 增加方法
// interface 增加方法
interfaze
.
addImportedTypes
(
importedTypes
);
interfaze
.
addImportedTypes
(
importedTypes
);
interfaze
.
addMethod
(
mLogicalDeleteByPrimaryKey
);
interfaze
.
addMethod
(
mLogicalDeleteByPrimaryKey
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()+
"增加方法logicalDeleteByPrimaryKey。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加方法logicalDeleteByPrimaryKey。"
);
}
}
}
}
return
true
;
return
true
;
...
@@ -199,14 +205,13 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -199,14 +205,13 @@ public class LogicalDeletePlugin extends BasePlugin {
/**
/**
* SQL Map Methods 生成
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param document
* @param document
* @param introspectedTable
* @param introspectedTable
* @return
* @return
*/
*/
@Override
@Override
public
boolean
sqlMapDocumentGenerated
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapDocumentGenerated
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
logicalDeleteColumn
!=
null
){
if
(
this
.
logicalDeleteColumn
!=
null
)
{
// 1. 逻辑删除ByExample
// 1. 逻辑删除ByExample
XmlElement
logicalDeleteByExample
=
new
XmlElement
(
"update"
);
//$NON-NLS-1$
XmlElement
logicalDeleteByExample
=
new
XmlElement
(
"update"
);
//$NON-NLS-1$
logicalDeleteByExample
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_LOGICAL_DELETE_BY_EXAMPLE
));
logicalDeleteByExample
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_LOGICAL_DELETE_BY_EXAMPLE
));
...
@@ -223,14 +228,14 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -223,14 +228,14 @@ public class LogicalDeletePlugin extends BasePlugin {
// 判断字段类型
// 判断字段类型
JDBCType
type
=
JDBCType
.
valueOf
(
this
.
logicalDeleteColumn
.
getJdbcType
());
JDBCType
type
=
JDBCType
.
valueOf
(
this
.
logicalDeleteColumn
.
getJdbcType
());
if
(
this
.
logicalDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalDeleteValue
)){
if
(
this
.
logicalDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalDeleteValue
))
{
sb
.
append
(
"NULL"
);
sb
.
append
(
"NULL"
);
}
else
if
(
JDBCType
.
CHAR
==
type
}
else
if
(
JDBCType
.
CHAR
==
type
||
JDBCType
.
LONGNVARCHAR
==
type
||
JDBCType
.
LONGNVARCHAR
==
type
||
JDBCType
.
LONGVARCHAR
==
type
||
JDBCType
.
LONGVARCHAR
==
type
||
JDBCType
.
NCHAR
==
type
||
JDBCType
.
NCHAR
==
type
||
JDBCType
.
NVARCHAR
==
type
||
JDBCType
.
NVARCHAR
==
type
||
JDBCType
.
VARCHAR
==
type
){
||
JDBCType
.
VARCHAR
==
type
)
{
sb
.
append
(
"'"
);
sb
.
append
(
"'"
);
sb
.
append
(
this
.
logicalDeleteValue
);
sb
.
append
(
this
.
logicalDeleteValue
);
sb
.
append
(
"'"
);
sb
.
append
(
"'"
);
...
@@ -242,10 +247,10 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -242,10 +247,10 @@ public class LogicalDeletePlugin extends BasePlugin {
logicalDeleteByExample
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
logicalDeleteByExample
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
document
.
getRootElement
().
addElement
(
logicalDeleteByExample
);
document
.
getRootElement
().
addElement
(
logicalDeleteByExample
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()+
"增加方法logicalDeleteByExample的实现。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加方法logicalDeleteByExample的实现。"
);
// 2. 判断是否有主键,生成主键删除方法
// 2. 判断是否有主键,生成主键删除方法
if
(
introspectedTable
.
hasPrimaryKeyColumns
()){
if
(
introspectedTable
.
hasPrimaryKeyColumns
())
{
XmlElement
logicalDeleteByPrimaryKey
=
new
XmlElement
(
"update"
);
//$NON-NLS-1$
XmlElement
logicalDeleteByPrimaryKey
=
new
XmlElement
(
"update"
);
//$NON-NLS-1$
logicalDeleteByPrimaryKey
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY
));
logicalDeleteByPrimaryKey
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY
));
...
@@ -274,14 +279,14 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -274,14 +279,14 @@ public class LogicalDeletePlugin extends BasePlugin {
// 判断字段类型
// 判断字段类型
JDBCType
type1
=
JDBCType
.
valueOf
(
this
.
logicalDeleteColumn
.
getJdbcType
());
JDBCType
type1
=
JDBCType
.
valueOf
(
this
.
logicalDeleteColumn
.
getJdbcType
());
if
(
this
.
logicalDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalDeleteValue
)){
if
(
this
.
logicalDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalDeleteValue
))
{
sb1
.
append
(
"NULL"
);
sb1
.
append
(
"NULL"
);
}
else
if
(
JDBCType
.
CHAR
==
type1
}
else
if
(
JDBCType
.
CHAR
==
type1
||
JDBCType
.
LONGNVARCHAR
==
type1
||
JDBCType
.
LONGNVARCHAR
==
type1
||
JDBCType
.
LONGVARCHAR
==
type1
||
JDBCType
.
LONGVARCHAR
==
type1
||
JDBCType
.
NCHAR
==
type1
||
JDBCType
.
NCHAR
==
type1
||
JDBCType
.
NVARCHAR
==
type1
||
JDBCType
.
NVARCHAR
==
type1
||
JDBCType
.
VARCHAR
==
type1
){
||
JDBCType
.
VARCHAR
==
type1
)
{
sb1
.
append
(
"'"
);
sb1
.
append
(
"'"
);
sb1
.
append
(
this
.
logicalDeleteValue
);
sb1
.
append
(
this
.
logicalDeleteValue
);
sb1
.
append
(
"'"
);
sb1
.
append
(
"'"
);
...
@@ -308,7 +313,7 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -308,7 +313,7 @@ public class LogicalDeletePlugin extends BasePlugin {
}
}
document
.
getRootElement
().
addElement
(
logicalDeleteByPrimaryKey
);
document
.
getRootElement
().
addElement
(
logicalDeleteByPrimaryKey
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()+
"增加方法logicalDeleteByPrimaryKey的实现。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加方法logicalDeleteByPrimaryKey的实现。"
);
}
}
}
}
return
true
;
return
true
;
...
@@ -317,50 +322,61 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -317,50 +322,61 @@ public class LogicalDeletePlugin extends BasePlugin {
/**
/**
* Model 生成
* Model 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param topLevelClass
* @param introspectedTable
* @param introspectedTable
* @return
* @return
*/
*/
@Override
@Override
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
logicalDeleteColumn
!=
null
){
if
(
this
.
logicalDeleteColumn
!=
null
)
{
ArrayList
<
Field
>
fields
=
(
ArrayList
<
Field
>)
topLevelClass
.
getFields
();
ArrayList
<
Field
>
fields
=
(
ArrayList
<
Field
>)
topLevelClass
.
getFields
();
// 添加删除标志位常量
DEL_FLAG_OFF
// 添加删除标志位常量
String
delFlagOn
Value
;
String
logicalDelete
Value
;
if
(
this
.
logicalDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalDeleteValue
)){
if
(
this
.
logicalDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalDeleteValue
))
{
delFlagOn
Value
=
"null"
;
logicalDelete
Value
=
"null"
;
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"String"
)){
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"String"
))
{
delFlagOn
Value
=
"\""
+
this
.
logicalDeleteValue
+
"\""
;
logicalDelete
Value
=
"\""
+
this
.
logicalDeleteValue
+
"\""
;
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"Boolean"
)){
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"Boolean"
))
{
delFlagOn
Value
=
(
this
.
logicalDeleteValue
.
equals
(
"1"
)
||
this
.
logicalDeleteValue
.
equalsIgnoreCase
(
"true"
))
?
"true"
:
"false"
;
logicalDelete
Value
=
(
this
.
logicalDeleteValue
.
equals
(
"1"
)
||
this
.
logicalDeleteValue
.
equalsIgnoreCase
(
"true"
))
?
"true"
:
"false"
;
}
else
{
}
else
{
delFlagOn
Value
=
this
.
logicalDeleteValue
;
logicalDelete
Value
=
this
.
logicalDeleteValue
;
}
}
Field
field
=
JavaElementGeneratorTools
.
generateStaticFinalField
(
DEL_FLAG_NAME
,
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
(),
delFlagOnValue
);
// TODO 过期
Field
field
=
JavaElementGeneratorTools
.
generateStaticFinalField
(
"DEL_FLAG_OFF"
,
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
(),
logicalDeleteValue
);
field
.
addAnnotation
(
"@Deprecated"
);
commentGenerator
.
addFieldComment
(
field
,
introspectedTable
);
commentGenerator
.
addFieldComment
(
field
,
introspectedTable
);
// 常量插入到第一位
// 常量插入到第一位
fields
.
add
(
0
,
field
);
fields
.
add
(
0
,
field
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
topLevelClass
.
getType
().
getShortName
()+
"增加方法DEL_FLAG_OFF的常量。"
);
Field
logicalDeleteConstField
=
JavaElementGeneratorTools
.
generateStaticFinalField
(
this
.
logicalDeleteConstName
,
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
(),
logicalDeleteValue
);
commentGenerator
.
addFieldComment
(
logicalDeleteConstField
,
introspectedTable
);
fields
.
add
(
0
,
logicalDeleteConstField
);
// 添加删除标志位常量 DEL_FLAG_ON
// 添加删除标志位常量 DEL_FLAG_ON
String
unDelFlagOn
Value
;
String
logicalUnDelete
Value
;
if
(
this
.
logicalUnDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalUnDeleteValue
)){
if
(
this
.
logicalUnDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
this
.
logicalUnDeleteValue
))
{
unDelFlagOn
Value
=
"null"
;
logicalUnDelete
Value
=
"null"
;
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"String"
)){
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"String"
))
{
unDelFlagOn
Value
=
"\""
+
this
.
logicalUnDeleteValue
+
"\""
;
logicalUnDelete
Value
=
"\""
+
this
.
logicalUnDeleteValue
+
"\""
;
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"Boolean"
)){
}
else
if
(
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
().
getShortNameWithoutTypeArguments
().
equalsIgnoreCase
(
"Boolean"
))
{
unDelFlagOn
Value
=
(
this
.
logicalUnDeleteValue
.
equals
(
"1"
)
||
this
.
logicalUnDeleteValue
.
equalsIgnoreCase
(
"true"
))
?
"true"
:
"false"
;
logicalUnDelete
Value
=
(
this
.
logicalUnDeleteValue
.
equals
(
"1"
)
||
this
.
logicalUnDeleteValue
.
equalsIgnoreCase
(
"true"
))
?
"true"
:
"false"
;
}
else
{
}
else
{
unDelFlagOn
Value
=
this
.
logicalUnDeleteValue
;
logicalUnDelete
Value
=
this
.
logicalUnDeleteValue
;
}
}
Field
field1
=
JavaElementGeneratorTools
.
generateStaticFinalField
(
UN_DEL_FLAG_NAME
,
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
(),
unDelFlagOnValue
);
// TODO 过期
Field
field1
=
JavaElementGeneratorTools
.
generateStaticFinalField
(
"DEL_FLAG_ON"
,
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
(),
logicalUnDeleteValue
);
field1
.
addAnnotation
(
"@Deprecated"
);
commentGenerator
.
addFieldComment
(
field1
,
introspectedTable
);
commentGenerator
.
addFieldComment
(
field1
,
introspectedTable
);
// 常量插入到第一位
// 常量插入到第一位
fields
.
add
(
0
,
field1
);
fields
.
add
(
0
,
field1
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
topLevelClass
.
getType
().
getShortName
()+
"增加方法DEL_FLAG_ON的常量。"
);
Field
logicalUnDeleteConstField
=
JavaElementGeneratorTools
.
generateStaticFinalField
(
this
.
logicalUnDeleteConstName
,
this
.
logicalDeleteColumn
.
getFullyQualifiedJavaType
(),
logicalUnDeleteValue
);
commentGenerator
.
addFieldComment
(
logicalUnDeleteConstField
,
introspectedTable
);
fields
.
add
(
0
,
logicalUnDeleteConstField
);
}
}
return
true
;
return
true
;
}
}
...
@@ -368,14 +384,13 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -368,14 +384,13 @@ public class LogicalDeletePlugin extends BasePlugin {
/**
/**
* ModelExample Methods 生成
* ModelExample Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param topLevelClass
* @param introspectedTable
* @param introspectedTable
* @return
* @return
*/
*/
@Override
@Override
public
boolean
modelExampleClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
public
boolean
modelExampleClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
logicalDeleteColumn
!=
null
){
if
(
this
.
logicalDeleteColumn
!=
null
)
{
// 引入 Model类
// 引入 Model类
FullyQualifiedJavaType
model
=
introspectedTable
.
getRules
().
calculateAllFieldsClass
();
FullyQualifiedJavaType
model
=
introspectedTable
.
getRules
().
calculateAllFieldsClass
();
topLevelClass
.
addImportedType
(
model
);
topLevelClass
.
addImportedType
(
model
);
...
@@ -402,7 +417,7 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -402,7 +417,7 @@ public class LogicalDeletePlugin extends BasePlugin {
equalToMethodName
.
insert
(
0
,
"and"
);
//$NON-NLS-1$
equalToMethodName
.
insert
(
0
,
"and"
);
//$NON-NLS-1$
equalToMethodName
.
append
(
"EqualTo"
);
equalToMethodName
.
append
(
"EqualTo"
);
sb
.
append
(
equalToMethodName
);
sb
.
append
(
equalToMethodName
);
sb
.
append
(
"("
+
modelName
+
"."
+
DEL_FLAG_NAME
+
")"
);
sb
.
append
(
"("
+
modelName
+
"."
+
this
.
logicalDeleteConstName
+
")"
);
sb
.
append
(
" : "
);
sb
.
append
(
" : "
);
...
@@ -413,14 +428,14 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -413,14 +428,14 @@ public class LogicalDeletePlugin extends BasePlugin {
notEqualToMethodName
.
insert
(
0
,
"and"
);
//$NON-NLS-1$
notEqualToMethodName
.
insert
(
0
,
"and"
);
//$NON-NLS-1$
notEqualToMethodName
.
append
(
"NotEqualTo"
);
notEqualToMethodName
.
append
(
"NotEqualTo"
);
sb
.
append
(
notEqualToMethodName
);
sb
.
append
(
notEqualToMethodName
);
sb
.
append
(
"("
+
modelName
+
"."
+
DEL_FLAG_NAME
+
")"
);
sb
.
append
(
"("
+
modelName
+
"."
+
this
.
logicalDeleteConstName
+
")"
);
sb
.
append
(
";"
);
sb
.
append
(
";"
);
method
.
addBodyLine
(
sb
.
toString
());
method
.
addBodyLine
(
sb
.
toString
());
innerClass
.
addMethod
(
method
);
innerClass
.
addMethod
(
method
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
topLevelClass
.
getType
().
getShortName
()+
"."
+
innerClass
.
getType
().
getShortName
()+
"增加andDeleted方法。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
topLevelClass
.
getType
().
getShortName
()
+
"."
+
innerClass
.
getType
().
getShortName
()
+
"增加andDeleted方法。"
);
}
}
}
}
}
}
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java
View file @
e4095dab
...
@@ -148,4 +148,31 @@ public class LogicalDeletePluginTest {
...
@@ -148,4 +148,31 @@ public class LogicalDeletePluginTest {
}
}
});
});
}
}
/**
* 测试自定义常量
*/
@Test
public
void
testCustomConst
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml"
);
tool
.
generate
(
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
Tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andDelFlagEqualTo"
,
Tb
.
get
(
"UN_DEL"
));
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
// 验证sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"select id, del_flag, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '0' and id = '3' )"
);
// 验证执行
Object
result
=
tbMapper
.
invoke
(
"selectByExample"
,
tbExample
.
getObject
());
Assert
.
assertEquals
(((
List
)
result
).
size
(),
0
);
}
});
}
}
}
src/test/resources/scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml
0 → 100644
View file @
e4095dab
<?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.LogicalDeletePlugin"
>
<property
name=
"logicalDeleteColumn"
value=
"del_flag"
/>
<property
name=
"logicalDeleteValue"
value=
"1"
/>
<property
name=
"logicalUnDeleteValue"
value=
"0"
/>
<property
name=
"logicalDeleteConstName"
value=
"DEL"
/>
<property
name=
"logicalUnDeleteConstName"
value=
"UN_DEL"
/>
</plugin>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
/>
<!--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