Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/123'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/com/zzg/mybatis/generator/plugins/CommonDAOInterfacePlugin.java
  • Loading branch information
zouzhigang committed Aug 16, 2018
2 parents d11e88a + ddcab79 commit 754bb81
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 45 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 @@ -26,9 +26,11 @@ public class CommonDAOInterfacePlugin extends PluginAdapter {
private List<Method> methods = new ArrayList<>();

private ShellCallback shellCallback = null;
private boolean useExample;

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

@Override
Expand All @@ -51,13 +53,17 @@ public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTa
mapperInterface.addJavaDocLine(" * " + "DAO公共基类,由MybatisGenerator自动生成请勿修改");
mapperInterface.addJavaDocLine(" * " + "@param <Model> The Model Class 这里是泛型不是Model类");
mapperInterface.addJavaDocLine(" * " + "@param <PK> The Primary Key Class 如果是无主键,则可以用Model来跳过,如果是多主键则是Key类");
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()) {
for (Method method : methods) {
Expand Down Expand Up @@ -114,13 +120,14 @@ public boolean clientGenerated(Interface interfaze,
primaryKeyTypeJavaType = baseModelJavaType;
}
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 @@ -133,9 +140,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 @@ -151,43 +160,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 @@ -201,45 +216,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 @@ -252,20 +277,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 754bb81

Please sign in to comment.