28
28
import org .apache .ibatis .annotations .Result ;
29
29
import org .apache .ibatis .annotations .Results ;
30
30
import org .apache .ibatis .annotations .Select ;
31
+ import org .apache .ibatis .annotations .SelectKey ;
31
32
import org .apache .ibatis .annotations .SelectProvider ;
32
33
import org .apache .ibatis .annotations .TypeDiscriminator ;
33
34
import org .apache .ibatis .annotations .Update ;
43
44
import org .apache .ibatis .executor .keygen .Jdbc3KeyGenerator ;
44
45
import org .apache .ibatis .executor .keygen .KeyGenerator ;
45
46
import org .apache .ibatis .executor .keygen .NoKeyGenerator ;
47
+ import org .apache .ibatis .executor .keygen .SelectKeyGenerator ;
46
48
import org .apache .ibatis .io .Resources ;
47
49
import org .apache .ibatis .mapping .Discriminator ;
50
+ import org .apache .ibatis .mapping .MappedStatement ;
48
51
import org .apache .ibatis .mapping .ResultFlag ;
49
52
import org .apache .ibatis .mapping .ResultMapping ;
50
53
import org .apache .ibatis .mapping .ResultSetType ;
@@ -113,14 +116,14 @@ private void loadXmlResource() {
113
116
}
114
117
115
118
private void parseCache () {
116
- CacheNamespace cacheDomain = ( CacheNamespace ) type .getAnnotation (CacheNamespace .class );
119
+ CacheNamespace cacheDomain = type .getAnnotation (CacheNamespace .class );
117
120
if (cacheDomain != null ) {
118
121
assistant .useNewCache (cacheDomain .implementation (), cacheDomain .eviction (), cacheDomain .flushInterval (), cacheDomain .size (), cacheDomain .readWrite (), null );
119
122
}
120
123
}
121
124
122
125
private void parseCacheRef () {
123
- CacheNamespaceRef cacheDomainRef = ( CacheNamespaceRef ) type .getAnnotation (CacheNamespaceRef .class );
126
+ CacheNamespaceRef cacheDomainRef = type .getAnnotation (CacheNamespaceRef .class );
124
127
if (cacheDomainRef != null ) {
125
128
assistant .useCacheRef (cacheDomainRef .value ().getName ());
126
129
}
@@ -218,19 +221,40 @@ private void parseStatement(Method method) {
218
221
StatementType statementType = StatementType .PREPARED ;
219
222
ResultSetType resultSetType = ResultSetType .FORWARD_ONLY ;
220
223
SqlCommandType sqlCommandType = getSqlCommandType (method );
221
- KeyGenerator keyGenerator = configuration . isUseGeneratedKeys ()
222
- && SqlCommandType . INSERT . equals ( sqlCommandType ) ? new Jdbc3KeyGenerator () : new NoKeyGenerator () ;
224
+
225
+ KeyGenerator keyGenerator ;
223
226
String keyProperty = "id" ;
227
+ if (SqlCommandType .INSERT .equals (sqlCommandType )) {
228
+ // first check for SelectKey annotation - that overrides everything else
229
+ SelectKey selectKey = method .getAnnotation (SelectKey .class );
230
+ if (selectKey != null ) {
231
+ keyGenerator = handleSelectKeyAnnotation (selectKey , mappedStatementId , getParameterType (method ));
232
+ keyProperty = selectKey .keyProperty ();
233
+ } else {
234
+ if (configuration .isUseGeneratedKeys ()) {
235
+ if (options == null ) {
236
+ keyGenerator = new Jdbc3KeyGenerator ();
237
+ } else {
238
+ keyProperty = options .keyProperty ();
239
+ keyGenerator = options .useGeneratedKeys () ? new Jdbc3KeyGenerator () : new NoKeyGenerator ();
240
+ }
241
+ } else {
242
+ keyGenerator = new NoKeyGenerator ();
243
+ }
244
+ }
245
+ } else {
246
+ keyGenerator = new NoKeyGenerator ();
247
+ }
248
+
224
249
if (options != null ) {
225
250
flushCache = options .flushCache ();
226
251
useCache = options .useCache ();
227
252
fetchSize = options .fetchSize () > -1 ? options .fetchSize () : null ;
228
253
timeout = options .timeout () > -1 ? options .timeout () : null ;
229
254
statementType = options .statementType ();
230
255
resultSetType = options .resultSetType ();
231
- keyGenerator = options .useGeneratedKeys () ? new Jdbc3KeyGenerator () : new NoKeyGenerator ();
232
- keyProperty = options .keyProperty ();
233
256
}
257
+
234
258
assistant .addMappedStatement (
235
259
mappedStatementId ,
236
260
sqlSource ,
@@ -304,15 +328,7 @@ private SqlSource getSqlSourceFromAnnotations(Method method) {
304
328
}
305
329
Annotation sqlAnnotation = method .getAnnotation (sqlAnnotationType );
306
330
final String [] strings = (String []) sqlAnnotation .getClass ().getMethod ("value" ).invoke (sqlAnnotation );
307
- final StringBuilder sql = new StringBuilder ();
308
- for (String fragment : strings ) {
309
- sql .append (fragment );
310
- sql .append (" " );
311
- }
312
- ArrayList <SqlNode > contents = new ArrayList <SqlNode >();
313
- contents .add (new TextSqlNode (sql .toString ()));
314
- MixedSqlNode rootSqlNode = new MixedSqlNode (contents );
315
- return new DynamicSqlSource (configuration , rootSqlNode );
331
+ return buildSqlSourceFromStrings (strings );
316
332
} else if (sqlProviderAnnotationType != null ) {
317
333
Annotation sqlProviderAnnotation = method .getAnnotation (sqlProviderAnnotationType );
318
334
return new ProviderSqlSource (assistant .getConfiguration (), sqlProviderAnnotation );
@@ -323,6 +339,18 @@ private SqlSource getSqlSourceFromAnnotations(Method method) {
323
339
}
324
340
}
325
341
342
+ private SqlSource buildSqlSourceFromStrings (String [] strings ) {
343
+ final StringBuilder sql = new StringBuilder ();
344
+ for (String fragment : strings ) {
345
+ sql .append (fragment );
346
+ sql .append (" " );
347
+ }
348
+ ArrayList <SqlNode > contents = new ArrayList <SqlNode >();
349
+ contents .add (new TextSqlNode (sql .toString ()));
350
+ MixedSqlNode rootSqlNode = new MixedSqlNode (contents );
351
+ return new DynamicSqlSource (configuration , rootSqlNode );
352
+ }
353
+
326
354
private SqlCommandType getSqlCommandType (Method method ) {
327
355
Class <? extends Annotation > type = getSqlAnnotationType (method );
328
356
@@ -431,4 +459,35 @@ private Arg[] argsIf(ConstructorArgs args) {
431
459
return args == null ? new Arg [0 ] : args .value ();
432
460
}
433
461
462
+ private KeyGenerator handleSelectKeyAnnotation (SelectKey selectKeyAnnotation , String baseStatementId , Class <?> parameterTypeClass ) {
463
+ String id = baseStatementId + SelectKeyGenerator .SELECT_KEY_SUFFIX ;
464
+ Class <?> resultTypeClass = selectKeyAnnotation .resultType ();
465
+ StatementType statementType = selectKeyAnnotation .statementType ();
466
+ String keyProperty = selectKeyAnnotation .keyProperty ();
467
+ boolean executeBefore = selectKeyAnnotation .before ();
468
+
469
+ // defaults
470
+ boolean useCache = false ;
471
+ KeyGenerator keyGenerator = new NoKeyGenerator ();
472
+ Integer fetchSize = null ;
473
+ Integer timeout = null ;
474
+ boolean flushCache = false ;
475
+ String parameterMap = null ;
476
+ String resultMap = null ;
477
+ ResultSetType resultSetTypeEnum = null ;
478
+
479
+ SqlSource sqlSource = buildSqlSourceFromStrings (selectKeyAnnotation .statement ());
480
+ SqlCommandType sqlCommandType = SqlCommandType .SELECT ;
481
+
482
+ assistant .addMappedStatement (id , sqlSource , statementType , sqlCommandType ,
483
+ fetchSize , timeout , parameterMap , parameterTypeClass , resultMap , resultTypeClass ,
484
+ resultSetTypeEnum , flushCache , useCache , keyGenerator , keyProperty );
485
+
486
+ id = assistant .applyCurrentNamespace (id );
487
+
488
+ MappedStatement keyStatement = configuration .getMappedStatement (id );
489
+ SelectKeyGenerator answer = new SelectKeyGenerator (keyStatement , executeBefore );
490
+ configuration .addKeyGenerator (id , answer );
491
+ return answer ;
492
+ }
434
493
}
0 commit comments