Skip to content

Commit 4b769f0

Browse files
committed
[Generator] Add new mixed annotation/XML code generator
1 parent 51f36af commit 4b769f0

File tree

40 files changed

+16503
-32
lines changed

40 files changed

+16503
-32
lines changed

mybatis-generator-core/doc/html/configreference/commentGenerator.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ <h2>Supported Properties</h2>
8080
<th valign="top">true</th>
8181
<td>When the property is true, no comments will be added to any
8282
generated element.
83-
<p><b>Warning: </b> if you set this value to true, then
84-
all code merging will be disabled.</p?
8583
</td>
8684
</tr>
8785
</table>
86+
<p><b>Warning: </b> if you set this value to true, then all code merging will be disabled.</p>
8887
</td>
8988
</tr>
9089
<tr>

mybatis-generator-core/doc/html/configreference/javaClientGenerator.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,19 @@ <h2>Required Attributes</h2>
4747
<th nowrap="nowrap" valign="top">ANNOTATEDMAPPER</th>
4848
<td>The generated objects will be Java interfaces for the MyBatis 3.x mapper
4949
infrastructure. The interfaces will be based on annotations and MyBatis 3.x SqlProviders.
50-
No XML mapper files will be generated.</td>
50+
No XML mapper files will be generated.
51+
<p>The ANNOTATEDMAPPER requires MyBatis version 3.0.4 or higher.</p>
52+
</td>
53+
</tr>
54+
<tr>
55+
<th nowrap="nowrap" valign="top">MIXEDMAPPER</th>
56+
<td>The generated objects will be Java interfaces for the MyBatis 3.x mapper
57+
infrastructure. The interfaces will be based on a mix of annotations and XML.
58+
An annotation will be used where a simple annotation will work. This client
59+
will not generate and Sql Provider, so all complex dynamic SQL will be generated
60+
in XML.
61+
<p>The MIXEDMAPPER requires MyBatis version 3.0.4 or higher.</p>
62+
</td>
5163
</tr>
5264
<tr>
5365
<th nowrap="nowrap" valign="top">XMLMAPPER</th>

