1
1
/*
2
- * Copyright 2009-2024 the original author or authors.
2
+ * Copyright 2009-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
63
63
import org .apache .ibatis .builder .CacheRefResolver ;
64
64
import org .apache .ibatis .builder .IncompleteElementException ;
65
65
import org .apache .ibatis .builder .MapperBuilderAssistant ;
66
+ import org .apache .ibatis .builder .ResultMappingConstructorResolver ;
66
67
import org .apache .ibatis .builder .xml .XMLMapperBuilder ;
67
68
import org .apache .ibatis .cursor .Cursor ;
68
69
import org .apache .ibatis .executor .keygen .Jdbc3KeyGenerator ;
@@ -237,7 +238,7 @@ private String generateResultMapName(Method method) {
237
238
private void applyResultMap (String resultMapId , Class <?> returnType , Arg [] args , Result [] results ,
238
239
TypeDiscriminator discriminator ) {
239
240
List <ResultMapping > resultMappings = new ArrayList <>();
240
- applyConstructorArgs (args , returnType , resultMappings );
241
+ applyConstructorArgs (args , returnType , resultMappings , resultMapId );
241
242
applyResults (results , returnType , resultMappings );
242
243
Discriminator disc = applyDiscriminator (resultMapId , returnType , discriminator );
243
244
// TODO add AutoMappingBehaviour
@@ -251,7 +252,7 @@ private void createDiscriminatorResultMaps(String resultMapId, Class<?> resultTy
251
252
String caseResultMapId = resultMapId + "-" + c .value ();
252
253
List <ResultMapping > resultMappings = new ArrayList <>();
253
254
// issue #136
254
- applyConstructorArgs (c .constructArgs (), resultType , resultMappings );
255
+ applyConstructorArgs (c .constructArgs (), resultType , resultMappings , resultMapId );
255
256
applyResults (c .results (), resultType , resultMappings );
256
257
// TODO add AutoMappingBehaviour
257
258
assistant .addResultMap (caseResultMapId , c .type (), resultMapId , null , resultMappings , null );
@@ -461,15 +462,15 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
461
462
462
463
private String findColumnPrefix (Result result ) {
463
464
String columnPrefix = result .one ().columnPrefix ();
464
- if (columnPrefix .length () < 1 ) {
465
+ if (columnPrefix .isEmpty () ) {
465
466
columnPrefix = result .many ().columnPrefix ();
466
467
}
467
468
return columnPrefix ;
468
469
}
469
470
470
471
private String nestedResultMapId (Result result ) {
471
472
String resultMapId = result .one ().resultMap ();
472
- if (resultMapId .length () < 1 ) {
473
+ if (resultMapId .isEmpty () ) {
473
474
resultMapId = result .many ().resultMap ();
474
475
}
475
476
if (!resultMapId .contains ("." )) {
@@ -479,15 +480,15 @@ private String nestedResultMapId(Result result) {
479
480
}
480
481
481
482
private boolean hasNestedResultMap (Result result ) {
482
- if (result .one ().resultMap ().length () > 0 && result .many ().resultMap ().length () > 0 ) {
483
+ if (! result .one ().resultMap ().isEmpty () && ! result .many ().resultMap ().isEmpty () ) {
483
484
throw new BuilderException ("Cannot use both @One and @Many annotations in the same @Result" );
484
485
}
485
- return result .one ().resultMap ().length () > 0 || result .many ().resultMap ().length () > 0 ;
486
+ return ! result .one ().resultMap ().isEmpty () || ! result .many ().resultMap ().isEmpty () ;
486
487
}
487
488
488
489
private String nestedSelectId (Result result ) {
489
490
String nestedSelect = result .one ().select ();
490
- if (nestedSelect .length () < 1 ) {
491
+ if (nestedSelect .isEmpty () ) {
491
492
nestedSelect = result .many ().select ();
492
493
}
493
494
if (!nestedSelect .contains ("." )) {
@@ -498,22 +499,24 @@ private String nestedSelectId(Result result) {
498
499
499
500
private boolean isLazy (Result result ) {
500
501
boolean isLazy = configuration .isLazyLoadingEnabled ();
501
- if (result .one ().select ().length () > 0 && FetchType .DEFAULT != result .one ().fetchType ()) {
502
+ if (! result .one ().select ().isEmpty () && FetchType .DEFAULT != result .one ().fetchType ()) {
502
503
isLazy = result .one ().fetchType () == FetchType .LAZY ;
503
- } else if (result .many ().select ().length () > 0 && FetchType .DEFAULT != result .many ().fetchType ()) {
504
+ } else if (! result .many ().select ().isEmpty () && FetchType .DEFAULT != result .many ().fetchType ()) {
504
505
isLazy = result .many ().fetchType () == FetchType .LAZY ;
505
506
}
506
507
return isLazy ;
507
508
}
508
509
509
510
private boolean hasNestedSelect (Result result ) {
510
- if (result .one ().select ().length () > 0 && result .many ().select ().length () > 0 ) {
511
+ if (! result .one ().select ().isEmpty () && ! result .many ().select ().isEmpty () ) {
511
512
throw new BuilderException ("Cannot use both @One and @Many annotations in the same @Result" );
512
513
}
513
- return result .one ().select ().length () > 0 || result .many ().select ().length () > 0 ;
514
+ return ! result .one ().select ().isEmpty () || ! result .many ().select ().isEmpty () ;
514
515
}
515
516
516
- private void applyConstructorArgs (Arg [] args , Class <?> resultType , List <ResultMapping > resultMappings ) {
517
+ private void applyConstructorArgs (Arg [] args , Class <?> resultType , List <ResultMapping > resultMappings ,
518
+ String resultMapId ) {
519
+ final List <ResultMapping > mappings = new ArrayList <>();
517
520
for (Arg arg : args ) {
518
521
List <ResultFlag > flags = new ArrayList <>();
519
522
flags .add (ResultFlag .CONSTRUCTOR );
@@ -527,12 +530,16 @@ private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMa
527
530
nullOrEmpty (arg .column ()), arg .javaType () == void .class ? null : arg .javaType (),
528
531
arg .jdbcType () == JdbcType .UNDEFINED ? null : arg .jdbcType (), nullOrEmpty (arg .select ()),
529
532
nullOrEmpty (arg .resultMap ()), null , nullOrEmpty (arg .columnPrefix ()), typeHandler , flags , null , null , false );
530
- resultMappings .add (resultMapping );
533
+ mappings .add (resultMapping );
531
534
}
535
+
536
+ final ResultMappingConstructorResolver resolver = new ResultMappingConstructorResolver (configuration , mappings ,
537
+ resultType , resultMapId );
538
+ resultMappings .addAll (resolver .resolveWithConstructor ());
532
539
}
533
540
534
541
private String nullOrEmpty (String value ) {
535
- return value == null || value .trim ().length () == 0 ? null : value ;
542
+ return value == null || value .trim ().isEmpty () ? null : value ;
536
543
}
537
544
538
545
private KeyGenerator handleSelectKeyAnnotation (SelectKey selectKeyAnnotation , String baseStatementId ,
0 commit comments