Commit 78e16adb authored by hewei's avatar hewei

重构代码插件依赖关系

parent 9425e882
...@@ -234,6 +234,8 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH ...@@ -234,6 +234,8 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
return true; return true;
} }
// =================================================== 原生方法的支持 ====================================================
/** /**
* 有Selective代码生成 * 有Selective代码生成
* @param element * @param element
......
...@@ -28,11 +28,8 @@ import org.mybatis.generator.api.dom.xml.XmlElement; ...@@ -28,11 +28,8 @@ import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.config.Context; import org.mybatis.generator.config.Context;
import org.mybatis.generator.internal.util.StringUtility; import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
...@@ -44,8 +41,6 @@ import java.util.List; ...@@ -44,8 +41,6 @@ import java.util.List;
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class IncrementsPluginTools { public class IncrementsPluginTools {
private final static Logger logger = LoggerFactory.getLogger(IncrementsPluginTools.class);
private Context context; // 上下文
private IntrospectedTable introspectedTable; // 表 private IntrospectedTable introspectedTable; // 表
private List<IntrospectedColumn> columns = new ArrayList<>(); // 表启用增量操作的字段 private List<IntrospectedColumn> columns = new ArrayList<>(); // 表启用增量操作的字段
...@@ -55,7 +50,6 @@ public class IncrementsPluginTools { ...@@ -55,7 +50,6 @@ public class IncrementsPluginTools {
* @param introspectedTable * @param introspectedTable
*/ */
private IncrementsPluginTools(Context context, IntrospectedTable introspectedTable) { private IncrementsPluginTools(Context context, IntrospectedTable introspectedTable) {
this.context = context;
this.introspectedTable = introspectedTable; this.introspectedTable = introspectedTable;
} }
...@@ -126,96 +120,8 @@ public class IncrementsPluginTools { ...@@ -126,96 +120,8 @@ public class IncrementsPluginTools {
return false; return false;
} }
/**
* 生成sets Selective Ele
* @param columns
* @param prefix
* @return
*/
public XmlElement generateSetsSelective(List<IntrospectedColumn> columns, String prefix) {
return generateSetsSelective(columns, prefix, false);
}
/**
* 生成sets Selective Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public XmlElement generateSetsSelective(List<IntrospectedColumn> 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<Element> generateSets(List<IntrospectedColumn> columns, String prefix) {
return generateSets(columns, prefix, false);
}
/**
* 生成sets Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public List<Element> generateSets(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
List<Element> list = new ArrayList<>();
if (bracket) {
list.add(new TextElement("("));
}
Iterator<IntrospectedColumn> 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 { ...@@ -263,36 +169,4 @@ public class IncrementsPluginTools {
return list; 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);
}
} }
...@@ -377,34 +377,17 @@ public class XmlElementGeneratorTools { ...@@ -377,34 +377,17 @@ public class XmlElementGeneratorTools {
List<Element> list = new ArrayList<>(); List<Element> list = new ArrayList<>();
if (hasIdentityAndGeneratedAlwaysColumns(columns)) { if (hasIdentityAndGeneratedAlwaysColumns(columns)) {
XmlElement eleTrim = new XmlElement("trim"); XmlElement trimEle = generateTrim(bracket);
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) { for (IntrospectedColumn introspectedColumn : columns) {
if (introspectedColumn.isGeneratedAlways() || introspectedColumn.isIdentity()) { if (introspectedColumn.isGeneratedAlways() || introspectedColumn.isIdentity()) {
generateSelectiveToTrimEle(eleTrim, introspectedColumn, prefix, type); generateSelectiveToTrimEleTo(trimEle, introspectedColumn, prefix, type);
} else { } else {
switch (type) { generateSelectiveCommColumnTo(trimEle, introspectedColumn, prefix, 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;
}
} }
} }
return Arrays.asList(eleTrim); return Arrays.asList(trimEle);
} else { } else {
StringBuilder sb = new StringBuilder(bracket ? "(" : ""); StringBuilder sb = new StringBuilder(bracket ? "(" : "");
Iterator<IntrospectedColumn> columnIterator = columns.iterator(); Iterator<IntrospectedColumn> columnIterator = columns.iterator();
...@@ -458,20 +441,28 @@ public class XmlElementGeneratorTools { ...@@ -458,20 +441,28 @@ public class XmlElementGeneratorTools {
* @return * @return
*/ */
private static XmlElement generateCommColumnsSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) { private static XmlElement generateCommColumnsSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) {
XmlElement eleTrim = new XmlElement("trim"); XmlElement trimEle = generateTrim(bracket);
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) { 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 { ...@@ -481,41 +472,45 @@ public class XmlElementGeneratorTools {
* @param prefix * @param prefix
* @param type 1:key,2:value,3:set * @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 (type != 3 && (introspectedColumn.isSequenceColumn() || introspectedColumn.getFullyQualifiedJavaType().isPrimitive())) {
// if it is a sequence column, it is not optional // if it is a sequence column, it is not optional
// This is required for MyBatis3 because MyBatis3 parses // This is required for MyBatis3 because MyBatis3 parses
// and calculates the SQL before executing the selectKey // and calculates the SQL before executing the selectKey
// if it is primitive, we cannot do a null check // if it is primitive, we cannot do a null check
switch (type) { generateSelectiveCommColumnTo(trimEle, introspectedColumn, prefix, type);
case 2:
trimEle.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
break;
case 1:
trimEle.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ","));
break;
}
} else { } else {
XmlElement eleIf = new XmlElement("if"); XmlElement eleIf = new XmlElement("if");
eleIf.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null")); eleIf.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null"));
switch (type) { generateSelectiveCommColumnTo(eleIf, introspectedColumn, prefix, 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;
}
trimEle.addElement(eleIf); 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节点下指定节点名称的元素 * 查找指定xml节点下指定节点名称的元素
* @param xmlElement * @param xmlElement
......
...@@ -40,7 +40,7 @@ import java.util.List; ...@@ -40,7 +40,7 @@ import java.util.List;
* @time:2018/4/27 11:33 * @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); // 日志 protected static final Logger logger = LoggerFactory.getLogger(BasePlugin.class); // 日志
private final static HookAggregator instance = new HookAggregator(); private final static HookAggregator instance = new HookAggregator();
private Context context; private Context context;
...@@ -93,6 +93,8 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo ...@@ -93,6 +93,8 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
return list; return list;
} }
// ============================================= IIncrementsPluginHook ==============================================
// ============================================ IModelBuilderPluginHook ============================================= // ============================================ IModelBuilderPluginHook =============================================
@Override @Override
public boolean modelBuilderClassGenerated(TopLevelClass topLevelClass, InnerClass builderClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) { public boolean modelBuilderClassGenerated(TopLevelClass topLevelClass, InnerClass builderClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
......
/*
* 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 {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment