Commit baf72992 authored by hewei's avatar hewei

SelectSelectivePlugin插件重构

parent 0be29103
...@@ -16,10 +16,8 @@ ...@@ -16,10 +16,8 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.*;
import com.itfsw.mybatis.generator.plugins.utils.FormatTools; import com.itfsw.mybatis.generator.plugins.utils.hook.ISelectOneByExamplePluginHook;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.Attribute;
...@@ -60,9 +58,12 @@ public class SelectOneByExamplePlugin extends BasePlugin { ...@@ -60,9 +58,12 @@ public class SelectOneByExamplePlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(method, introspectedTable); commentGenerator.addGeneralMethodComment(method, introspectedTable);
// hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) {
// interface 增加方法 // interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method); FormatTools.addMethodWithBestPosition(interfaze, method);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。"); logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。");
}
// 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准 // 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
if (introspectedTable.hasBLOBColumns()) { if (introspectedTable.hasBLOBColumns()) {
...@@ -75,10 +76,13 @@ public class SelectOneByExamplePlugin extends BasePlugin { ...@@ -75,10 +76,13 @@ public class SelectOneByExamplePlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(method1, introspectedTable); commentGenerator.addGeneralMethodComment(method1, introspectedTable);
// hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithBLOBsMethodGenerated(method1, interfaze, introspectedTable)) {
// interface 增加方法 // interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method1); FormatTools.addMethodWithBestPosition(interfaze, method1);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。"); logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。");
} }
}
return true; return true;
} }
......
/*
* 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;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TypeParameter;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/7 18:14
* ---------------------------------------------------------------------------
*/
public class JavaElementTools {
/**
* clone
* @param method
* @return
*/
public static Method clone(Method method) {
Method dest = new Method(method.getName());
// 注解
for (String javaDocLine : method.getJavaDocLines()) {
dest.addJavaDocLine(javaDocLine);
}
dest.setReturnType(method.getReturnType());
for (Parameter parameter : method.getParameters()) {
dest.addParameter(JavaElementTools.clone(parameter));
}
for (FullyQualifiedJavaType exception : method.getExceptions()) {
dest.addException(exception);
}
for (TypeParameter typeParameter : method.getTypeParameters()) {
dest.addTypeParameter(typeParameter);
}
dest.addBodyLines(method.getBodyLines());
dest.setConstructor(method.isConstructor());
dest.setNative(method.isNative());
dest.setSynchronized(method.isSynchronized());
dest.setDefault(method.isDefault());
dest.setFinal(method.isFinal());
dest.setStatic(method.isStatic());
dest.setVisibility(method.getVisibility());
return dest;
}
/**
* clone
* @param parameter
* @return
*/
public static Parameter clone(Parameter parameter) {
Parameter dest = new Parameter(parameter.getType(), parameter.getName(), parameter.isVarargs());
for (String annotation : parameter.getAnnotations()) {
dest.addAnnotation(annotation);
}
return dest;
}
}
...@@ -32,6 +32,8 @@ import java.util.Arrays; ...@@ -32,6 +32,8 @@ import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
/** /**
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
* Xml 节点生成工具 参考 org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator * Xml 节点生成工具 参考 org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator
...@@ -543,4 +545,23 @@ public class XmlElementGeneratorTools { ...@@ -543,4 +545,23 @@ public class XmlElementGeneratorTools {
} }
return false; return false;
} }
/**
* 生成resultMap的result 节点
* @param name
* @param introspectedColumn
* @return
*/
public static XmlElement generateResultMapResultElement(String name, IntrospectedColumn introspectedColumn) {
XmlElement resultElement = new XmlElement(name);
resultElement.addAttribute(new Attribute("column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
resultElement.addAttribute(new Attribute("property", introspectedColumn.getJavaProperty()));
resultElement.addAttribute(new Attribute("jdbcType", introspectedColumn.getJdbcTypeName()));
if (stringHasValue(introspectedColumn.getTypeHandler())) {
resultElement.addAttribute(new Attribute("typeHandler", introspectedColumn.getTypeHandler()));
}
return resultElement;
}
} }
...@@ -42,7 +42,7 @@ import java.util.List; ...@@ -42,7 +42,7 @@ import java.util.List;
* @time:2018/4/27 11:33 * @time:2018/4/27 11:33
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHook, IIncrementsPluginHook, IOptimisticLockerPluginHook { public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHook, IIncrementsPluginHook, IOptimisticLockerPluginHook, ISelectOneByExamplePluginHook {
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;
...@@ -207,4 +207,27 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo ...@@ -207,4 +207,27 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
return this.getPlugins(IOptimisticLockerPluginHook.class).get(0).generateSetsSelectiveElement(columns, versionColumn, setsElement); return this.getPlugins(IOptimisticLockerPluginHook.class).get(0).generateSetsSelectiveElement(columns, versionColumn, setsElement);
} }
} }
// ============================================= ISelectOneByExamplePluginHook ==============================================
@Override
public boolean clientSelectOneByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
for (ISelectOneByExamplePluginHook plugin : this.getPlugins(ISelectOneByExamplePluginHook.class)) {
if (!plugin.clientSelectOneByExampleWithBLOBsMethodGenerated(method, interfaze, introspectedTable)) {
return false;
}
}
return true;
}
@Override
public boolean clientSelectOneByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
for (ISelectOneByExamplePluginHook plugin : this.getPlugins(ISelectOneByExamplePluginHook.class)) {
if (!plugin.clientSelectOneByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) {
return false;
}
}
return true;
}
} }
/*
* 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;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/7 18:51
* ---------------------------------------------------------------------------
*/
public interface ISelectOneByExamplePluginHook {
/**
* selectOneByExampleWithBLOBs 接口方法生成
* @param method
* @param interfaze
* @param introspectedTable
* @return
*/
boolean clientSelectOneByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable);
/**
* selectOneByExample 接口方法生成
* @param method
* @param interfaze
* @param introspectedTable
* @return
*/
boolean clientSelectOneByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable);
}
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