From 78e16adb02c32c7a78b0975eb84e28bd18fd203e Mon Sep 17 00:00:00 2001 From: hewei Date: Wed, 2 May 2018 10:11:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E4=BE=9D=E8=B5=96=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/plugins/IncrementsPlugin.java | 2 + .../plugins/utils/IncrementsPluginTools.java | 126 ------------------ .../utils/XmlElementGeneratorTools.java | 99 +++++++------- .../plugins/utils/hook/HookAggregator.java | 4 +- .../utils/hook/IIncrementsPluginHook.java | 28 ++++ 5 files changed, 80 insertions(+), 179 deletions(-) create mode 100644 src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IIncrementsPluginHook.java 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 8e14e05..9d1002e 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java @@ -234,6 +234,8 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH return true; } + // =================================================== 原生方法的支持 ==================================================== + /** * 有Selective代码生成 * @param element diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java index fea2cff..648052f 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java @@ -28,11 +28,8 @@ import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; import org.mybatis.generator.config.Context; import org.mybatis.generator.internal.util.StringUtility; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; /** @@ -44,8 +41,6 @@ import java.util.List; * --------------------------------------------------------------------------- */ public class IncrementsPluginTools { - private final static Logger logger = LoggerFactory.getLogger(IncrementsPluginTools.class); - private Context context; // 上下文 private IntrospectedTable introspectedTable; // 表 private List columns = new ArrayList<>(); // 表启用增量操作的字段 @@ -55,7 +50,6 @@ public class IncrementsPluginTools { * @param introspectedTable */ private IncrementsPluginTools(Context context, IntrospectedTable introspectedTable) { - this.context = context; this.introspectedTable = introspectedTable; } @@ -126,96 +120,8 @@ public class IncrementsPluginTools { return false; } - /** - * 生成sets Selective Ele - * @param columns - * @param prefix - * @return - */ - public XmlElement generateSetsSelective(List columns, String prefix) { - return generateSetsSelective(columns, prefix, false); - } - /** - * 生成sets Selective Ele - * @param columns - * @param prefix - * @param bracket - * @return - */ - public XmlElement generateSetsSelective(List columns, String prefix, boolean bracket) { - XmlElement eleTrim = new XmlElement("trim"); - if (bracket) { - eleTrim.addAttribute(new Attribute("prefix", "(")); - eleTrim.addAttribute(new Attribute("suffix", ")")); - eleTrim.addAttribute(new Attribute("suffixOverrides", ",")); - } else { - eleTrim.addAttribute(new Attribute("suffixOverrides", ",")); - } - for (IntrospectedColumn introspectedColumn : columns) { - XmlElement eleIf = new XmlElement("if"); - eleIf.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null")); - if (this.supportColumn(introspectedColumn)) { - for (Element ele : this.generatedIncrementsElement(introspectedColumn, prefix, true)) { - eleIf.addElement(ele); - } - } else { - eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); - } - eleTrim.addElement(eleIf); - } - - return eleTrim; - } - - /** - * 生成sets Ele - * @param columns - * @param prefix - * @return - */ - public List generateSets(List columns, String prefix) { - return generateSets(columns, prefix, false); - } - - /** - * 生成sets Ele - * @param columns - * @param prefix - * @param bracket - * @return - */ - public List generateSets(List columns, String prefix, boolean bracket) { - List list = new ArrayList<>(); - if (bracket) { - list.add(new TextElement("(")); - } - Iterator columnIterator = columns.iterator(); - while (columnIterator.hasNext()) { - IntrospectedColumn introspectedColumn = columnIterator.next(); - - if (this.supportColumn(introspectedColumn)) { - for (Element ele : this.generatedIncrementsElement(introspectedColumn, prefix, columnIterator.hasNext())) { - list.add(ele); - } - } else { - StringBuilder sb = new StringBuilder(); - sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)); - sb.append(" = "); - sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix)); - if (columnIterator.hasNext()) { - sb.append(", "); - } - list.add(new TextElement(sb.toString())); - } - } - if (bracket) { - list.add(new TextElement(")")); - } - - return list; - } /** * 生成增量操作节点 @@ -263,36 +169,4 @@ public class IncrementsPluginTools { return list; } - - /** - * 创建 sets (SelectiveEnhancedPlugin) - * @param setForeachEle - */ - public void generateSetsSelectiveWithSelectiveEnhancedPlugin(XmlElement setForeachEle) { - XmlElement choose = new XmlElement("choose"); - - for (IntrospectedColumn introspectedColumn : columns) { - XmlElement when = new XmlElement("when"); - - // 需要 inc 的列 - StringBuilder sb = new StringBuilder(); - sb.append("'"); - sb.append(introspectedColumn.getActualColumnName()); - sb.append("'.toString()"); - sb.append(" == "); - sb.append("column.value"); - - when.addAttribute(new Attribute("test", sb.toString())); - when.addElement(new TextElement("${column.value} = ${column.value} ${record.incrementsColumnsInfoMap." - + introspectedColumn.getActualColumnName() - + ".value} #{record.${column.javaProperty},jdbcType=${column.jdbcType}}")); - choose.addElement(when); - } - - XmlElement otherwise = new XmlElement("otherwise"); - otherwise.addElement(new TextElement("${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}")); - choose.addElement(otherwise); - - setForeachEle.addElement(choose); - } } 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 4198c7e..1e69f92 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 @@ -377,34 +377,17 @@ public class XmlElementGeneratorTools { List list = new ArrayList<>(); if (hasIdentityAndGeneratedAlwaysColumns(columns)) { - XmlElement eleTrim = new XmlElement("trim"); - if (bracket) { - eleTrim.addAttribute(new Attribute("prefix", "(")); - eleTrim.addAttribute(new Attribute("suffix", ")")); - eleTrim.addAttribute(new Attribute("suffixOverrides", ",")); - } else { - eleTrim.addAttribute(new Attribute("suffixOverrides", ",")); - } + XmlElement trimEle = generateTrim(bracket); for (IntrospectedColumn introspectedColumn : columns) { if (introspectedColumn.isGeneratedAlways() || introspectedColumn.isIdentity()) { - generateSelectiveToTrimEle(eleTrim, introspectedColumn, prefix, type); + generateSelectiveToTrimEleTo(trimEle, introspectedColumn, prefix, type); } else { - switch (type) { - case 3: - eleTrim.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); - break; - case 2: - eleTrim.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); - break; - case 1: - eleTrim.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ",")); - break; - } + generateSelectiveCommColumnTo(trimEle, introspectedColumn, prefix, type); } } - return Arrays.asList(eleTrim); + return Arrays.asList(trimEle); } else { StringBuilder sb = new StringBuilder(bracket ? "(" : ""); Iterator columnIterator = columns.iterator(); @@ -458,20 +441,28 @@ public class XmlElementGeneratorTools { * @return */ private static XmlElement generateCommColumnsSelective(List columns, String prefix, boolean bracket, int type) { - XmlElement eleTrim = new XmlElement("trim"); - if (bracket) { - eleTrim.addAttribute(new Attribute("prefix", "(")); - eleTrim.addAttribute(new Attribute("suffix", ")")); - eleTrim.addAttribute(new Attribute("suffixOverrides", ",")); - } else { - eleTrim.addAttribute(new Attribute("suffixOverrides", ",")); - } - + XmlElement trimEle = generateTrim(bracket); for (IntrospectedColumn introspectedColumn : columns) { - generateSelectiveToTrimEle(eleTrim, introspectedColumn, prefix, type); + generateSelectiveToTrimEleTo(trimEle, introspectedColumn, prefix, type); } + return trimEle; + } - return eleTrim; + /** + * trim 节点 + * @param bracket + * @return + */ + private static XmlElement generateTrim(boolean bracket){ + XmlElement trimEle = new XmlElement("trim"); + if (bracket) { + trimEle.addAttribute(new Attribute("prefix", "(")); + trimEle.addAttribute(new Attribute("suffix", ")")); + trimEle.addAttribute(new Attribute("suffixOverrides", ",")); + } else { + trimEle.addAttribute(new Attribute("suffixOverrides", ",")); + } + return trimEle; } /** @@ -481,41 +472,45 @@ public class XmlElementGeneratorTools { * @param prefix * @param type 1:key,2:value,3:set */ - private static void generateSelectiveToTrimEle(XmlElement trimEle, IntrospectedColumn introspectedColumn, String prefix, int type) { + private static void generateSelectiveToTrimEleTo(XmlElement trimEle, IntrospectedColumn introspectedColumn, String prefix, int type) { if (type != 3 && (introspectedColumn.isSequenceColumn() || introspectedColumn.getFullyQualifiedJavaType().isPrimitive())) { // if it is a sequence column, it is not optional // This is required for MyBatis3 because MyBatis3 parses // and calculates the SQL before executing the selectKey // if it is primitive, we cannot do a null check - switch (type) { - case 2: - trimEle.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); - break; - case 1: - trimEle.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ",")); - break; - } + generateSelectiveCommColumnTo(trimEle, introspectedColumn, prefix, type); } else { XmlElement eleIf = new XmlElement("if"); eleIf.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null")); - switch (type) { - case 3: - eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); - break; - case 2: - eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); - break; - case 1: - eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ",")); - break; - } + generateSelectiveCommColumnTo(eleIf, introspectedColumn, prefix, type); trimEle.addElement(eleIf); } } + /** + * 生成 + * @param element + * @param introspectedColumn + * @param prefix + * @param type 1:key,2:value,3:set + */ + private static void generateSelectiveCommColumnTo(XmlElement element, IntrospectedColumn introspectedColumn, String prefix, int type){ + switch (type) { + case 3: + element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); + break; + case 2: + element.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); + break; + case 1: + element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ",")); + break; + } + } + /** * 查找指定xml节点下指定节点名称的元素 * @param xmlElement diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java index 39cced6..213f430 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java @@ -40,7 +40,7 @@ import java.util.List; * @time:2018/4/27 11:33 * --------------------------------------------------------------------------- */ -public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHook { +public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHook, IIncrementsPluginHook { protected static final Logger logger = LoggerFactory.getLogger(BasePlugin.class); // 日志 private final static HookAggregator instance = new HookAggregator(); private Context context; @@ -93,6 +93,8 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo return list; } + // ============================================= IIncrementsPluginHook ============================================== + // ============================================ IModelBuilderPluginHook ============================================= @Override public boolean modelBuilderClassGenerated(TopLevelClass topLevelClass, InnerClass builderClass, List columns, IntrospectedTable introspectedTable) { diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IIncrementsPluginHook.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IIncrementsPluginHook.java new file mode 100644 index 0000000..9648cd0 --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IIncrementsPluginHook.java @@ -0,0 +1,28 @@ +/* + * 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; + +/** + * --------------------------------------------------------------------------- + * + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2018/4/28 17:50 + * --------------------------------------------------------------------------- + */ +public interface IIncrementsPluginHook { +}