Compare commits

...
6 Commits
Author SHA1 Message Date
hewei 91322dc642 [maven-release-plugin] prepare release V1.0.17 2017-08-17 13:54:41 +08:00
hewei f0867171ff V1.0.17 2017-08-17 13:52:17 +08:00
hewei e4095dab7b bugfix[issues#11]:修复逻辑删除插件的常量命名 2017-08-17 13:30:18 +08:00
hewei c485d7beb1 统一命名格式 2017-08-17 11:04:42 +08:00
hewei 03116e71c0 测试工具驼峰命名config 2017-08-14 18:59:26 +08:00
hewei 03f79f62a1 [maven-release-plugin] prepare for next development iteration 2017-08-07 13:18:53 +08:00
15 changed files with 197 additions and 97 deletions
+9 -5
View File
@@ -27,7 +27,7 @@ Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.16</version>
<version>1.0.17</version>
</dependency>
```
---------------------------------------
@@ -350,7 +350,7 @@ public class Test {
因为很多实际项目数据都不允许物理删除,多采用逻辑删除,所以单独为逻辑删除做了一个插件,方便使用。
- 增加logicalDeleteByExample和logicalDeleteByPrimaryKey方法;
- 查询构造工具中增加逻辑删除条件andDeleted(boolean);
- 增加逻辑删除常量DEL_FLAG_OFF(已删除)、DEL_FLAG_ON(未删除);
- 增加逻辑删除常量IS_DELETED(已删除 默认值)、NOT_DELETED(未删除 默认值)([[issues#11]](https://github.com/itfsw/mybatis-generator-plugin/issues/11));
插件:
```xml
@@ -364,6 +364,10 @@ public class Test {
<property name="logicalDeleteValue" value="9"/>
<!-- 逻辑删除-未删除值 -->
<property name="logicalUnDeleteValue" value="0"/>
<!-- 逻辑删除常量名称,不配置默认为 IS_DELETED -->
<property name="logicalDeleteValue" value="DEL"/>
<!-- 逻辑删除常量(未删除)名称,不配置默认为 NOT_DELETED -->
<property name="logicalUnDeleteValue" value="UN_DEL"/>
</plugin>
<table tableName="tb">
@@ -397,14 +401,14 @@ public class Test {
// 新增了一个andDeleted方法过滤逻辑删除数据
.andDeleted(true)
// 当然也可直接使用逻辑删除列的查询方法,我们数据Model中定义了一个逻辑删除常量DEL_FLAG
.andDelFlagEqualTo(Tb.DEL_FLAG)
.andDelFlagEqualTo(Tb.IS_DELETED)
.example()
);
// 4. 逻辑删除和未删除常量
Tb tb = new Tb.Builder()
.delFlag(Tb.DEL_FLAG_OFF) // 删除
.delFlag(Tb.DEL_FLAG_ON) // 未删除
.delFlag(Tb.IS_DELETED) // 删除
.delFlag(Tb.NOT_DELETED) // 未删除
.build();
}
}
+2 -2
View File
@@ -3,7 +3,7 @@
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.16</version>
<version>1.0.17</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
@@ -212,7 +212,7 @@
<connection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</connection>
<developerConnection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</developerConnection>
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
<tag>V1.0.16</tag>
<tag>V1.0.17</tag>
</scm>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -29,7 +29,7 @@ import java.util.Properties;
* ---------------------------------------------------------------------------
*/
public class CommentPlugin extends BasePlugin {
public static final String PRE_TEMPLATE = "template"; // 模板 property
public static final String PRO_TEMPLATE = "template"; // 模板 property
/**
* 插件具体实现查看BasePlugin
@@ -35,7 +35,7 @@ import java.util.Properties;
* ---------------------------------------------------------------------------
*/
public class ExampleTargetPlugin extends BasePlugin {
public static final String TARGET_PACKAGE_KEY = "targetPackage"; // 配置targetPackage名
public static final String PRO_TARGET_PACKAGE = "targetPackage"; // 配置targetPackage名
private static String targetPackage; // 目标包
/**
@@ -45,7 +45,7 @@ public class ExampleTargetPlugin extends BasePlugin {
public boolean validate(List<String> warnings) {
// 获取配置的目标package
Properties properties = getProperties();
this.targetPackage = properties.getProperty(TARGET_PACKAGE_KEY);
this.targetPackage = properties.getProperty(PRO_TARGET_PACKAGE);
if (this.targetPackage == null){
warnings.add("请配置com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin插件的目标包名(targetPackage)!");
return false;
@@ -36,7 +36,7 @@ import java.util.List;
* ---------------------------------------------------------------------------
*/
public class IncrementsPlugin extends BasePlugin {
public static final String PRE_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
public static final String PRO_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
public static final String FIELD_INC_MAP = "incrementsColumnsInfoMap"; // 为了防止和用户数据库字段冲突,特殊命名
public static final String METHOD_INC_CHECK = "hasIncsForColumn"; // inc 检查方法名称
private IncrementsPluginTools incTools; // 增量插件工具
@@ -43,23 +43,26 @@ public class LogicalDeletePlugin extends BasePlugin {
public static final String METHOD_LOGICAL_DELETE_BY_EXAMPLE = "logicalDeleteByExample"; // 方法名
public static final String METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY = "logicalDeleteByPrimaryKey"; // 方法名
public static final String LOGICAL_DELETE_COLUMN_KEY = "logicalDeleteColumn"; // 逻辑删除列-Key
public static final String LOGICAL_DELETE_VALUE_KEY = "logicalDeleteValue"; // 逻辑删除值-Key
public static final String LOGICAL_UN_DELETE_VALUE_KEY = "logicalUnDeleteValue"; // 逻辑删除未删除值-Key
public static final String PRO_LOGICAL_DELETE_COLUMN = "logicalDeleteColumn"; // 逻辑删除列-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 DEL_FLAG_NAME = "DEL_FLAG_OFF"; // 逻辑删除标志位常量名称
public static final String UN_DEL_FLAG_NAME = "DEL_FLAG_ON"; // 逻辑删除标志位常量名称(未删除)
public static final String PRO_LOGICAL_DELETE_CONST_NAME = "logicalDeleteConstName"; // 逻辑删除常量
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"; // 逻辑删除查询方法
private IntrospectedColumn logicalDeleteColumn; // 逻辑删除列
private String logicalDeleteValue; // 逻辑删除值
private String logicalUnDeleteValue; // 逻辑删除值(未删除)
private String logicalDeleteConstName; // 逻辑删除常量
private String logicalUnDeleteConstName; // 逻辑删除常量(未删除)
/**
* 初始化阶段
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param introspectedTable
* @return
*/
@@ -67,24 +70,24 @@ public class LogicalDeletePlugin extends BasePlugin {
public void initialized(IntrospectedTable introspectedTable) {
// 1. 首先获取全局配置
Properties properties = getProperties();
String logicalDeleteColumn = properties.getProperty(LOGICAL_DELETE_COLUMN_KEY);
this.logicalDeleteValue = properties.getProperty(LOGICAL_DELETE_VALUE_KEY);
this.logicalUnDeleteValue = properties.getProperty(LOGICAL_UN_DELETE_VALUE_KEY);
String logicalDeleteColumn = properties.getProperty(PRO_LOGICAL_DELETE_COLUMN);
this.logicalDeleteValue = properties.getProperty(PRO_LOGICAL_DELETE_VALUE);
this.logicalUnDeleteValue = properties.getProperty(PRO_LOGICAL_UN_DELETE_VALUE);
// 2. 获取表单独配置,如果有则覆盖全局配置
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null){
logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY);
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) != null) {
logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN);
}
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY) != null){
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY);
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE) != null) {
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE);
}
if (introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY) != null){
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY);
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE) != null) {
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE);
}
// 3. 判断该表是否存在逻辑删除列
this.logicalDeleteColumn = null;
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
for (IntrospectedColumn column : columns) {
if (column.getActualColumnName().equalsIgnoreCase(logicalDeleteColumn)){
if (column.getActualColumnName().equalsIgnoreCase(logicalDeleteColumn)) {
// 判断字段类型
JDBCType type = JDBCType.valueOf(column.getJdbcType());
if (JDBCType.BIGINT == type
@@ -102,28 +105,31 @@ public class LogicalDeletePlugin extends BasePlugin {
|| JDBCType.NVARCHAR == type
|| JDBCType.SMALLINT == type
|| JDBCType.TINYINT == type
|| JDBCType.VARCHAR == type){
|| JDBCType.VARCHAR == type) {
this.logicalDeleteColumn = column;
} else {
warnings.add("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")的类型不在支持范围(请使用数字列,字符串列,布尔列)!");
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "逻辑删除列(" + introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) + ")的类型不在支持范围(请使用数字列,字符串列,布尔列)!");
}
}
}
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null && this.logicalDeleteColumn == null){
warnings.add("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")!");
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) != null && this.logicalDeleteColumn == null) {
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "没有找到您配置的逻辑删除列(" + introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) + ")!");
}
// 4. 判断逻辑删除值是否配置了
if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)){
warnings.add("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)) {
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 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param interfaze
* @param topLevelClass
* @param introspectedTable
@@ -131,7 +137,7 @@ public class LogicalDeletePlugin extends BasePlugin {
*/
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (this.logicalDeleteColumn != null){
if (this.logicalDeleteColumn != null) {
// 1. 逻辑删除ByExample
Method mLogicalDeleteByExample = JavaElementGeneratorTools.generateMethod(
METHOD_LOGICAL_DELETE_BY_EXAMPLE,
@@ -144,10 +150,10 @@ public class LogicalDeletePlugin extends BasePlugin {
commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
// interface 增加方法
interfaze.addMethod(mLogicalDeleteByExample);
logger.debug("itfsw(逻辑删除插件):"+interfaze.getType().getShortName()+"增加方法logicalDeleteByExample。");
logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByExample。");
// 2. 判断是否有主键,生成主键删除方法
if (introspectedTable.hasPrimaryKeyColumns()){
if (introspectedTable.hasPrimaryKeyColumns()) {
// 1. 逻辑删除ByExample
Method mLogicalDeleteByPrimaryKey = new Method(METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY);
// 返回值类型
@@ -190,7 +196,7 @@ public class LogicalDeletePlugin extends BasePlugin {
// interface 增加方法
interfaze.addImportedTypes(importedTypes);
interfaze.addMethod(mLogicalDeleteByPrimaryKey);
logger.debug("itfsw(逻辑删除插件):"+interfaze.getType().getShortName()+"增加方法logicalDeleteByPrimaryKey。");
logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByPrimaryKey。");
}
}
return true;
@@ -199,14 +205,13 @@ public class LogicalDeletePlugin extends BasePlugin {
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param document
* @param introspectedTable
* @return
*/
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
if (this.logicalDeleteColumn != null){
if (this.logicalDeleteColumn != null) {
// 1. 逻辑删除ByExample
XmlElement logicalDeleteByExample = new XmlElement("update"); //$NON-NLS-1$
logicalDeleteByExample.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_EXAMPLE));
@@ -223,14 +228,14 @@ public class LogicalDeletePlugin extends BasePlugin {
// 判断字段类型
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");
} else if (JDBCType.CHAR == type
|| JDBCType.LONGNVARCHAR == type
|| JDBCType.LONGVARCHAR == type
|| JDBCType.NCHAR == type
|| JDBCType.NVARCHAR == type
|| JDBCType.VARCHAR == type){
|| JDBCType.VARCHAR == type) {
sb.append("'");
sb.append(this.logicalDeleteValue);
sb.append("'");
@@ -242,10 +247,10 @@ public class LogicalDeletePlugin extends BasePlugin {
logicalDeleteByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
document.getRootElement().addElement(logicalDeleteByExample);
logger.debug("itfsw(逻辑删除插件):"+introspectedTable.getMyBatis3XmlMapperFileName()+"增加方法logicalDeleteByExample的实现。");
logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByExample的实现。");
// 2. 判断是否有主键,生成主键删除方法
if (introspectedTable.hasPrimaryKeyColumns()){
if (introspectedTable.hasPrimaryKeyColumns()) {
XmlElement logicalDeleteByPrimaryKey = new XmlElement("update"); //$NON-NLS-1$
logicalDeleteByPrimaryKey.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY));
@@ -274,14 +279,14 @@ public class LogicalDeletePlugin extends BasePlugin {
// 判断字段类型
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");
} else if (JDBCType.CHAR == type1
|| JDBCType.LONGNVARCHAR == type1
|| JDBCType.LONGVARCHAR == type1
|| JDBCType.NCHAR == type1
|| JDBCType.NVARCHAR == type1
|| JDBCType.VARCHAR == type1){
|| JDBCType.VARCHAR == type1) {
sb1.append("'");
sb1.append(this.logicalDeleteValue);
sb1.append("'");
@@ -308,7 +313,7 @@ public class LogicalDeletePlugin extends BasePlugin {
}
document.getRootElement().addElement(logicalDeleteByPrimaryKey);
logger.debug("itfsw(逻辑删除插件):"+introspectedTable.getMyBatis3XmlMapperFileName()+"增加方法logicalDeleteByPrimaryKey的实现。");
logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByPrimaryKey的实现。");
}
}
return true;
@@ -317,50 +322,61 @@ public class LogicalDeletePlugin extends BasePlugin {
/**
* Model 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (this.logicalDeleteColumn != null){
if (this.logicalDeleteColumn != null) {
ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields();
// 添加删除标志位常量 DEL_FLAG_OFF
String delFlagOnValue;
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){
delFlagOnValue = "null";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){
delFlagOnValue = "\"" + this.logicalDeleteValue + "\"";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){
delFlagOnValue = (this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
// 添加删除标志位常量
String logicalDeleteValue;
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)) {
logicalDeleteValue = "null";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")) {
logicalDeleteValue = "\"" + this.logicalDeleteValue + "\"";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")) {
logicalDeleteValue = (this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
} else {
delFlagOnValue = this.logicalDeleteValue;
logicalDeleteValue = 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);
// 常量插入到第一位
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
String unDelFlagOnValue;
if (this.logicalUnDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalUnDeleteValue)){
unDelFlagOnValue = "null";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){
unDelFlagOnValue = "\"" + this.logicalUnDeleteValue + "\"";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){
unDelFlagOnValue = (this.logicalUnDeleteValue.equals("1") || this.logicalUnDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
String logicalUnDeleteValue;
if (this.logicalUnDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalUnDeleteValue)) {
logicalUnDeleteValue = "null";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")) {
logicalUnDeleteValue = "\"" + this.logicalUnDeleteValue + "\"";
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")) {
logicalUnDeleteValue = (this.logicalUnDeleteValue.equals("1") || this.logicalUnDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
} else {
unDelFlagOnValue = this.logicalUnDeleteValue;
logicalUnDeleteValue = 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);
// 常量插入到第一位
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;
}
@@ -368,14 +384,13 @@ public class LogicalDeletePlugin extends BasePlugin {
/**
* ModelExample Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (this.logicalDeleteColumn != null){
if (this.logicalDeleteColumn != null) {
// 引入 Model类
FullyQualifiedJavaType model = introspectedTable.getRules().calculateAllFieldsClass();
topLevelClass.addImportedType(model);
@@ -402,7 +417,7 @@ public class LogicalDeletePlugin extends BasePlugin {
equalToMethodName.insert(0, "and"); //$NON-NLS-1$
equalToMethodName.append("EqualTo");
sb.append(equalToMethodName);
sb.append("("+modelName + "." + DEL_FLAG_NAME + ")");
sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")");
sb.append(" : ");
@@ -413,14 +428,14 @@ public class LogicalDeletePlugin extends BasePlugin {
notEqualToMethodName.insert(0, "and"); //$NON-NLS-1$
notEqualToMethodName.append("NotEqualTo");
sb.append(notEqualToMethodName);
sb.append("("+modelName + "." + DEL_FLAG_NAME + ")");
sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")");
sb.append(";");
method.addBodyLine(sb.toString());
innerClass.addMethod(method);
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"增加andDeleted方法。");
logger.debug("itfsw(逻辑删除插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加andDeleted方法。");
}
}
}
@@ -33,7 +33,7 @@ import java.util.List;
*/
public class TablePrefixPlugin extends BasePlugin {
public static final String PRE_PREFIX = "prefix"; // 前缀 property
public static final String PRO_PREFIX = "prefix"; // 前缀 property
private String prefix; // 前缀
/**
@@ -62,14 +62,14 @@ public class TablePrefixPlugin extends BasePlugin {
@Override
public void initialized(IntrospectedTable introspectedTable) {
// 1. 首先获取全局配置
this.prefix = getProperties().getProperty(PRE_PREFIX);
this.prefix = getProperties().getProperty(PRO_PREFIX);
// 2. 获取每个table 具体的
if (introspectedTable.getTableConfigurationProperty(PRE_PREFIX) != null) {
this.prefix = introspectedTable.getTableConfigurationProperty(PRE_PREFIX);
if (introspectedTable.getTableConfigurationProperty(PRO_PREFIX) != null) {
this.prefix = introspectedTable.getTableConfigurationProperty(PRO_PREFIX);
}
// 3. 判断是否配置了前缀
// !!! TableRenamePlugin 插件的 tableOverride 优先级最高
if (this.prefix != null && introspectedTable.getTableConfigurationProperty(TableRenamePlugin.PRE_TABLE_OVERRIDE) == null) {
if (this.prefix != null && introspectedTable.getTableConfigurationProperty(TableRenamePlugin.PRO_TABLE_OVERRIDE) == null) {
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
domainObjectName = prefix + domainObjectName;
try {
@@ -34,9 +34,9 @@ import java.util.regex.Pattern;
* ---------------------------------------------------------------------------
*/
public class TableRenamePlugin extends BasePlugin {
public static final String PRE_SEARCH_STRING = "searchString"; // 查找 property
public static final String PRE_REPLACE_STRING = "replaceString"; // 替换 property
public static final String PRE_TABLE_OVERRIDE = "tableOverride"; // table 重命名 property
public static final String PRO_SEARCH_STRING = "searchString"; // 查找 property
public static final String PRO_REPLACE_STRING = "replaceString"; // 替换 property
public static final String PRO_TABLE_OVERRIDE = "tableOverride"; // table 重命名 property
/**
* {@inheritDoc}
@@ -45,8 +45,8 @@ public class TableRenamePlugin extends BasePlugin {
public boolean validate(List<String> warnings) {
// 如果配置了searchString 或者 replaceString,二者不允许单独存在
if ((getProperties().getProperty(PRE_SEARCH_STRING) == null && getProperties().getProperty(PRE_REPLACE_STRING) != null)
|| (getProperties().getProperty(PRE_SEARCH_STRING) != null && getProperties().getProperty(PRE_REPLACE_STRING) == null)) {
if ((getProperties().getProperty(PRO_SEARCH_STRING) == null && getProperties().getProperty(PRO_REPLACE_STRING) != null)
|| (getProperties().getProperty(PRO_SEARCH_STRING) != null && getProperties().getProperty(PRO_REPLACE_STRING) == null)) {
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件的searchString、replaceString属性需配合使用,不能单独存在!");
return false;
}
@@ -71,16 +71,16 @@ public class TableRenamePlugin extends BasePlugin {
@Override
public void initialized(IntrospectedTable introspectedTable) {
// 1. 获取表单独配置
if (introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE) != null) {
String override = introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE);
if (introspectedTable.getTableConfigurationProperty(PRO_TABLE_OVERRIDE) != null) {
String override = introspectedTable.getTableConfigurationProperty(PRO_TABLE_OVERRIDE);
try {
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), override);
} catch (Exception e) {
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用tableOverride替换时异常!", e);
}
} else if (getProperties().getProperty(PRE_SEARCH_STRING) != null) {
String searchString = getProperties().getProperty(PRE_SEARCH_STRING);
String replaceString = getProperties().getProperty(PRE_REPLACE_STRING);
} else if (getProperties().getProperty(PRO_SEARCH_STRING) != null) {
String searchString = getProperties().getProperty(PRO_SEARCH_STRING);
String replaceString = getProperties().getProperty(PRO_REPLACE_STRING);
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
Pattern pattern = Pattern.compile(searchString);
@@ -46,7 +46,7 @@ public class UpsertPlugin extends BasePlugin {
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE = "upsertByExampleSelective"; // 方法名
public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
public static final String PRO_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
private boolean allowMultiQueries = false; // 是否允许多sql提交
/**
@@ -63,7 +63,7 @@ public class UpsertPlugin extends BasePlugin {
// 插件是否开启了多sql提交
Properties properties = this.getProperties();
String allowMultiQueries = properties.getProperty(PRE_ALLOW_MULTI_QUERIES);
String allowMultiQueries = properties.getProperty(PRO_ALLOW_MULTI_QUERIES);
this.allowMultiQueries = allowMultiQueries == null ? false : StringUtility.isTrue(allowMultiQueries);
if (this.allowMultiQueries) {
// 提示用户注意信息
@@ -56,7 +56,7 @@ public class BasePlugin extends PluginAdapter {
// 配置插件使用的模板引擎
PluginConfiguration cfg = PluginTools.getPluginConfiguration(context, CommentPlugin.class);
if (cfg == null || cfg.getProperty(CommentPlugin.PRE_TEMPLATE) == null){
if (cfg == null || cfg.getProperty(CommentPlugin.PRO_TEMPLATE) == null){
if (context.getCommentGenerator() instanceof DefaultCommentGenerator){
// 使用默认模板引擎
commentGenerator = new TemplateCommentGenerator("default-comment.ftl", true);
@@ -65,7 +65,7 @@ public class BasePlugin extends PluginAdapter {
commentGenerator = context.getCommentGenerator();
}
} else {
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRE_TEMPLATE), false);
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRO_TEMPLATE), false);
// ITFSW 插件使用的注释生成器
commentGenerator = templateCommentGenerator;
@@ -69,7 +69,7 @@ public class IncrementsPluginTools {
IncrementsPluginTools tools = new IncrementsPluginTools(context, introspectedTable);
// 判断是否启用了插件
if (PluginTools.getPluginConfiguration(context, IncrementsPlugin.class) != null) {
String incrementsColumns = introspectedTable.getTableConfigurationProperty(IncrementsPlugin.PRE_INCREMENTS_COLUMNS);
String incrementsColumns = introspectedTable.getTableConfigurationProperty(IncrementsPlugin.PRO_INCREMENTS_COLUMNS);
if (StringUtility.stringHasValue(incrementsColumns)) {
// 切分
String[] incrementsColumnsStrs = incrementsColumns.split(",");
@@ -71,12 +71,12 @@ public class IntrospectedTableTools {
// 注意!! 如果配置了ExampleTargetPlugin插件,要修正Example 位置
PluginConfiguration configuration = PluginTools.getPluginConfiguration(context, ExampleTargetPlugin.class);
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY) != null) {
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.PRO_TARGET_PACKAGE) != null) {
String exampleType = introspectedTable.getExampleType();
// 修改包名
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration();
String targetPackage = javaModelGeneratorConfiguration.getTargetPackage();
String newExampleType = exampleType.replace(targetPackage, configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY));
String newExampleType = exampleType.replace(targetPackage, configuration.getProperty(ExampleTargetPlugin.PRO_TARGET_PACKAGE));
introspectedTable.setExampleType(newExampleType);
}
@@ -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", 3l);
// 验证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);
}
});
}
}
@@ -137,6 +137,7 @@ public class MyBatisGeneratorTool {
public SqlSession getSqlSession() throws IOException, ClassNotFoundException {
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
config.setCallSettersOnNulls(true); // 设计null调用setter方法
config.setMapUnderscoreToCamelCase(true); // 驼峰命名支持
// 设置mapper
config.addMappers(targetPackage);
@@ -0,0 +1,53 @@
<?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>