View Javadoc
1   /*
2    *    Copyright 2006-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.codegen.mybatis3.javamapper;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.mybatis.generator.api.CommentGenerator;
26  import org.mybatis.generator.api.dom.java.CompilationUnit;
27  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
28  import org.mybatis.generator.api.dom.java.Interface;
29  import org.mybatis.generator.api.dom.java.JavaVisibility;
30  import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
31  import org.mybatis.generator.codegen.AbstractXmlGenerator;
32  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
33  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.DeleteByPrimaryKeyMethodGenerator;
34  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertMethodGenerator;
35  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectAllMethodGenerator;
36  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByPrimaryKeyMethodGenerator;
37  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeyWithoutBLOBsMethodGenerator;
38  import org.mybatis.generator.codegen.mybatis3.xmlmapper.SimpleXMLMapperGenerator;
39  import org.mybatis.generator.config.PropertyRegistry;
40  
41  public class SimpleJavaClientGenerator extends AbstractJavaClientGenerator {
42  
43      public SimpleJavaClientGenerator(String project) {
44          this(project, true);
45      }
46  
47      public SimpleJavaClientGenerator(String project, boolean requiresMatchedXMLGenerator) {
48          super(project, requiresMatchedXMLGenerator);
49      }
50  
51      @Override
52      public List<CompilationUnit> getCompilationUnits() {
53          progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$
54                  introspectedTable.getFullyQualifiedTable().toString()));
55          CommentGenerator commentGenerator = context.getCommentGenerator();
56  
57          FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
58          Interface interfaze = new Interface(type);
59          interfaze.setVisibility(JavaVisibility.PUBLIC);
60          commentGenerator.addJavaFileComment(interfaze);
61  
62          String rootInterface = introspectedTable.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
63          if (!stringHasValue(rootInterface)) {
64              rootInterface = context.getJavaClientGeneratorConfiguration()
65                      .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
66          }
67  
68          if (stringHasValue(rootInterface)) {
69              FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(rootInterface);
70              interfaze.addSuperInterface(fqjt);
71              interfaze.addImportedType(fqjt);
72          }
73  
74          addDeleteByPrimaryKeyMethod(interfaze);
75          addInsertMethod(interfaze);
76          addSelectByPrimaryKeyMethod(interfaze);
77          addSelectAllMethod(interfaze);
78          addUpdateByPrimaryKeyMethod(interfaze);
79  
80          List<CompilationUnit> answer = new ArrayList<>();
81          if (context.getPlugins().clientGenerated(interfaze, introspectedTable)) {
82              answer.add(interfaze);
83          }
84  
85          List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
86          if (extraCompilationUnits != null) {
87              answer.addAll(extraCompilationUnits);
88          }
89  
90          return answer;
91      }
92  
93      protected void addDeleteByPrimaryKeyMethod(Interface interfaze) {
94          if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
95              AbstractJavaMapperMethodGenerator methodGenerator = new DeleteByPrimaryKeyMethodGenerator(true);
96              initializeAndExecuteGenerator(methodGenerator, interfaze);
97          }
98      }
99  
100     protected void addInsertMethod(Interface interfaze) {
101         if (introspectedTable.getRules().generateInsert()) {
102             AbstractJavaMapperMethodGenerator methodGenerator = new InsertMethodGenerator(true);
103             initializeAndExecuteGenerator(methodGenerator, interfaze);
104         }
105     }
106 
107     protected void addSelectByPrimaryKeyMethod(Interface interfaze) {
108         if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
109             AbstractJavaMapperMethodGenerator methodGenerator = new SelectByPrimaryKeyMethodGenerator(true);
110             initializeAndExecuteGenerator(methodGenerator, interfaze);
111         }
112     }
113 
114     protected void addSelectAllMethod(Interface interfaze) {
115         AbstractJavaMapperMethodGenerator methodGenerator = new SelectAllMethodGenerator();
116         initializeAndExecuteGenerator(methodGenerator, interfaze);
117     }
118 
119     protected void addUpdateByPrimaryKeyMethod(Interface interfaze) {
120         if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
121             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByPrimaryKeyWithoutBLOBsMethodGenerator();
122             initializeAndExecuteGenerator(methodGenerator, interfaze);
123         }
124     }
125 
126     protected void initializeAndExecuteGenerator(AbstractJavaMapperMethodGenerator methodGenerator,
127             Interface interfaze) {
128         methodGenerator.setContext(context);
129         methodGenerator.setIntrospectedTable(introspectedTable);
130         methodGenerator.setProgressCallback(progressCallback);
131         methodGenerator.setWarnings(warnings);
132         methodGenerator.addInterfaceElements(interfaze);
133     }
134 
135     public List<CompilationUnit> getExtraCompilationUnits() {
136         return Collections.emptyList();
137     }
138 
139     @Override
140     public AbstractXmlGenerator getMatchedXMLGenerator() {
141         return new SimpleXMLMapperGenerator();
142     }
143 }