From 24368c58d13624a6303489c99656f7e99cef7dde Mon Sep 17 00:00:00 2001 From: hewei Date: Wed, 21 Jun 2017 14:20:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A2=9E=E9=87=8F=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/plugins/IncrementsPlugin.java | 258 +++++++++++++++++- .../generator/plugins/ModelBuilderPlugin.java | 34 ++- .../plugins/SelectiveEnhancedPlugin.java | 2 +- .../utils/XmlElementGeneratorTools.java | 22 ++ 4 files changed, 301 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java index ccb1865..4a39740 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java @@ -18,7 +18,18 @@ package com.itfsw.mybatis.generator.plugins; import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.PluginTools; +import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools; +import org.mybatis.generator.api.IntrospectedColumn; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.TopLevelClass; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.Element; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.XmlElement; +import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; +import org.mybatis.generator.internal.util.StringUtility; +import java.util.ArrayList; import java.util.List; /** @@ -31,9 +42,12 @@ import java.util.List; */ public class IncrementsPlugin extends BasePlugin { public static final String PRE_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property + private List columns; // 需要进行自增的字段 /** - * {@inheritDoc} + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param warnings + * @return */ @Override public boolean validate(List warnings) { @@ -46,4 +60,246 @@ public class IncrementsPlugin extends BasePlugin { return super.validate(warnings); } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param introspectedTable + */ + @Override + public void initialized(IntrospectedTable introspectedTable) { + this.columns = new ArrayList<>(); + // 获取表配置信息 + String incrementsColumns = introspectedTable.getTableConfigurationProperty(IncrementsPlugin.PRE_INCREMENTS_COLUMNS); + if (StringUtility.stringHasValue(incrementsColumns)) { + // 切分 + String[] incrementsColumnsStrs = incrementsColumns.split(","); + List columns = introspectedTable.getAllColumns(); + for (String incrementsColumnsStr : incrementsColumnsStrs) { + IntrospectedColumn column = introspectedTable.getColumn(incrementsColumnsStr); + if (column == null) { + logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件没有找到column为" + incrementsColumnsStr + "的字段!"); + } else if (columns.indexOf(column) != -1) { + this.columns.add(column); + } + } + } + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param topLevelClass + * @param introspectedTable + * @return + */ + @Override + public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + // 具体实现在 ModelBuilderPlugin.generateModelBuilder + return true; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param topLevelClass + * @param introspectedTable + * @return + */ + @Override + public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + // 具体实现在 ModelBuilderPlugin.generateModelBuilder + return true; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param element + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapUpdateByExampleSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + generatedWithSelective(element, introspectedTable, true); + return true; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param element + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapUpdateByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + generatedWithoutSelective(element, introspectedTable, true); + return true; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param element + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapUpdateByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + generatedWithoutSelective(element, introspectedTable, true); + return true; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param element + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapUpdateByPrimaryKeySelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + generatedWithSelective(element, introspectedTable, false); + return true; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param element + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + generatedWithoutSelective(element, introspectedTable, false); + return true; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param element + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + generatedWithoutSelective(element, introspectedTable, false); + return true; + } + + /** + * 是否需要替换 + * @param columnName + * @return + */ + private boolean needReplace(String columnName) { + for (IntrospectedColumn introspectedColumn : this.columns) { + if (introspectedColumn.getActualColumnName().equals(columnName)) { + return true; + } + } + return false; + } + + /** + * 有Selective代码生成 + * @param element + */ + private void generatedWithSelective(XmlElement element, IntrospectedTable introspectedTable, boolean hasPrefix) { + if (columns.size() > 0) { + // 查找 set->if->text + List sets = XmlElementGeneratorTools.findXmlElements(element, "set"); + if (sets.size() > 0) { + List ifs = XmlElementGeneratorTools.findXmlElements(sets.get(0), "if"); + if (ifs.size() > 0) { + for (XmlElement xmlElement : ifs) { + // 下面为if的text节点 + List textEles = xmlElement.getElements(); + TextElement textEle = (TextElement) textEles.get(0); + String[] strs = textEle.getContent().split("="); + String columnName = strs[0].trim(); + // 查找是否需要进行增量操作 + if (needReplace(columnName)) { + IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName); + xmlElement.getElements().clear(); + xmlElement.getElements().addAll(generatedIncrementsElement(xmlElement, introspectedColumn, hasPrefix, true)); + } + } + } + } + } + } + + /** + * 无Selective代码生成 + * @param xmlElement + * @param introspectedTable + * @param hasPrefix + */ + private void generatedWithoutSelective(XmlElement xmlElement, IntrospectedTable introspectedTable, boolean hasPrefix) { + if (columns.size() > 0) { + List newEles = new ArrayList<>(); + for (Element ele : xmlElement.getElements()) { + // 找到text节点且格式为 set xx = xx 或者 xx = xx + if (ele instanceof TextElement) { + String text = ((TextElement) ele).getContent().trim(); + if (text.matches("(set\\s)?\\S+\\s?=.*")) { + // 清理 set 操作 + text = text.replaceFirst("set\\s", "").trim(); + String columnName = text.split("=")[0].trim(); + + // 查找判断是否需要进行节点替换 + if (needReplace(columnName)) { + IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName); + newEles.addAll(generatedIncrementsElement(xmlElement, introspectedColumn, hasPrefix, text.endsWith(","))); + + continue; + } + } + } + newEles.add(ele); + } + + // 替换节点 + xmlElement.getElements().clear(); + xmlElement.getElements().addAll(newEles); + } + } + + /** + * 生成增量操作节点 + * @param element + * @param introspectedColumn + * @param hasPrefix + * @param hasComma + */ + private List generatedIncrementsElement(XmlElement element, IntrospectedColumn introspectedColumn, boolean hasPrefix, boolean hasComma) { + List list = new ArrayList<>(); + + // 1. column = 节点 + list.add(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = ")); + + // 2. 选择节点 + // 条件 + XmlElement choose = new XmlElement("choose"); + + // 没有启用增量操作 + XmlElement when = new XmlElement("when"); + when.addAttribute(new Attribute("test", (hasPrefix ? "record" : "_parameter") + ".incs.isEmpty()")); + TextElement normal = new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, hasPrefix ? "record." : null)); + when.addElement(normal); + choose.addElement(when); + + // 启用了增量操作 + XmlElement otherwise = new XmlElement("otherwise"); + TextElement spec = new TextElement( + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + + " ${" + (hasPrefix ? "record" : "_parameter") + ".incs." + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ".value} " + + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, hasPrefix ? "record." : null)); + otherwise.addElement(spec); + choose.addElement(otherwise); + + list.add(choose); + + // 3. 结尾逗号 + if (hasComma) { + list.add(new TextElement(",")); + } + + return list; + } } diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java index 80a5142..a15ae22 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java @@ -40,17 +40,6 @@ public class ModelBuilderPlugin extends BasePlugin { public static final String BUILDER_CLASS_NAME = "Builder"; // Builder 类名 private FullyQualifiedJavaType inc; // 是否支持Increments - /** - * 初始化阶段 - * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html - * @param introspectedTable - * @return - */ - @Override - public void initialized(IntrospectedTable introspectedTable) { - this.inc = null; - } - /** * Model Methods 生成 * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html @@ -153,8 +142,28 @@ public class ModelBuilderPlugin extends BasePlugin { InnerEnum eIncrements = new InnerEnum(new FullyQualifiedJavaType("Inc")); eIncrements.setVisibility(JavaVisibility.PUBLIC); eIncrements.setStatic(true); - eIncrements.addEnumConstant("INC,DEC"); + eIncrements.addEnumConstant("INC(\"+\")"); + eIncrements.addEnumConstant("DEC(\"-\")"); commentGenerator.addEnumComment(eIncrements, introspectedTable); + // 生成属性和构造函数 + Field fValue = new Field("value", FullyQualifiedJavaType.getStringInstance()); + fValue.setVisibility(JavaVisibility.PRIVATE); + fValue.setFinal(true); + commentGenerator.addFieldComment(fValue, introspectedTable); + eIncrements.addField(fValue); + + Method mInc = new Method("Inc"); + mInc.setConstructor(true); + mInc.addBodyLine("this.value = value;"); + mInc.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "value")); + commentGenerator.addGeneralMethodComment(mInc, introspectedTable); + eIncrements.addMethod(mInc); + logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加构造方法和column属性。"); + + Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue); + commentGenerator.addGeneralMethodComment(mValue, introspectedTable); + eIncrements.addMethod(mValue); + innerClass.addInnerEnum(eIncrements); // 增加field Field fIncrements = JavaElementGeneratorTools.generateField( @@ -208,5 +217,4 @@ public class ModelBuilderPlugin extends BasePlugin { return innerClass; } - } diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java index 9356be1..c1c0d39 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java @@ -228,7 +228,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin { } } else { String columnName; - if (text.matches(".*\\s*=.*")){ + if (text.matches(".*=.*")){ columnName = text.split("=")[0]; } else { columnName = text.replaceAll(",", ""); diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java index 4bcbadc..94f95ad 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java @@ -25,6 +25,7 @@ import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; import org.mybatis.generator.config.GeneratedKey; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -377,4 +378,25 @@ public class XmlElementGeneratorTools { return eleTrim; } + /** + * 查找指定xml节点下指定节点名称的元素 + * + * @param xmlElement + * @param name + * @return + */ + public static List findXmlElements(XmlElement xmlElement, String name){ + List list = new ArrayList<>(); + List elements = xmlElement.getElements(); + for (Element ele : elements) { + if (ele instanceof XmlElement) { + XmlElement xmlElement1 = (XmlElement) ele; + if (name.equalsIgnoreCase(xmlElement1.getName())) { + list.add(xmlElement1); + } + } + } + return list; + } + }