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.CountByExampleMethodGenerator;
34  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.DeleteByExampleMethodGenerator;
35  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.DeleteByPrimaryKeyMethodGenerator;
36  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertMethodGenerator;
37  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertSelectiveMethodGenerator;
38  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByExampleWithBLOBsMethodGenerator;
39  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByExampleWithoutBLOBsMethodGenerator;
40  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByPrimaryKeyMethodGenerator;
41  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByExampleSelectiveMethodGenerator;
42  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByExampleWithBLOBsMethodGenerator;
43  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByExampleWithoutBLOBsMethodGenerator;
44  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeySelectiveMethodGenerator;
45  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeyWithBLOBsMethodGenerator;
46  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeyWithoutBLOBsMethodGenerator;
47  import org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator;
48  import org.mybatis.generator.config.PropertyRegistry;
49  
50  public class JavaMapperGenerator extends AbstractJavaClientGenerator {
51  
52      public JavaMapperGenerator(String project) {
53          this(project, true);
54      }
55  
56      public JavaMapperGenerator(String project, boolean requiresMatchedXMLGenerator) {
57          super(project, requiresMatchedXMLGenerator);
58      }
59  
60      @Override
61      public List<CompilationUnit> getCompilationUnits() {
62          progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$
63                  introspectedTable.getFullyQualifiedTable().toString()));
64          CommentGenerator commentGenerator = context.getCommentGenerator();
65  
66          FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
67          Interface interfaze = new Interface(type);
68          interfaze.setVisibility(JavaVisibility.PUBLIC);
69          commentGenerator.addJavaFileComment(interfaze);
70  
71          String rootInterface = introspectedTable.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
72          if (!stringHasValue(rootInterface)) {
73              rootInterface = context.getJavaClientGeneratorConfiguration()
74                      .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
75          }
76  
77          if (stringHasValue(rootInterface)) {
78              FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(rootInterface);
79              interfaze.addSuperInterface(fqjt);
80              interfaze.addImportedType(fqjt);
81          }
82  
83          addCountByExampleMethod(interfaze);
84          addDeleteByExampleMethod(interfaze);
85          addDeleteByPrimaryKeyMethod(interfaze);
86          addInsertMethod(interfaze);
87          addInsertSelectiveMethod(interfaze);
88          addSelectByExampleWithBLOBsMethod(interfaze);
89          addSelectByExampleWithoutBLOBsMethod(interfaze);
90          addSelectByPrimaryKeyMethod(interfaze);
91          addUpdateByExampleSelectiveMethod(interfaze);
92          addUpdateByExampleWithBLOBsMethod(interfaze);
93          addUpdateByExampleWithoutBLOBsMethod(interfaze);
94          addUpdateByPrimaryKeySelectiveMethod(interfaze);
95          addUpdateByPrimaryKeyWithBLOBsMethod(interfaze);
96          addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze);
97  
98          List<CompilationUnit> answer = new ArrayList<>();
99          if (context.getPlugins().clientGenerated(interfaze, introspectedTable)) {
100             answer.add(interfaze);
101         }
102 
103         List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
104         if (extraCompilationUnits != null) {
105             answer.addAll(extraCompilationUnits);
106         }
107 
108         return answer;
109     }
110 
111     protected void addCountByExampleMethod(Interface interfaze) {
112         if (introspectedTable.getRules().generateCountByExample()) {
113             AbstractJavaMapperMethodGenerator methodGenerator = new CountByExampleMethodGenerator();
114             initializeAndExecuteGenerator(methodGenerator, interfaze);
115         }
116     }
117 
118     protected void addDeleteByExampleMethod(Interface interfaze) {
119         if (introspectedTable.getRules().generateDeleteByExample()) {
120             AbstractJavaMapperMethodGenerator methodGenerator = new DeleteByExampleMethodGenerator();
121             initializeAndExecuteGenerator(methodGenerator, interfaze);
122         }
123     }
124 
125     protected void addDeleteByPrimaryKeyMethod(Interface interfaze) {
126         if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
127             AbstractJavaMapperMethodGenerator methodGenerator = new DeleteByPrimaryKeyMethodGenerator(false);
128             initializeAndExecuteGenerator(methodGenerator, interfaze);
129         }
130     }
131 
132     protected void addInsertMethod(Interface interfaze) {
133         if (introspectedTable.getRules().generateInsert()) {
134             AbstractJavaMapperMethodGenerator methodGenerator = new InsertMethodGenerator(false);
135             initializeAndExecuteGenerator(methodGenerator, interfaze);
136         }
137     }
138 
139     protected void addInsertSelectiveMethod(Interface interfaze) {
140         if (introspectedTable.getRules().generateInsertSelective()) {
141             AbstractJavaMapperMethodGenerator methodGenerator = new InsertSelectiveMethodGenerator();
142             initializeAndExecuteGenerator(methodGenerator, interfaze);
143         }
144     }
145 
146     protected void addSelectByExampleWithBLOBsMethod(Interface interfaze) {
147         if (introspectedTable.getRules().generateSelectByExampleWithBLOBs()) {
148             AbstractJavaMapperMethodGenerator methodGenerator = new SelectByExampleWithBLOBsMethodGenerator();
149             initializeAndExecuteGenerator(methodGenerator, interfaze);
150         }
151     }
152 
153     protected void addSelectByExampleWithoutBLOBsMethod(Interface interfaze) {
154         if (introspectedTable.getRules().generateSelectByExampleWithoutBLOBs()) {
155             AbstractJavaMapperMethodGenerator methodGenerator = new SelectByExampleWithoutBLOBsMethodGenerator();
156             initializeAndExecuteGenerator(methodGenerator, interfaze);
157         }
158     }
159 
160     protected void addSelectByPrimaryKeyMethod(Interface interfaze) {
161         if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
162             AbstractJavaMapperMethodGenerator methodGenerator = new SelectByPrimaryKeyMethodGenerator(false);
163             initializeAndExecuteGenerator(methodGenerator, interfaze);
164         }
165     }
166 
167     protected void addUpdateByExampleSelectiveMethod(Interface interfaze) {
168         if (introspectedTable.getRules().generateUpdateByExampleSelective()) {
169             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByExampleSelectiveMethodGenerator();
170             initializeAndExecuteGenerator(methodGenerator, interfaze);
171         }
172     }
173 
174     protected void addUpdateByExampleWithBLOBsMethod(Interface interfaze) {
175         if (introspectedTable.getRules().generateUpdateByExampleWithBLOBs()) {
176             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByExampleWithBLOBsMethodGenerator();
177             initializeAndExecuteGenerator(methodGenerator, interfaze);
178         }
179     }
180 
181     protected void addUpdateByExampleWithoutBLOBsMethod(Interface interfaze) {
182         if (introspectedTable.getRules().generateUpdateByExampleWithoutBLOBs()) {
183             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByExampleWithoutBLOBsMethodGenerator();
184             initializeAndExecuteGenerator(methodGenerator, interfaze);
185         }
186     }
187 
188     protected void addUpdateByPrimaryKeySelectiveMethod(Interface interfaze) {
189         if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
190             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByPrimaryKeySelectiveMethodGenerator();
191             initializeAndExecuteGenerator(methodGenerator, interfaze);
192         }
193     }
194 
195     protected void addUpdateByPrimaryKeyWithBLOBsMethod(Interface interfaze) {
196         if (introspectedTable.getRules().generateUpdateByPrimaryKeyWithBLOBs()) {
197             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByPrimaryKeyWithBLOBsMethodGenerator();
198             initializeAndExecuteGenerator(methodGenerator, interfaze);
199         }
200     }
201 
202     protected void addUpdateByPrimaryKeyWithoutBLOBsMethod(Interface interfaze) {
203         if (introspectedTable.getRules().generateUpdateByPrimaryKeyWithoutBLOBs()) {
204             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByPrimaryKeyWithoutBLOBsMethodGenerator();
205             initializeAndExecuteGenerator(methodGenerator, interfaze);
206         }
207     }
208 
209     protected void initializeAndExecuteGenerator(AbstractJavaMapperMethodGenerator methodGenerator,
210             Interface interfaze) {
211         methodGenerator.setContext(context);
212         methodGenerator.setIntrospectedTable(introspectedTable);
213         methodGenerator.setProgressCallback(progressCallback);
214         methodGenerator.setWarnings(warnings);
215         methodGenerator.addInterfaceElements(interfaze);
216     }
217 
218     public List<CompilationUnit> getExtraCompilationUnits() {
219         return Collections.emptyList();
220     }
221 
222     @Override
223     public AbstractXmlGenerator getMatchedXMLGenerator() {
224         return new XMLMapperGenerator();
225     }
226 }