com.revengemission.plugins.mybatis.SelectColumnsByExamplePlugin Maven / Gradle / Ivy
package com.revengemission.plugins.mybatis;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
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 java.util.List;
/**
* 根据Example查询部分字段及group by 语句
*/
public class SelectColumnsByExamplePlugin extends AbstractXmbgPlugin {
private static final String CLIENT_METHOD_NAME = "selectColumnsByExample";
private static final String CLIENT_METHOD_NAME_IDS = "selectIdsByExample";
private static final String CLIENT_METHOD_NAME_Aggregate_Query = "aggregateQueryByExample";
@Override
public boolean validate(List warnings) {
return true;
}
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
Field commaSeparatedColumnsField = new Field("commaSeparatedColumns", FullyQualifiedJavaType.getStringInstance());
commaSeparatedColumnsField.setVisibility(JavaVisibility.PRIVATE);
commaSeparatedColumnsField.addJavaDocLine("/**");
commaSeparatedColumnsField.addJavaDocLine(" * 期望返回字段,以逗号分割开");
commaSeparatedColumnsField.addJavaDocLine(" */");
topLevelClass.addField(commaSeparatedColumnsField);
Method setCommaSeparatedColumns = new Method("setCommaSeparatedColumns");
setCommaSeparatedColumns.setVisibility(JavaVisibility.PUBLIC);
setCommaSeparatedColumns.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "commaSeparatedColumns"));
setCommaSeparatedColumns.addBodyLine("this.commaSeparatedColumns = commaSeparatedColumns;");
setCommaSeparatedColumns.addJavaDocLine("/**");
setCommaSeparatedColumns.addJavaDocLine(" * @param commaSeparatedColumns 期望返回字段,以逗号分割开");
setCommaSeparatedColumns.addJavaDocLine(" */");
topLevelClass.addMethod(setCommaSeparatedColumns);
Method getCommaSeparatedColumns = new Method("getCommaSeparatedColumns");
getCommaSeparatedColumns.setVisibility(JavaVisibility.PUBLIC);
getCommaSeparatedColumns.setReturnType(FullyQualifiedJavaType.getStringInstance());
getCommaSeparatedColumns.addBodyLine("return commaSeparatedColumns;");
topLevelClass.addMethod(getCommaSeparatedColumns);
Field aggregateQueryClause = new Field("aggregateQueryClause", FullyQualifiedJavaType.getStringInstance());
aggregateQueryClause.setVisibility(JavaVisibility.PRIVATE);
aggregateQueryClause.addJavaDocLine("/**");
aggregateQueryClause.addJavaDocLine(" * aggregate query clause 语句, 注意未做防注入处理");
aggregateQueryClause.addJavaDocLine(" */");
topLevelClass.addField(aggregateQueryClause);
Method setAggregateQueryClause = new Method("setAggregateQueryClause");
setAggregateQueryClause.setVisibility(JavaVisibility.PUBLIC);
setAggregateQueryClause.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "aggregateQueryClause"));
setAggregateQueryClause.addBodyLine("this.aggregateQueryClause = aggregateQueryClause;");
setAggregateQueryClause.addJavaDocLine("/**");
setAggregateQueryClause.addJavaDocLine(" * @param aggregateQueryClause aggregate query 语句, 注意未做防注入处理");
setAggregateQueryClause.addJavaDocLine(" */");
topLevelClass.addMethod(setAggregateQueryClause);
Method getAggregateQueryClause = new Method("getAggregateQueryClause");
getAggregateQueryClause.setVisibility(JavaVisibility.PUBLIC);
getAggregateQueryClause.setReturnType(FullyQualifiedJavaType.getStringInstance());
getAggregateQueryClause.addBodyLine("return aggregateQueryClause;");
topLevelClass.addMethod(getAggregateQueryClause);
return true;
}
@Override
public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
interfaze.addImportedType(FullyQualifiedJavaType.getNewMapInstance());
Method method = new Method(CLIENT_METHOD_NAME);
method.setAbstract(true);
method.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example"));
method.setReturnType(new FullyQualifiedJavaType("List<" + getEntityName(introspectedTable) + ">"));
interfaze.addMethod(method);
Method method2 = new Method(CLIENT_METHOD_NAME_IDS);
method2.setAbstract(true);
method2.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example"));
method2.setReturnType(new FullyQualifiedJavaType("List"));
interfaze.addMethod(method2);
Method method3 = new Method(CLIENT_METHOD_NAME_Aggregate_Query);
method3.addJavaDocLine("/**");
method3.addJavaDocLine(" * 聚合查询");
method3.addJavaDocLine(" */");
method3.setAbstract(true);
method3.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example"));
method3.setReturnType(new FullyQualifiedJavaType("List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy