新插件SelectSelectivePlugin实现
This commit is contained in:
@@ -169,6 +169,31 @@ public class LimitPlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element, introspectedTable);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL Map Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param element
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
this.generateLimitElement(element, introspectedTable);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成limit节点
|
||||
*
|
||||
* @param element
|
||||
* @param introspectedTable
|
||||
*/
|
||||
public void generateLimitElement(XmlElement element, IntrospectedTable introspectedTable){
|
||||
XmlElement ifLimitNotNullElement = new XmlElement("if");
|
||||
ifLimitNotNullElement.addAttribute(new Attribute("test", "rows != null"));
|
||||
|
||||
@@ -185,20 +210,5 @@ public class LimitPlugin extends BasePlugin {
|
||||
element.addElement(ifLimitNotNullElement);
|
||||
|
||||
logger.debug("itfsw(MySQL分页插件):"+introspectedTable.getMyBatis3XmlMapperFileName()+"selectByExample方法增加分页条件。");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL Map Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param element
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
return this.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.enhanced.JavaElementEnhanced;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
@@ -200,7 +200,7 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
mIncrements.addBodyLine("obj." + setterMethod.getName() + "(" + field.getName() + ");");
|
||||
mIncrements.addBodyLine("return this;");
|
||||
|
||||
JavaElementEnhanced.addMethodWithBestPosition(innerClass, mIncrements);
|
||||
FormatTools.addMethodWithBestPosition(innerClass, mIncrements);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
@@ -132,7 +133,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
|
||||
commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method);
|
||||
FormatTools.addMethodWithBestPosition(interfaze, method);
|
||||
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。");
|
||||
|
||||
// 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
|
||||
@@ -147,7 +148,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
|
||||
commentGenerator.addGeneralMethodComment(method1, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method1);
|
||||
FormatTools.addMethodWithBestPosition(interfaze, method1);
|
||||
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。");
|
||||
}
|
||||
|
||||
@@ -200,7 +201,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
|
||||
// 只查询一条
|
||||
selectOneElement.addElement(new TextElement("limit 1"));
|
||||
// 添加到根节点
|
||||
document.getRootElement().addElement(selectOneElement);
|
||||
FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneElement);
|
||||
logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExample方法。");
|
||||
|
||||
// ------------------------------------ selectOneByExampleWithBLOBs ----------------------------------
|
||||
@@ -247,7 +248,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
|
||||
selectOneWithBLOBsElement.addElement(new TextElement("limit 1"));
|
||||
|
||||
// 添加到根节点
|
||||
document.getRootElement().addElement(selectOneWithBLOBsElement);
|
||||
FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneWithBLOBsElement);
|
||||
logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExampleWithBLOBs方法。");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* Copyright (c) 2017.
|
||||
*
|
||||
* 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;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.*;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
import org.mybatis.generator.api.dom.xml.Document;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/6/29 13:34
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class SelectSelectivePlugin extends BasePlugin {
|
||||
public static final String METHOD_SELECT_BY_EXAMPLE_SELECTIVE = "selectByExampleSelective";
|
||||
public static final String METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE = "selectByPrimaryKeySelective";
|
||||
public static final String METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE = "selectOneByExampleSelective";
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param warnings
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
|
||||
// 插件使用前提是使用了ModelColumnPlugin插件
|
||||
if (!PluginTools.checkDependencyPlugin(getContext(), ModelColumnPlugin.class)) {
|
||||
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return super.validate(warnings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param interfaze
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
// 找出全字段对应的Model
|
||||
FullyQualifiedJavaType fullFieldModel = introspectedTable.getRules().calculateAllFieldsClass();
|
||||
// 返回list类型
|
||||
FullyQualifiedJavaType listType = FullyQualifiedJavaType.getNewListInstance();
|
||||
listType.addTypeArgument(fullFieldModel);
|
||||
// column枚举
|
||||
FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(fullFieldModel.getShortName() + "." + ModelColumnPlugin.ENUM_NAME);
|
||||
|
||||
// 1. selectByExampleSelective 方法
|
||||
Method mSelectByExampleSelective = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_SELECT_BY_EXAMPLE_SELECTIVE,
|
||||
JavaVisibility.DEFAULT,
|
||||
listType,
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")"),
|
||||
new Parameter(selectiveType, "selective", "@Param(\"selective\")", true)
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mSelectByExampleSelective, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(interfaze, mSelectByExampleSelective);
|
||||
|
||||
// 2. selectByPrimaryKeySelective
|
||||
Method mSelectByPrimaryKeySelective = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE,
|
||||
JavaVisibility.DEFAULT,
|
||||
fullFieldModel
|
||||
);
|
||||
if (introspectedTable.getRules().generatePrimaryKeyClass()) {
|
||||
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
|
||||
mSelectByPrimaryKeySelective.addParameter(new Parameter(type, "key", "@Param(\"record\")"));
|
||||
} else {
|
||||
// no primary key class - fields are in the base class
|
||||
// if more than one PK field, then we need to annotate the
|
||||
// parameters
|
||||
// for MyBatis3
|
||||
List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
|
||||
|
||||
for (IntrospectedColumn introspectedColumn : introspectedColumns) {
|
||||
FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
|
||||
Parameter parameter = new Parameter(type, introspectedColumn.getJavaProperty());
|
||||
parameter.addAnnotation("@Param(\"" + introspectedColumn.getJavaProperty() + "\")");
|
||||
mSelectByPrimaryKeySelective.addParameter(parameter);
|
||||
}
|
||||
}
|
||||
mSelectByPrimaryKeySelective.addParameter(new Parameter(selectiveType, "selective", "@Param(\"selective\")", true));
|
||||
commentGenerator.addGeneralMethodComment(mSelectByPrimaryKeySelective, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(interfaze, mSelectByPrimaryKeySelective);
|
||||
|
||||
// 3. selectOneByExampleSelective
|
||||
if (PluginTools.getPluginConfiguration(context, SelectOneByExamplePlugin.class) != null) {
|
||||
Method mSelectOneByExampleSelective = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE,
|
||||
JavaVisibility.DEFAULT,
|
||||
fullFieldModel,
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")"),
|
||||
new Parameter(selectiveType, "selective", "@Param(\"selective\")", true)
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mSelectOneByExampleSelective, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(interfaze, mSelectOneByExampleSelective);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
// 生成返回字段节点
|
||||
XmlElement columnsEle = new XmlElement("foreach");
|
||||
columnsEle.addAttribute(new Attribute("collection", "selective"));
|
||||
columnsEle.addAttribute(new Attribute("item", "column"));
|
||||
columnsEle.addAttribute(new Attribute("separator", ","));
|
||||
columnsEle.addElement(new TextElement("${column.value}"));
|
||||
|
||||
// 1. selectByExampleSelective 方法
|
||||
XmlElement selectByExampleSelectiveEle = new XmlElement("select");
|
||||
commentGenerator.addComment(selectByExampleSelectiveEle);
|
||||
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("id", METHOD_SELECT_BY_EXAMPLE_SELECTIVE));
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("resultType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("parameterType", "map"));
|
||||
|
||||
selectByExampleSelectiveEle.addElement(new TextElement("select"));
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
selectByExampleSelectiveEle.addElement(new TextElement("'" + introspectedTable.getSelectByExampleQueryId() + "' as QUERYID,"));
|
||||
}
|
||||
selectByExampleSelectiveEle.addElement(columnsEle);
|
||||
selectByExampleSelectiveEle.addElement(new TextElement("from " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
|
||||
selectByExampleSelectiveEle.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
XmlElement ifElement = new XmlElement("if");
|
||||
ifElement.addAttribute(new Attribute("test", "example.orderByClause != null"));
|
||||
ifElement.addElement(new TextElement("order by ${example.orderByClause}"));
|
||||
selectByExampleSelectiveEle.addElement(ifElement);
|
||||
|
||||
FormatTools.addElementWithBestPosition(document.getRootElement(), selectByExampleSelectiveEle);
|
||||
|
||||
// 2. selectByPrimaryKeySelective
|
||||
XmlElement selectByPrimaryKeySelectiveEle = new XmlElement("select");
|
||||
commentGenerator.addComment(selectByPrimaryKeySelectiveEle);
|
||||
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("id", METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE));
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("resultType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("parameterType", "map"));
|
||||
|
||||
selectByPrimaryKeySelectiveEle.addElement(new TextElement("select"));
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
selectByPrimaryKeySelectiveEle.addElement(new TextElement("'" + introspectedTable.getSelectByExampleQueryId() + "' as QUERYID,"));
|
||||
}
|
||||
selectByPrimaryKeySelectiveEle.addElement(columnsEle);
|
||||
selectByPrimaryKeySelectiveEle.addElement(new TextElement("from " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
|
||||
boolean and = false;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
|
||||
sb.setLength(0);
|
||||
if (and) {
|
||||
sb.append(" and ");
|
||||
} else {
|
||||
sb.append("where ");
|
||||
and = true;
|
||||
}
|
||||
|
||||
sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
|
||||
sb.append(" = ");
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, introspectedTable.getRules().generatePrimaryKeyClass() ? "record." : null));
|
||||
selectByPrimaryKeySelectiveEle.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
|
||||
FormatTools.addElementWithBestPosition(document.getRootElement(), selectByPrimaryKeySelectiveEle);
|
||||
|
||||
// 3. selectOneByExampleSelective
|
||||
if (PluginTools.getPluginConfiguration(context, SelectOneByExamplePlugin.class) != null) {
|
||||
XmlElement selectOneByExampleSelectiveEle = new XmlElement("select");
|
||||
commentGenerator.addComment(selectOneByExampleSelectiveEle);
|
||||
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE));
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("resultType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("parameterType", "map"));
|
||||
|
||||
selectOneByExampleSelectiveEle.addElement(new TextElement("select"));
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
selectOneByExampleSelectiveEle.addElement(new TextElement("'" + introspectedTable.getSelectByExampleQueryId() + "' as QUERYID,"));
|
||||
}
|
||||
selectOneByExampleSelectiveEle.addElement(columnsEle);
|
||||
selectOneByExampleSelectiveEle.addElement(new TextElement("from " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
|
||||
selectOneByExampleSelectiveEle.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
XmlElement ifElement1 = new XmlElement("if");
|
||||
ifElement1.addAttribute(new Attribute("test", "example.orderByClause != null"));
|
||||
ifElement1.addElement(new TextElement("order by ${example.orderByClause}"));
|
||||
selectOneByExampleSelectiveEle.addElement(ifElement1);
|
||||
|
||||
// 只查询一条
|
||||
selectOneByExampleSelectiveEle.addElement(new TextElement("limit 1"));
|
||||
|
||||
// 添加到根节点
|
||||
FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneByExampleSelectiveEle);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (c) 2017.
|
||||
*
|
||||
* 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;
|
||||
|
||||
import org.mybatis.generator.api.dom.java.InnerClass;
|
||||
import org.mybatis.generator.api.dom.java.Interface;
|
||||
import org.mybatis.generator.api.dom.java.Method;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
import org.mybatis.generator.api.dom.xml.Element;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 格式化工具,优化输出
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/6/30 10:53
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class FormatTools {
|
||||
/**
|
||||
* 在最佳位置添加方法
|
||||
*
|
||||
* @param innerClass
|
||||
* @param method
|
||||
*/
|
||||
public static void addMethodWithBestPosition(InnerClass innerClass, Method method){
|
||||
List<Method> methods = innerClass.getMethods();
|
||||
int index = -1;
|
||||
for (int i = 0; i < methods.size(); i++){
|
||||
Method m = methods.get(i);
|
||||
if (m.getName().equals(method.getName())){
|
||||
if (m.getParameters().size() <= method.getParameters().size()){
|
||||
index = i + 1;
|
||||
} else {
|
||||
index = i;
|
||||
}
|
||||
} else if (m.getName().startsWith(method.getName())){
|
||||
if (index == - 1){
|
||||
index = i;
|
||||
}
|
||||
} else if (method.getName().startsWith(m.getName())){
|
||||
index = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1 || index >= methods.size()){
|
||||
innerClass.addMethod(method);
|
||||
} else {
|
||||
methods.add(index, method);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在最佳位置添加方法
|
||||
*
|
||||
* @param interfacz
|
||||
* @param method
|
||||
*/
|
||||
public static void addMethodWithBestPosition(Interface interfacz, Method method){
|
||||
List<Method> methods = interfacz.getMethods();
|
||||
int index = -1;
|
||||
for (int i = 0; i < methods.size(); i++){
|
||||
Method m = methods.get(i);
|
||||
if (m.getName().equals(method.getName())){
|
||||
if (m.getParameters().size() <= method.getParameters().size()){
|
||||
index = i + 1;
|
||||
} else {
|
||||
index = i;
|
||||
}
|
||||
} else if (m.getName().startsWith(method.getName())){
|
||||
if (index == - 1){
|
||||
index = i;
|
||||
}
|
||||
} else if (method.getName().startsWith(m.getName())){
|
||||
index = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1 || index >= methods.size()){
|
||||
interfacz.addMethod(method);
|
||||
} else {
|
||||
methods.add(index, method);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在最佳位置添加节点
|
||||
*
|
||||
* @param rootElement
|
||||
* @param element
|
||||
*/
|
||||
public static void addElementWithBestPosition(XmlElement rootElement, XmlElement element){
|
||||
String id = getIdFromElement(element);
|
||||
if (id == null){
|
||||
rootElement.addElement(element);
|
||||
} else {
|
||||
List<Element> elements = rootElement.getElements();
|
||||
int index = -1;
|
||||
for (int i = 0; i < elements.size(); i++){
|
||||
Element ele = elements.get(i);
|
||||
if (ele instanceof XmlElement){
|
||||
String eleId = getIdFromElement((XmlElement) ele);
|
||||
if (eleId != null){
|
||||
if (eleId.startsWith(id)){
|
||||
if (index == - 1){
|
||||
index = i;
|
||||
}
|
||||
} else if (id.startsWith(eleId)){
|
||||
index = i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1 || index >= elements.size()){
|
||||
rootElement.addElement(element);
|
||||
} else {
|
||||
elements.add(index, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 找出节点ID值
|
||||
*
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
private static String getIdFromElement(XmlElement element){
|
||||
for (Attribute attribute : element.getAttributes()){
|
||||
if (attribute.getName().equals("id")){
|
||||
return attribute.getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017.
|
||||
*
|
||||
* 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.enhanced;
|
||||
|
||||
import org.mybatis.generator.api.dom.java.InnerClass;
|
||||
import org.mybatis.generator.api.dom.java.Method;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* JavaElement 增强
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/6/19 16:34
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class JavaElementEnhanced {
|
||||
/**
|
||||
* 在最佳位置添加方法
|
||||
*
|
||||
* @param innerClass
|
||||
* @param method
|
||||
*/
|
||||
public static void addMethodWithBestPosition(InnerClass innerClass, Method method){
|
||||
List<Method> methods = innerClass.getMethods();
|
||||
int index = -1;
|
||||
for (int i = 0; i < methods.size(); i++){
|
||||
Method m = methods.get(i);
|
||||
if (m.getName().equals(method.getName())){
|
||||
if (m.getParameters().size() <= method.getParameters().size() || index == -1){
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1){
|
||||
innerClass.addMethod(method);
|
||||
} else {
|
||||
methods.add(index, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user