Skip to content

Commit

Permalink
remove generated methods during merger
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntungho committed Jun 5, 2019
1 parent dab4f2c commit 1d23f64
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static List<String> runWithConfigurationFile(String path) throws IOExcept
List<String> warnings = new ArrayList<>();
ConfigurationParser parser = new ConfigurationParser(warnings);
Configuration configuration = parser.parseConfiguration(new File(path));
ShellCallback shellCallback = new DefaultShellCallback(true);
ShellCallback shellCallback = new CustomShellCallback(true);
MyBatisGenerator generator = new MyBatisGenerator(configuration, shellCallback, warnings);
ProgressCallback processCallback = new NullProgressCallback();
generator.generate(processCallback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.exception.ShellException;
import org.mybatis.generator.internal.DefaultShellCallback;

Expand Down Expand Up @@ -63,22 +65,30 @@ public String mergeJavaFile(String newFileSource,
}

public String mergerFile(CompilationUnit newCompilationUnit, CompilationUnit oldCompilationUnit) {
NodeList<ImportDeclaration> newImports = newCompilationUnit.getImports();
NodeList<ImportDeclaration> oldImports = oldCompilationUnit.getImports();
NodeList<ImportDeclaration> newImports = newCompilationUnit.getImports();

LinkedHashSet<ImportDeclaration> customImportSet = new LinkedHashSet<>(oldImports);
customImportSet.removeAll(newImports);
for (ImportDeclaration importDeclaration : customImportSet) {
newCompilationUnit.addImport(importDeclaration);
}

List<MethodDeclaration> newMethods = newCompilationUnit.getTypes().get(0).getMethods();
List<MethodDeclaration> oldMethods = oldCompilationUnit.getTypes().get(0).getMethods();
List<MethodDeclaration> oldMethods = oldCompilationUnit.getType(0).getMethods();
TypeDeclaration<?> typeDeclaration = newCompilationUnit.getType(0);
List<MethodDeclaration> newMethods = typeDeclaration.getMethods();

LinkedHashSet<MethodDeclaration> customMethodSet = new LinkedHashSet<>(oldMethods);
customMethodSet.removeAll(newMethods);
for (MethodDeclaration methodDeclaration : customMethodSet) {
newCompilationUnit.getType(0).addMember(methodDeclaration);
if (methodDeclaration.getComment().isPresent()) {
if (methodDeclaration.getComment().get().getContent().contains(MergeConstants.NEW_ELEMENT_TAG)) {
// skip generated methods
continue;
}
}

typeDeclaration.addMember(methodDeclaration);
}

return newCompilationUnit.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void generate() {

TableInfo tableInfo = new TableInfo();
tableInfo.setTableName("actions");
tableInfo.setDomainName("Action");
tableInfo.setDomainName("gene.Action");
param.setSelectedTables(Arrays.asList(tableInfo));

GeneratorToolWrapper tool = new GeneratorToolWrapper(param);
Expand Down
8 changes: 6 additions & 2 deletions src/test/resources/generator-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

<generatorConfiguration>
<context id="test" targetRuntime="MyBatis3">
<commentGenerator type="com.chuntung.plugin.mybatisbuilder.generator.CustomCommentGenerator">
<property name="addDatabaseRemark" value="true"/>
</commentGenerator>

<jdbcConnection driverClass="org.sqlite.JDBC"
connectionURL="jdbc:sqlite:/Users/tonyho/Downloads/ghost.db"
userId=""
password="">
</jdbcConnection>

<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>

<javaModelGenerator targetPackage="mybatisbuilder.example.model" targetProject="./src/test/java">
Expand All @@ -28,7 +32,7 @@
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<table tableName="actions" domainObjectName="gen.Action"
<table tableName="clients" domainObjectName="gene.Client"
enableSelectByExample="true"
enableUpdateByExample="false"
enableCountByExample="false"
Expand Down

0 comments on commit 1d23f64

Please sign in to comment.