Commit ab702538 authored by hewei's avatar hewei

重构代码插件依赖关系

parent 23cf1069
......@@ -247,10 +247,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
* @return
*/
@Override
public List<Element> incrementElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
public List<Element> incrementSetElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
List<Element> list = new ArrayList<>();
if (incTools.supportColumn(introspectedColumn)){
if (incTools.supportColumn(introspectedColumn)) {
// 1. column = 节点
list.add(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = "));
......@@ -289,6 +289,43 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
return list;
}
/**
* 生成增量操作节点(SelectiveEnhancedPlugin)
* @return
*/
@Override
public Element incrementSetsWithSelectiveEnhancedPluginElementGenerated() {
if (incTools.support()) {
XmlElement choose = new XmlElement("choose");
for (IntrospectedColumn introspectedColumn : incTools.getColumns()) {
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);
return choose;
}
return null;
}
// =================================================== 原生方法的支持 ====================================================
/**
......@@ -310,7 +347,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
String columnName = strs[0].trim();
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
// 查找是否需要进行增量操作
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, hasPrefix ? "record." : null, true);
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, hasPrefix ? "record." : null, true);
if (!incrementEles.isEmpty()) {
xmlElement.getElements().clear();
xmlElement.getElements().addAll(incrementEles);
......@@ -340,7 +377,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
String columnName = text.split("=")[0].trim();
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
// 查找判断是否需要进行节点替换
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, hasPrefix ? "record." : null, text.endsWith(","));
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, hasPrefix ? "record." : null, text.endsWith(","));
if (!incrementEles.isEmpty()) {
newEles.addAll(incrementEles);
......
......@@ -19,6 +19,7 @@ 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 com.itfsw.mybatis.generator.plugins.utils.hook.IIncrementsPluginHook;
import com.itfsw.mybatis.generator.plugins.utils.hook.IUpsertPluginHook;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
......@@ -27,6 +28,7 @@ import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
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.ListUtilities;
......@@ -253,7 +255,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
// selective
answer.addElement(new TextElement("SET"));
answer.addElement(this.generateSetsSelective(ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns())));
answer.addElement(this.generateSetsSelective(ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())));
XmlElementGeneratorTools.generateWhereByPrimaryKeyTo(answer, introspectedTable.getPrimaryKeyColumns(), "record.");
......@@ -445,7 +447,14 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
setForeachEle.addAttribute(new Attribute("collection", "selective"));
setForeachEle.addAttribute(new Attribute("item", "column"));
setForeachEle.addAttribute(new Attribute("separator", ","));
Element incrementEle = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetsWithSelectiveEnhancedPluginElementGenerated();
if (incrementEle == null) {
setForeachEle.addElement(new TextElement("${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"));
} else {
setForeachEle.addElement(incrementEle);
}
XmlElement setOtherwiseEle = new XmlElement("otherwise");
setOtherwiseEle.addElement(XmlElementGeneratorTools.generateSetsSelective(columns, "record."));
......
......@@ -383,8 +383,8 @@ public class XmlElementGeneratorTools {
switch (type) {
case 3:
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, prefix, columnIterator.hasNext());
if (incrementEles.isEmpty()) {
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, prefix, false);
if (!incrementEles.isEmpty()) {
// 增量插件支持
if (sb.length() > 0) {
list.add(new TextElement(sb.toString()));
......@@ -498,8 +498,8 @@ public class XmlElementGeneratorTools {
private static void generateSelectiveCommColumnTo(XmlElement element, IntrospectedColumn introspectedColumn, String prefix, int type) {
switch (type) {
case 3:
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, prefix, true);
if (incrementEles.isEmpty()) {
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, prefix, true);
if (!incrementEles.isEmpty()) {
// 增量插件支持
for (Element ele : incrementEles) {
element.addElement(ele);
......
......@@ -97,11 +97,20 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
// ============================================= IIncrementsPluginHook ==============================================
@Override
public List<Element> incrementElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
public List<Element> incrementSetElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
if (this.getPlugins(IIncrementsPluginHook.class).isEmpty()) {
return new ArrayList<>();
} else {
return this.getPlugins(IIncrementsPluginHook.class).get(0).incrementElementGenerated(introspectedColumn, prefix, hasComma);
return this.getPlugins(IIncrementsPluginHook.class).get(0).incrementSetElementGenerated(introspectedColumn, prefix, hasComma);
}
}
@Override
public Element incrementSetsWithSelectiveEnhancedPluginElementGenerated() {
if (this.getPlugins(IIncrementsPluginHook.class).isEmpty()){
return null;
} else {
return this.getPlugins(IIncrementsPluginHook.class).get(0).incrementSetsWithSelectiveEnhancedPluginElementGenerated();
}
}
......
......@@ -37,5 +37,11 @@ public interface IIncrementsPluginHook {
* @param hasComma
* @return
*/
List<Element> incrementElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma);
List<Element> incrementSetElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma);
/**
* 生成增量操作节点(SelectiveEnhancedPlugin)
* @return
*/
Element incrementSetsWithSelectiveEnhancedPluginElementGenerated();
}
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