重构代码插件依赖关系
This commit is contained in:
@@ -234,6 +234,8 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
|
||||
return true;
|
||||
}
|
||||
|
||||
// =================================================== 原生方法的支持 ====================================================
|
||||
|
||||
/**
|
||||
* 有Selective代码生成
|
||||
* @param element
|
||||
|
||||
@@ -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<IntrospectedColumn> 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<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 {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
+47
-52
@@ -377,34 +377,17 @@ public class XmlElementGeneratorTools {
|
||||
List<Element> 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<IntrospectedColumn> columnIterator = columns.iterator();
|
||||
@@ -458,20 +441,28 @@ public class XmlElementGeneratorTools {
|
||||
* @return
|
||||
*/
|
||||
private static XmlElement generateCommColumnsSelective(List<IntrospectedColumn> 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
|
||||
|
||||
@@ -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<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
|
||||
+28
@@ -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 {
|
||||
}
|
||||
Reference in New Issue
Block a user