diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java index 8486536..1d62d07 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java @@ -17,6 +17,7 @@ package com.itfsw.mybatis.generator.plugins; import com.itfsw.mybatis.generator.plugins.utils.CommentTools; +import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools; import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; @@ -50,13 +51,16 @@ public class LogicalDeletePlugin extends PluginAdapter { 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 DEL_FLAG_NAME = "DEL_FLAG"; // 逻辑删除标志位常量名称 + 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 METHOD_LOGICAL_DELETE = "andDeleted"; // 逻辑删除查询方法 private IntrospectedColumn logicalDeleteColumn; // 逻辑删除列 private String logicalDeleteValue; // 逻辑删除值 + private String logicalUnDeleteValue; // 逻辑删除值(未删除) /** * {@inheritDoc} @@ -84,6 +88,7 @@ public class LogicalDeletePlugin extends PluginAdapter { 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); // 2. 获取表单独配置,如果有则覆盖全局配置 if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null){ logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY); @@ -91,6 +96,9 @@ public class LogicalDeletePlugin extends PluginAdapter { if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY) != null){ this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY); } + if (introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY) != null){ + this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY); + } // 3. 判断该表是否存在逻辑删除列 this.logicalDeleteColumn = null; List columns = introspectedTable.getAllColumns(); @@ -124,6 +132,11 @@ public class LogicalDeletePlugin extends PluginAdapter { if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null && this.logicalDeleteColumn == null){ logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")!"); } + + // 4. 判断逻辑删除值是否配置了 + if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)){ + logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!"); + } } /** @@ -330,27 +343,47 @@ public class LogicalDeletePlugin extends PluginAdapter { @Override public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (this.logicalDeleteColumn != null){ - // 添加删除标志位常量 - Field field = new Field(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType()); - field.setVisibility(JavaVisibility.PUBLIC); - field.setStatic(true); - field.setFinal(true); - if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){ - field.setInitializationString("null"); - } else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){ - field.setInitializationString("\"" + this.logicalDeleteValue + "\""); - } else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){ - field.setInitializationString((this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false"); - } else { - field.setInitializationString(this.logicalDeleteValue); - } - CommentTools.addFieldComment(field, introspectedTable); - - // 常量插入到第一位 ArrayList fields = (ArrayList) topLevelClass.getFields(); - fields.add(0, field); + + // TODO 过期的 + Field field2 = JavaElementGeneratorTools.generateStaticFinalField("DEL_FLAG", this.logicalDeleteColumn.getFullyQualifiedJavaType(), DEL_FLAG_NAME, introspectedTable); + field2.addAnnotation("@Deprecated"); + // 常量插入到第一位 + fields.add(0, field2); logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG的常量。"); + + // 添加删除标志位常量 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"; + } else { + delFlagOnValue = this.logicalDeleteValue; + } + Field field = JavaElementGeneratorTools.generateStaticFinalField(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), delFlagOnValue, introspectedTable); + // 常量插入到第一位 + fields.add(0, field); + logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_OFF的常量。"); + + // 添加删除标志位常量 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"; + } else { + unDelFlagOnValue = this.logicalUnDeleteValue; + } + Field field1 = JavaElementGeneratorTools.generateStaticFinalField(UN_DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), unDelFlagOnValue, introspectedTable); + // 常量插入到第一位 + fields.add(0, field1); + logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_ON的常量。"); } return true; } diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/JavaElementGeneratorTools.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/JavaElementGeneratorTools.java new file mode 100644 index 0000000..38a5203 --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/JavaElementGeneratorTools.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.itfsw.mybatis.generator.plugins.utils; + +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.Field; +import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; +import org.mybatis.generator.api.dom.java.JavaVisibility; + +/** + * --------------------------------------------------------------------------- + * Java ele 生成工具 + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2017/4/21 16:22 + * --------------------------------------------------------------------------- + */ +public class JavaElementGeneratorTools { + public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){ + Field field = new Field(fieldName, javaType); + CommentTools.addFieldComment(field, introspectedTable); + field.setVisibility(JavaVisibility.PUBLIC); + field.setStatic(true); + field.setFinal(true); + if (initString != null){ + field.setInitializationString(initString); + } + return field; + } +}