Skip to content

Commit f9291e4

Browse files
committed
[generator] tests and fixes for new simple target runtime
1 parent 3cc027d commit f9291e4

File tree

16 files changed

+843
-30
lines changed

16 files changed

+843
-30
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.mybatis.generator.api.dom.java.Interface;
2727
import org.mybatis.generator.api.dom.java.Method;
2828
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectAllMethodGenerator;
29+
import org.mybatis.generator.config.PropertyRegistry;
30+
import org.mybatis.generator.internal.util.StringUtility;
2931

3032
/**
3133
*
@@ -78,14 +80,29 @@ public void addMapperAnnotations(Interface interfaze, Method method) {
7880
method.addAnnotation(sb.toString());
7981
}
8082

83+
String orderByClause = introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_SELECT_ALL_ORDER_BY_CLAUSE);
84+
boolean hasOrderBy = StringUtility.stringHasValue(orderByClause);
85+
8186
sb.setLength(0);
8287
javaIndent(sb, 1);
8388
sb.append("\"from "); //$NON-NLS-1$
8489
sb.append(escapeStringForJava(introspectedTable
8590
.getAliasedFullyQualifiedTableNameAtRuntime()));
86-
sb.append("\""); //$NON-NLS-1$
91+
sb.append('\"');
92+
if (hasOrderBy) {
93+
sb.append(',');
94+
}
8795
method.addAnnotation(sb.toString());
8896

97+
if (hasOrderBy) {
98+
sb.setLength(0);
99+
javaIndent(sb, 1);
100+
sb.append("\"order by "); //$NON-NLS-1$
101+
sb.append(orderByClause);
102+
sb.append('\"');
103+
method.addAnnotation(sb.toString());
104+
}
105+
89106
method.addAnnotation("})"); //$NON-NLS-1$
90107

91108
addAnnotatedResults(interfaze, method);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,20 @@ protected void addSelectByPrimaryKeyElement(XmlElement parentElement) {
7878
}
7979

8080
protected void addSelectAllElement(XmlElement parentElement) {
81-
if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
82-
AbstractXmlElementGenerator elementGenerator = new SimpleSelectAllElementGenerator();
83-
initializeAndExecuteGenerator(elementGenerator, parentElement);
84-
}
81+
AbstractXmlElementGenerator elementGenerator = new SimpleSelectAllElementGenerator();
82+
initializeAndExecuteGenerator(elementGenerator, parentElement);
8583
}
8684

8785
protected void addDeleteByPrimaryKeyElement(XmlElement parentElement) {
8886
if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
89-
AbstractXmlElementGenerator elementGenerator = new DeleteByPrimaryKeyElementGenerator();
87+
AbstractXmlElementGenerator elementGenerator = new DeleteByPrimaryKeyElementGenerator(true);
9088
initializeAndExecuteGenerator(elementGenerator, parentElement);
9189
}
9290
}
9391

9492
protected void addInsertElement(XmlElement parentElement) {
9593
if (introspectedTable.getRules().generateInsert()) {
96-
AbstractXmlElementGenerator elementGenerator = new InsertElementGenerator();
94+
AbstractXmlElementGenerator elementGenerator = new InsertElementGenerator(true);
9795
initializeAndExecuteGenerator(elementGenerator, parentElement);
9896
}
9997
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ protected void addDeleteByExampleElement(XmlElement parentElement) {
167167

168168
protected void addDeleteByPrimaryKeyElement(XmlElement parentElement) {
169169
if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
170-
AbstractXmlElementGenerator elementGenerator = new DeleteByPrimaryKeyElementGenerator();
170+
AbstractXmlElementGenerator elementGenerator = new DeleteByPrimaryKeyElementGenerator(false);
171171
initializeAndExecuteGenerator(elementGenerator, parentElement);
172172
}
173173
}
174174

175175
protected void addInsertElement(XmlElement parentElement) {
176176
if (introspectedTable.getRules().generateInsert()) {
177-
AbstractXmlElementGenerator elementGenerator = new InsertElementGenerator();
177+
AbstractXmlElementGenerator elementGenerator = new InsertElementGenerator(false);
178178
initializeAndExecuteGenerator(elementGenerator, parentElement);
179179
}
180180
}

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/DeleteByPrimaryKeyElementGenerator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
public class DeleteByPrimaryKeyElementGenerator extends
3030
AbstractXmlElementGenerator {
3131

32-
public DeleteByPrimaryKeyElementGenerator() {
32+
private boolean isSimple;
33+
34+
public DeleteByPrimaryKeyElementGenerator(boolean isSimple) {
3335
super();
36+
this.isSimple = isSimple;
3437
}
3538

3639
@Override
@@ -40,7 +43,7 @@ public void addElements(XmlElement parentElement) {
4043
answer.addAttribute(new Attribute(
4144
"id", introspectedTable.getDeleteByPrimaryKeyStatementId())); //$NON-NLS-1$
4245
String parameterClass;
43-
if (introspectedTable.getRules().generatePrimaryKeyClass()) {
46+
if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
4447
parameterClass = introspectedTable.getPrimaryKeyType();
4548
} else {
4649
// PK fields are in the base class. If more than on PK

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/InsertElementGenerator.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
*/
3636
public class InsertElementGenerator extends AbstractXmlElementGenerator {
3737

38-
public InsertElementGenerator() {
38+
private boolean isSimple;
39+
40+
public InsertElementGenerator(boolean isSimple) {
3941
super();
42+
this.isSimple = isSimple;
4043
}
4144

4245
@Override
@@ -46,8 +49,14 @@ public void addElements(XmlElement parentElement) {
4649
answer.addAttribute(new Attribute(
4750
"id", introspectedTable.getInsertStatementId())); //$NON-NLS-1$
4851

49-
FullyQualifiedJavaType parameterType = introspectedTable.getRules()
50-
.calculateAllFieldsClass();
52+
FullyQualifiedJavaType parameterType;
53+
if (isSimple) {
54+
parameterType = new FullyQualifiedJavaType(
55+
introspectedTable.getBaseRecordType());
56+
} else {
57+
parameterType = introspectedTable.getRules()
58+
.calculateAllFieldsClass();
59+
}
5160

5261
answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
5362
parameterType.getFullyQualifiedName()));
@@ -57,19 +66,21 @@ public void addElements(XmlElement parentElement) {
5766
GeneratedKey gk = introspectedTable.getGeneratedKey();
5867
if (gk != null) {
5968
IntrospectedColumn introspectedColumn = introspectedTable
60-
.getColumn(gk.getColumn());
69+
.getColumn(gk.getColumn());
6170
// if the column is null, then it's a configuration error. The
6271
// warning has already been reported
6372
if (introspectedColumn != null) {
6473
if (gk.isJdbcStandard()) {
65-
answer.addAttribute(new Attribute("useGeneratedKeys", "true")); //$NON-NLS-1$ //$NON-NLS-2$
66-
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
74+
answer.addAttribute(new Attribute(
75+
"useGeneratedKeys", "true")); //$NON-NLS-1$ //$NON-NLS-2$
76+
answer.addAttribute(new Attribute(
77+
"keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
6778
} else {
6879
answer.addElement(getSelectKey(introspectedColumn, gk));
6980
}
7081
}
7182
}
72-
83+
7384
StringBuilder insertClause = new StringBuilder();
7485
StringBuilder valuesClause = new StringBuilder();
7586

mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/SimpleSelectAllElementGenerator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.mybatis.generator.api.dom.xml.TextElement;
2323
import org.mybatis.generator.api.dom.xml.XmlElement;
2424
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
25+
import org.mybatis.generator.config.PropertyRegistry;
26+
import org.mybatis.generator.internal.util.StringUtility;
2527

2628
/**
2729
*
@@ -73,6 +75,15 @@ public void addElements(XmlElement parentElement) {
7375
sb.append(introspectedTable
7476
.getAliasedFullyQualifiedTableNameAtRuntime());
7577
answer.addElement(new TextElement(sb.toString()));
78+
79+
String orderByClause = introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_SELECT_ALL_ORDER_BY_CLAUSE);
80+
boolean hasOrderBy = StringUtility.stringHasValue(orderByClause);
81+
if (hasOrderBy) {
82+
sb.setLength(0);
83+
sb.append("order by "); //$NON-NLS-1$
84+
sb.append(orderByClause);
85+
answer.addElement(new TextElement(sb.toString()));
86+
}
7687

7788
if (context.getPlugins().sqlMapSelectAllElementGenerated(
7889
answer, introspectedTable)) {

mybatis-generator-core/src/main/java/org/mybatis/generator/config/PropertyRegistry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class PropertyRegistry {
4747
public static final String TABLE_RUNTIME_SCHEMA = "runtimeSchema"; //$NON-NLS-1$
4848
public static final String TABLE_RUNTIME_TABLE_NAME = "runtimeTableName"; //$NON-NLS-1$
4949
public static final String TABLE_MODEL_ONLY = "modelOnly"; //$NON-NLS-1$
50+
public static final String TABLE_SELECT_ALL_ORDER_BY_CLAUSE = "selectAllOrderByClause"; //$NON-NLS-1$
5051

5152
public static final String CONTEXT_BEGINNING_DELIMITER = "beginningDelimiter"; //$NON-NLS-1$
5253
public static final String CONTEXT_ENDING_DELIMITER = "endingDelimiter"; //$NON-NLS-1$

mybatis-generator-core/src/site/xhtml/configreference/table.xhtml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ specified with the <a href="property.html">&lt;property&gt;</a> child element:</
371371
this property. You should also specify the <code>ignoreQualifiersAtRuntime</code>
372372
property in most cases with public synonyms.</td>
373373
</tr>
374+
<tr>
375+
<td valign="top">selectAllOrderByClause</td>
376+
<td>This property can be used to specify an order by clause that will be added to
377+
the <code>selectAll</code> method. This is only applicable if you are using the
378+
<code>MyBatis3Simple</code> target runtime. MBG will prepend <code>order by</code>
379+
to anything you specify in this property, so the property should contain
380+
a column list only (e.g <code>ID1, ID2</code> or <code>ID1 desc, ID2 asc</code>)
381+
</td>
382+
</tr>
374383
<tr>
375384
<td valign="top">useActualColumnNames</td>
376385
<td>If true, then

mybatis-generator-core/src/site/xhtml/license.xhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
you may not use this product except in compliance with the License.
1313
You may obtain a copy of the License at
1414
</p>
15-
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.apache.org/licenses/LICENSE-2.0">
15+
<p><a href="http://www.apache.org/licenses/LICENSE-2.0">
1616
http://www.apache.org/licenses/LICENSE-2.0</a></p>
1717
<p>
1818
Unless required by applicable law or agreed to in writing, software

mybatis-generator-core/src/site/xhtml/whatsNew.xhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
fields are null - thanks to Benjamin Klatt for finding this bug.</li>
197197
<li>IBATIS-601 - improper validation of &lt;generatedKey&gt;</li>
198198
<li>IBATIS-609 - incorrect parsing of Java generic types</li>
199-
<li>Fixed spelling error LONCVARCHAR->LONGVARCHAR (thanks Allard)</li>
199+
<li>Fixed spelling error LONCVARCHAR to LONGVARCHAR (thanks Allard)</li>
200200
<li>Fixed IBATIS-731 - change name of primary key variable to avoid conflicts</li>
201201
<li>Fixed IBATIS-699 - Overwrite unmergeable XML files if enabled</li>
202202
<li>Fixed issue where insertSelective failed if there is a sequence generating the

0 commit comments

Comments
 (0)