Skip to content

Commit

Permalink
界面不使用Example相应的xml也不生成
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Aug 3, 2018
1 parent 8d20c3b commit ddcab79
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.GeneratorConfig;
import com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin;
import com.zzg.mybatis.generator.plugins.DbRemarksCommentGenerator;
import com.zzg.mybatis.generator.util.ConfigHelper;
import com.zzg.mybatis.generator.util.DbUtil;
Expand Down Expand Up @@ -235,7 +234,8 @@ public void generate() throws Exception {
if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())
|| DbType.PostgreSQL.name().equals(selectedDatabaseConfig.getDbType())) {
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin");
pluginConfiguration.addProperty("useExample", String.valueOf(generatorConfig.isUseExample()));
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin");
pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin");
context.addPluginConfiguration(pluginConfiguration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ public class CommonDAOInterfacePlugin extends PluginAdapter {
private String expandDaoSuperClass;

private ShellCallback shellCallback = null;
private boolean useExample;

public CommonDAOInterfacePlugin() {
shellCallback = new DefaultShellCallback(false);
this.useExample = "true".equals(getProperties().getProperty("useExample"));
}

@Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {

JavaFormatter javaFormatter = context.getJavaFormatter();
JavaFormatter javaFormatter = context.getJavaFormatter();
String daoTargetDir = context.getJavaClientGeneratorConfiguration().getTargetProject();
String daoTargetPackage = context.getJavaClientGeneratorConfiguration().getTargetPackage();
List<GeneratedJavaFile> mapperJavaFiles = new ArrayList<GeneratedJavaFile>();
Expand All @@ -55,13 +56,17 @@ public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTa
mapperInterface.addJavaDocLine(" * " + "DAO公共基类,由MybatisGenerator自动生成请勿修改");
mapperInterface.addJavaDocLine(" * " + "@param <Model> The Model Class");
mapperInterface.addJavaDocLine(" * " + "@param <PK> The Primary Key Class");
mapperInterface.addJavaDocLine(" * " + "@param <E> The Example Class");
if (useExample) {
mapperInterface.addJavaDocLine(" * " + "@param <E> The Example Class");
}
mapperInterface.addJavaDocLine(" */");

FullyQualifiedJavaType daoBaseInterfaceJavaType = mapperInterface.getType();
daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("Model"));
daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("PK extends Serializable"));
daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("E"));
if (useExample) {
daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("E"));
}

if (!this.methods.isEmpty()) {

Expand Down Expand Up @@ -113,13 +118,14 @@ public boolean clientGenerated(Interface interfaze,

FullyQualifiedJavaType primaryKeyTypeJavaType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();
daoSuperType.addTypeArgument(primaryKeyTypeJavaType);

String exampleType = introspectedTable.getExampleType();
FullyQualifiedJavaType exampleTypeJavaType = new FullyQualifiedJavaType(exampleType);
daoSuperType.addTypeArgument(exampleTypeJavaType);

interfaze.addImportedType(primaryKeyTypeJavaType);
interfaze.addImportedType(exampleTypeJavaType);
interfaze.addImportedType(primaryKeyTypeJavaType);

if (useExample) {
String exampleType = introspectedTable.getExampleType();
FullyQualifiedJavaType exampleTypeJavaType = new FullyQualifiedJavaType(exampleType);
daoSuperType.addTypeArgument(exampleTypeJavaType);
interfaze.addImportedType(exampleTypeJavaType);
}
interfaze.addImportedType(baseModelJavaType);
interfaze.addImportedType(daoSuperType);
interfaze.addSuperInterface(daoSuperType);
Expand All @@ -132,9 +138,11 @@ public boolean validate(List<String> list) {
}

private void interceptExampleParam(Method method) {
method.getParameters().clear();
method.addParameter(new Parameter(new FullyQualifiedJavaType("E"), "example"));
methods.add(method);
if (useExample) {
method.getParameters().clear();
method.addParameter(new Parameter(new FullyQualifiedJavaType("E"), "example"));
methods.add(method);
}
}

private void interceptPrimaryKeyParam(Method method) {
Expand All @@ -150,43 +158,49 @@ private void interceptModelParam(Method method) {
}

private void interceptModelAndExampleParam(Method method) {
List<Parameter> parameters = method.getParameters();
if (parameters.size() == 1) {
interceptExampleParam(method);
}else{
method.getParameters().clear();
Parameter parameter1 = new Parameter(new FullyQualifiedJavaType("Model"), "record");
parameter1.addAnnotation("@Param(\"record\")");
method.addParameter(parameter1);

Parameter parameter2 = new Parameter(new FullyQualifiedJavaType("E"), "example");
parameter2.addAnnotation("@Param(\"example\")");
method.addParameter(parameter2);
methods.add(method);
}
if (useExample) {
List<Parameter> parameters = method.getParameters();
if (parameters.size() == 1) {
interceptExampleParam(method);
}else{
method.getParameters().clear();
Parameter parameter1 = new Parameter(new FullyQualifiedJavaType("Model"), "record");
parameter1.addAnnotation("@Param(\"record\")");
method.addParameter(parameter1);

Parameter parameter2 = new Parameter(new FullyQualifiedJavaType("E"), "example");
parameter2.addAnnotation("@Param(\"example\")");
method.addParameter(parameter2);
methods.add(method);
}
}
}

@Override
public boolean clientCountByExampleMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
// interfaze
interceptExampleParam(method);
return false;
}
// interface
if (useExample) {
interceptExampleParam(method);
}
return false;
}


@Override
public boolean clientDeleteByExampleMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptExampleParam(method);
if (useExample) {
interceptExampleParam(method);
}
return false;
}


@Override
public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptPrimaryKeyParam(method);
interceptPrimaryKeyParam(method);
return false;
}

Expand All @@ -200,45 +214,55 @@ public boolean clientInsertMethodGenerated(Method method, Interface interfaze,
@Override
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptExampleParam(method);
method.setReturnType(new FullyQualifiedJavaType("List<Model>"));
if (useExample) {
interceptExampleParam(method);
method.setReturnType(new FullyQualifiedJavaType("List<Model>"));
}
return false;
}

@Override
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptExampleParam(method);
method.setReturnType(new FullyQualifiedJavaType("List<Model>"));
if (useExample) {
interceptExampleParam(method);
method.setReturnType(new FullyQualifiedJavaType("List<Model>"));
}
return false;
}

@Override
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptPrimaryKeyParam(method);
interceptPrimaryKeyParam(method);
method.setReturnType(new FullyQualifiedJavaType("Model"));
return false;
}

@Override
public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptModelAndExampleParam(method);
if (useExample) {
interceptModelAndExampleParam(method);
}
return false;
}

@Override
public boolean clientUpdateByExampleWithBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptModelAndExampleParam(method);
if (useExample) {
interceptModelAndExampleParam(method);
}
return false;
}

@Override
public boolean clientUpdateByExampleWithoutBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptModelAndExampleParam(method);
if (useExample) {
interceptModelAndExampleParam(method);
}
return false;
}

Expand All @@ -251,20 +275,24 @@ public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method,

@Override
public boolean clientUpdateByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
interceptModelAndExampleParam(method);
if (useExample) {
interceptModelAndExampleParam(method);
}
return false;
}

@Override
public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
interceptModelAndExampleParam(method);
if (useExample) {
interceptModelAndExampleParam(method);
}
return false;
}

@Override
public boolean clientUpdateByPrimaryKeyWithBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
interceptModelParam(method);
interceptModelParam(method);
return false;
}

Expand Down

0 comments on commit ddcab79

Please sign in to comment.