Commit baf72992 authored by hewei's avatar hewei

SelectSelectivePlugin插件重构

parent 0be29103
......@@ -16,10 +16,8 @@
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 com.itfsw.mybatis.generator.plugins.utils.*;
import com.itfsw.mybatis.generator.plugins.utils.hook.ISelectOneByExamplePluginHook;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
......@@ -60,9 +58,12 @@ public class SelectOneByExamplePlugin extends BasePlugin {
);
commentGenerator.addGeneralMethodComment(method, introspectedTable);
// interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。");
// hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) {
// interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。");
}
// 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
if (introspectedTable.hasBLOBColumns()) {
......@@ -75,9 +76,12 @@ public class SelectOneByExamplePlugin extends BasePlugin {
);
commentGenerator.addGeneralMethodComment(method1, introspectedTable);
// interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method1);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。");
// hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithBLOBsMethodGenerated(method1, interfaze, introspectedTable)) {
// interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method1);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。");
}
}
return true;
......
......@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.*;
import com.itfsw.mybatis.generator.plugins.utils.hook.ISelectOneByExamplePluginHook;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
......@@ -40,7 +41,7 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
* @time:2017/6/29 13:34
* ---------------------------------------------------------------------------
*/
public class SelectSelectivePlugin extends BasePlugin {
public class SelectSelectivePlugin extends BasePlugin implements ISelectOneByExamplePluginHook {
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";
......@@ -63,74 +64,83 @@ public class SelectSelectivePlugin extends BasePlugin {
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);
// =========================================== client 方法生成 ===================================================
// 1. selectByExampleSelective 方法
Method mSelectByExampleSelective = JavaElementGeneratorTools.generateMethod(
@Override
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
FormatTools.addMethodWithBestPosition(interfaze, this.replaceMethodWithSelective(
method,
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);
"@Param(\"example\")",
introspectedTable
));
return super.clientSelectByExampleWithBLOBsMethodGenerated(method, interfaze, introspectedTable);
}
// 2. selectByPrimaryKeySelective
Method mSelectByPrimaryKeySelective = JavaElementGeneratorTools.generateMethod(
METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE,
JavaVisibility.DEFAULT,
fullFieldModel
);
@Override
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
if (!introspectedTable.hasBLOBColumns()) {
FormatTools.addMethodWithBestPosition(interfaze, this.replaceMethodWithSelective(
method,
METHOD_SELECT_BY_EXAMPLE_SELECTIVE,
"@Param(\"example\")",
introspectedTable
));
}
return super.clientSelectByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable);
}
@Override
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
if (introspectedTable.getRules().generatePrimaryKeyClass()) {
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
mSelectByPrimaryKeySelective.addParameter(new Parameter(type, "key", "@Param(\"record\")"));
FormatTools.addMethodWithBestPosition(interfaze, this.replaceMethodWithSelective(
method,
METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE,
"@Param(\"record\")",
introspectedTable
));
} 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);
Method withSelective = JavaElementTools.clone(method);
FormatTools.replaceGeneralMethodComment(commentGenerator, withSelective, introspectedTable);
withSelective.setName(METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE);
withSelective.getParameters().clear();
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
withSelective.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty(), "@Param(\"" + introspectedColumn.getJavaProperty() + "\")"));
}
// selective
withSelective.addParameter(
new Parameter(this.getModelColumnFullyQualifiedJavaType(introspectedTable), "selective", "@Param(\"selective\")", true)
);
FormatTools.addMethodWithBestPosition(interfaze, withSelective);
}
mSelectByPrimaryKeySelective.addParameter(new Parameter(selectiveType, "selective", "@Param(\"selective\")", true));
commentGenerator.addGeneralMethodComment(mSelectByPrimaryKeySelective, introspectedTable);
FormatTools.addMethodWithBestPosition(interfaze, mSelectByPrimaryKeySelective);
return super.clientSelectByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable);
}
// 3. selectOneByExampleSelective
if (PluginTools.getPluginConfiguration(context, SelectOneByExamplePlugin.class) != null) {
Method mSelectOneByExampleSelective = JavaElementGeneratorTools.generateMethod(
@Override
public boolean clientSelectOneByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
FormatTools.addMethodWithBestPosition(interfaze, this.replaceMethodWithSelective(
method,
METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE,
"@Param(\"example\")",
introspectedTable
));
return true;
}
@Override
public boolean clientSelectOneByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
if (!introspectedTable.hasBLOBColumns()) {
FormatTools.addMethodWithBestPosition(interfaze, this.replaceMethodWithSelective(
method,
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);
"@Param(\"example\")",
introspectedTable
));
}
return true;
}
......@@ -285,6 +295,40 @@ public class SelectSelectivePlugin extends BasePlugin {
return true;
}
// =========================================== 一些私有方法 =====================================================
/**
* 替换方法成withSelective
* @param method
* @param name
* @param firstAnnotation
* @param introspectedTable
* @return
*/
private Method replaceMethodWithSelective(Method method, String name, String firstAnnotation, IntrospectedTable introspectedTable) {
Method withSelective = JavaElementTools.clone(method);
FormatTools.replaceGeneralMethodComment(commentGenerator, withSelective, introspectedTable);
withSelective.setName(name);
// example
withSelective.getParameters().get(0).addAnnotation(firstAnnotation);
// selective
withSelective.addParameter(
new Parameter(this.getModelColumnFullyQualifiedJavaType(introspectedTable), "selective", "@Param(\"selective\")", true)
);
return withSelective;
}
/**
* 获取ModelColumn type
* @param introspectedTable
* @return
*/
private FullyQualifiedJavaType getModelColumnFullyQualifiedJavaType(IntrospectedTable introspectedTable) {
return new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName() + "." + ModelColumnPlugin.ENUM_NAME);
}
/**
* @param answer
* @param introspectedTable
......@@ -292,31 +336,12 @@ public class SelectSelectivePlugin extends BasePlugin {
*/
private void addResultMapElementsWithoutBLOBs(XmlElement answer, IntrospectedTable introspectedTable) {
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
XmlElement resultElement = new XmlElement("id");
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()));
}
answer.addElement(resultElement);
answer.addElement(XmlElementGeneratorTools.generateResultMapResultElement("id", introspectedColumn));
}
List<IntrospectedColumn> columns = introspectedTable.getBaseColumns();
for (IntrospectedColumn introspectedColumn : columns) {
XmlElement resultElement = new XmlElement("result");
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()));
}
answer.addElement(resultElement);
answer.addElement(XmlElementGeneratorTools.generateResultMapResultElement("result", introspectedColumn));
}
}
......@@ -327,15 +352,7 @@ public class SelectSelectivePlugin extends BasePlugin {
*/
private void addResultMapElementsWithBLOBs(XmlElement answer, IntrospectedTable introspectedTable) {
for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) {
XmlElement resultElement = new XmlElement("result");
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()));
}
answer.addElement(resultElement);
answer.addElement(XmlElementGeneratorTools.generateResultMapResultElement("result", introspectedColumn));
}
}
}
/*
* 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;
import java.util.Iterator;
import java.util.List;
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
/**
* ---------------------------------------------------------------------------
* Xml 节点生成工具 参考 org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator
......@@ -543,4 +545,23 @@ public class XmlElementGeneratorTools {
}
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;
* @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); // 日志
private final static HookAggregator instance = new HookAggregator();
private Context context;
......@@ -207,4 +207,27 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
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