mybatis-generator-core/doc/html/whatsNew.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h3>Bugs Fixed</h3>
2828
</ul>
2929
<h3>Enhancements</h3>
3030
<ul>
31-
<li>Added a new MyBatis 3 generator that generates code based solely on annotations
31+
<li>Added a new MyBatis3 generator that generates code based solely on annotations
3232
with no generated XML. Configuration settings for this new generator are as follows:
3333
<ul>
3434
<li>The <code>targetRuntime</code> attribute of
@@ -39,6 +39,17 @@ <h3>Enhancements</h3>
3939
element is <b>ANNOTATEDMAPPER</b></li>
4040
</ul>
4141
</li>
42+
<li>Added a new MyBatis3 generator that generates code based on a mix of annotations
43+
and generated XML. Configuration settings for this new generator are as follows:
44+
<ul>
45+
<li>The <code>targetRuntime</code> attribute of
46+
<a href="configreference/context.html">&lt;context&gt;</a>
47+
element is <b>MyBatis3</b></li>
48+
<li>The <code>type</code> attribute of
49+
<a href="configreference/javaClientGenerator.html">&lt;javaClientGenerator&gt;</a>
50+
element is <b>MIXEDMAPPER</b></li>
51+
</ul>
52+
</li>
4253
<li>Add support for JDBC types NCHAR, NCLOB, NVARCHAR to match MyBatis3.</li>
4354
<li>Added support for "useGeneratedKeys" in MyBatis3. See
4455
<a href="configreference/generatedKey.html">&lt;generatedKey&gt;</a>

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/AbstractJavaClientGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ public AbstractJavaClientGenerator(boolean requiresXMLGenerator) {
3939
public boolean requiresXMLGenerator() {
4040
return requiresXMLGenerator;
4141
}
42+
43+
/**
44+
* This method returns an instance of the XML generator associated
45+
* with this client generator.
46+
*
47+
* @return the matched XML generator. May return null if no
48+
* XML is required by this generator
49+
*/
50+
public abstract AbstractXmlGenerator getMatchedXMLGenerator();
4251
}

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/ibatis2/dao/DAOGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.mybatis.generator.api.dom.java.Method;
3232
import org.mybatis.generator.api.dom.java.TopLevelClass;
3333
import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
34+
import org.mybatis.generator.codegen.AbstractXmlGenerator;
3435
import org.mybatis.generator.codegen.ibatis2.dao.elements.AbstractDAOElementGenerator;
3536
import org.mybatis.generator.codegen.ibatis2.dao.elements.CountByExampleMethodGenerator;
3637
import org.mybatis.generator.codegen.ibatis2.dao.elements.DeleteByExampleMethodGenerator;
@@ -324,4 +325,10 @@ protected void initializeAndExecuteGenerator(
324325
methodGenerator.addImplementationElements(topLevelClass);
325326
methodGenerator.addInterfaceElements(interfaze);
326327
}
328+
329+
@Override
330+
public AbstractXmlGenerator getMatchedXMLGenerator() {
331+
// this method is not called for iBATIS2
332+
return null;
333+
}
327334
}

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/mybatis3/IntrospectedTableMyBatis3Impl.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.mybatis.generator.codegen.AbstractXmlGenerator;
3131
import org.mybatis.generator.codegen.mybatis3.javamapper.AnnotatedClientGenerator;
3232
import org.mybatis.generator.codegen.mybatis3.javamapper.JavaMapperGenerator;
33+
import org.mybatis.generator.codegen.mybatis3.javamapper.MixedClientGenerator;
3334
import org.mybatis.generator.codegen.mybatis3.model.BaseRecordGenerator;
3435
import org.mybatis.generator.codegen.mybatis3.model.ExampleGenerator;
3536
import org.mybatis.generator.codegen.mybatis3.model.PrimaryKeyGenerator;
@@ -57,14 +58,22 @@ public IntrospectedTableMyBatis3Impl() {
5758
public void calculateGenerators(List<String> warnings,
5859
ProgressCallback progressCallback) {
5960
calculateJavaModelGenerators(warnings, progressCallback);
60-
if (calculateClientGenerators(warnings, progressCallback)) {
61-
calculateXmlMapperGenerator(warnings, progressCallback);
62-
}
61+
62+
AbstractJavaClientGenerator javaClientGenerator =
63+
calculateClientGenerators(warnings, progressCallback);
64+
65+
calculateXmlMapperGenerator(javaClientGenerator, warnings, progressCallback);
6366
}
6467

65-
protected void calculateXmlMapperGenerator(List<String> warnings,
68+
protected void calculateXmlMapperGenerator(AbstractJavaClientGenerator javaClientGenerator,
69+
List<String> warnings,
6670
ProgressCallback progressCallback) {
67-
xmlMapperGenerator = new XMLMapperGenerator();
71+
if (javaClientGenerator == null) {
72+
xmlMapperGenerator = new XMLMapperGenerator();
73+
} else {
74+
xmlMapperGenerator = javaClientGenerator.getMatchedXMLGenerator();
75+
}
76+
6877
initializeAbstractGenerator(xmlMapperGenerator, warnings,
6978
progressCallback);
7079
}
@@ -75,17 +84,17 @@ protected void calculateXmlMapperGenerator(List<String> warnings,
7584
* @param progressCallback
7685
* @return true if an XML generator is required
7786
*/
78-
protected boolean calculateClientGenerators(List<String> warnings,
87+
protected AbstractJavaClientGenerator calculateClientGenerators(List<String> warnings,
7988
ProgressCallback progressCallback) {
8089
AbstractJavaClientGenerator javaGenerator = createJavaClientGenerator();
8190
if (javaGenerator == null) {
82-
return true;
91+
return null;
8392
}
8493

8594
initializeAbstractGenerator(javaGenerator, warnings, progressCallback);
8695
clientGenerators.add(javaGenerator);
8796

88-
return javaGenerator.requiresXMLGenerator();
97+
return javaGenerator;
8998
}
9099

91100
private AbstractJavaClientGenerator createJavaClientGenerator() {
@@ -99,6 +108,8 @@ private AbstractJavaClientGenerator createJavaClientGenerator() {
99108
AbstractJavaClientGenerator javaGenerator;
100109
if ("XMLMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
101110
javaGenerator = new JavaMapperGenerator();
111+
} else if ("MIXEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
112+
javaGenerator = new MixedClientGenerator();
102113
} else if ("ANNOTATEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
103114
javaGenerator = new AnnotatedClientGenerator();
104115
} else if ("MAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
@@ -145,6 +156,10 @@ protected void calculateJavaModelGenerators(List<String> warnings,
145156
protected void initializeAbstractGenerator(
146157
AbstractGenerator abstractGenerator, List<String> warnings,
147158
ProgressCallback progressCallback) {
159+
if (abstractGenerator == null) {
160+
return;
161+
}
162+
148163
abstractGenerator.setContext(context);
149164
abstractGenerator.setIntrospectedTable(this);
150165
abstractGenerator.setProgressCallback(progressCallback);

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/AnnotatedClientGenerator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.mybatis.generator.api.dom.java.CompilationUnit;
66
import org.mybatis.generator.api.dom.java.Interface;
7+
import org.mybatis.generator.codegen.AbstractXmlGenerator;
78
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
89
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated.AnnotatedCountByExampleMethodGenerator;
910
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated.AnnotatedDeleteByExampleMethodGenerator;
@@ -85,7 +86,7 @@ protected void addSelectByExampleWithoutBLOBsMethod(Interface interfaze) {
8586
@Override
8687
protected void addSelectByPrimaryKeyMethod(Interface interfaze) {
8788
if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
88-
AbstractJavaMapperMethodGenerator methodGenerator = new AnnotatedSelectByPrimaryKeyMethodGenerator();
89+
AbstractJavaMapperMethodGenerator methodGenerator = new AnnotatedSelectByPrimaryKeyMethodGenerator(false);
8990
initializeAndExecuteGenerator(methodGenerator, interfaze);
9091
}
9192
}
@@ -148,4 +149,10 @@ public List<CompilationUnit> getExtraCompilationUnits() {
148149
sqlProviderGenerator.setWarnings(warnings);
149150
return sqlProviderGenerator.getCompilationUnits();
150151
}
152+
153+
@Override
154+
public AbstractXmlGenerator getMatchedXMLGenerator() {
155+
// No XML required by the annotated client
156+
return null;
157+
}
151158
}

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/JavaMapperGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.mybatis.generator.api.dom.java.Interface;
2929
import org.mybatis.generator.api.dom.java.JavaVisibility;
3030
import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
31+
import org.mybatis.generator.codegen.AbstractXmlGenerator;
3132
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
3233
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.CountByExampleMethodGenerator;
3334
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.DeleteByExampleMethodGenerator;
@@ -43,6 +44,7 @@
4344
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeySelectiveMethodGenerator;
4445
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeyWithBLOBsMethodGenerator;
4546
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeyWithoutBLOBsMethodGenerator;
47+
import org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator;
4648
import org.mybatis.generator.config.PropertyRegistry;
4749

4850
/**
@@ -229,4 +231,9 @@ protected void initializeAndExecuteGenerator(
229231
public List<CompilationUnit> getExtraCompilationUnits() {
230232
return null;
231233
}
234+
235+
@Override
236+
public AbstractXmlGenerator getMatchedXMLGenerator() {
237+
return new XMLMapperGenerator();
238+
}
232239
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2010 The MyBatis Team
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+
* http://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 org.mybatis.generator.api.dom.java.Interface;
19+
import org.mybatis.generator.codegen.AbstractXmlGenerator;
20+
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
21+
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated.AnnotatedDeleteByPrimaryKeyMethodGenerator;
22+
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated.AnnotatedInsertMethodGenerator;
23+
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated.AnnotatedSelectByPrimaryKeyMethodGenerator;
24+
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated.AnnotatedUpdateByPrimaryKeyWithBLOBsMethodGenerator;
25+
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated.AnnotatedUpdateByPrimaryKeyWithoutBLOBsMethodGenerator;
26+
import org.mybatis.generator.codegen.mybatis3.xmlmapper.MixedMapperGenerator;
27+
28+
/**
29+
* This class overrides the base mapper to provide annotated methods
30+
* for the methods that don't require SQL provider support
31+
*
32+
* @author Jeff Butler
33+
*
34+
*/
35+
public class MixedClientGenerator extends JavaMapperGenerator {
36+
37+
public MixedClientGenerator() {
38+
super(true);
39+
}
40+
41+
@Override
42+
protected void addDeleteByPrimaryKeyMethod(Interface interfaze) {
43+
if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
44+
AbstractJavaMapperMethodGenerator methodGenerator = new AnnotatedDeleteByPrimaryKeyMethodGenerator();
45+
initializeAndExecuteGenerator(methodGenerator, interfaze);
46+
}
47+
}
48+
49+
@Override
50+
protected void addInsertMethod(Interface interfaze) {
51+
if (introspectedTable.getRules().generateInsert()) {
52+
AbstractJavaMapperMethodGenerator methodGenerator = new AnnotatedInsertMethodGenerator();
53+
initializeAndExecuteGenerator(methodGenerator, interfaze);
54+
}
55+
}
56+
57+
@Override
58+
protected void addSelectByPrimaryKeyMethod(Interface interfaze) {
59+
if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
60+
AbstractJavaMapperMethodGenerator methodGenerator = new AnnotatedSelectByPrimaryKeyMethodGenerator(true);
61+
initializeAndExecuteGenerator(methodGenerator, interfaze);
62+
}
63+
}
64+
65+
@Override
66+
protected void addUpdateByPrimaryKeyWithBLOBsMethod(Interface interfaze) {
67+
if (introspectedTable.getRules().generateUpdateByPrimaryKeyWithBLOBs()) {
68+
AbstractJavaMapperMethodGenerator methodGenerator = new AnnotatedUpdateByPrimaryKeyWithBLOBsMethodGenerator();
69+
initializeAndExecuteGenerator(methodGenerator, interfaze);
70+
}
71+
}
72+
73+
@Override
74+
protected void addUpdateByPrimaryKeyWithoutBLOBsMethod(Interface interfaze) {
75+
if (introspectedTable.getRules()
76+
.generateUpdateByPrimaryKeyWithoutBLOBs()) {
77+
AbstractJavaMapperMethodGenerator methodGenerator = new AnnotatedUpdateByPrimaryKeyWithoutBLOBsMethodGenerator();
78+
initializeAndExecuteGenerator(methodGenerator, interfaze);
79+
}
80+
}
81+
82+
@Override
83+
public AbstractXmlGenerator getMatchedXMLGenerator() {
84+
return new MixedMapperGenerator();
85+
}
86+
}

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/mybatis3/javamapper/elements/annotated/AnnotatedSelectByPrimaryKeyMethodGenerator.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,18 @@
3535
*/
3636
public class AnnotatedSelectByPrimaryKeyMethodGenerator extends
3737
SelectByPrimaryKeyMethodGenerator {
38+
39+
private boolean useResultMapIfAvailable;
3840

39-
public AnnotatedSelectByPrimaryKeyMethodGenerator() {
41+
public AnnotatedSelectByPrimaryKeyMethodGenerator(boolean useResultMapIfAvailable) {
4042
super();
43+
this.useResultMapIfAvailable = useResultMapIfAvailable;
4144
}
4245

4346
@Override
4447
public void addMapperAnnotations(Interface interfaze, Method method) {
4548
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Select")); //$NON-NLS-1$
46-
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.type.JdbcType")); //$NON-NLS-1$
4749

48-
if (introspectedTable.isConstructorBased()) {
49-
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Arg")); //$NON-NLS-1$
50-
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ConstructorArgs")); //$NON-NLS-1$
51-
} else {
52-
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Result")); //$NON-NLS-1$
53-
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Results")); //$NON-NLS-1$
54-
}
55-
5650
StringBuilder sb = new StringBuilder();
5751
method.addAnnotation("@Select({"); //$NON-NLS-1$
5852
javaIndent(sb, 1);
@@ -122,13 +116,43 @@ public void addMapperAnnotations(Interface interfaze, Method method) {
122116
}
123117

124118
method.addAnnotation("})"); //$NON-NLS-1$
119+
120+
if (useResultMapIfAvailable) {
121+
if (introspectedTable.getRules().generateBaseResultMap()
122+
|| introspectedTable.getRules().generateResultMapWithBLOBs()) {
123+
addResultMapAnnotation(interfaze, method);
124+
} else {
125+
addAnnotatedResults(interfaze, method);
126+
}
127+
} else {
128+
addAnnotatedResults(interfaze, method);
129+
}
130+
}
131+
132+
private void addResultMapAnnotation(Interface interfaze, Method method) {
133+
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ResultMap")); //$NON-NLS-1$
134+
135+
String annotation = String.format("@ResultMap(\"%s\")",
136+
introspectedTable.getRules().generateResultMapWithBLOBs() ?
137+
introspectedTable.getResultMapWithBLOBsId() : introspectedTable.getBaseResultMapId());
138+
method.addAnnotation(annotation);
139+
}
140+
141+
private void addAnnotatedResults(Interface interfaze, Method method) {
142+
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.type.JdbcType")); //$NON-NLS-1$
125143

126144
if (introspectedTable.isConstructorBased()) {
145+
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Arg")); //$NON-NLS-1$
146+
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ConstructorArgs")); //$NON-NLS-1$
127147
method.addAnnotation("@ConstructorArgs({"); //$NON-NLS-1$
128148
} else {
149+
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Result")); //$NON-NLS-1$
150+
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Results")); //$NON-NLS-1$
129151
method.addAnnotation("@Results({"); //$NON-NLS-1$
130152
}
131153

154+
StringBuilder sb = new StringBuilder();
155+
132156
Iterator<IntrospectedColumn> iterPk = introspectedTable.getPrimaryKeyColumns().iterator();
133157
Iterator<IntrospectedColumn> iterNonPk = introspectedTable.getNonPrimaryKeyColumns().iterator();
134158
while (iterPk.hasNext()) {

0 commit comments

Comments
 (0)