Skip to content

Commit

Permalink
add resultmapId to @many and @one
Browse files Browse the repository at this point in the history
  • Loading branch information
moonService committed Dec 10, 2019
1 parent bf01fee commit e05d686
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/Many.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Many {
/**
* Returns the id that retrieves result map.
*
* @return the id that retrieves result map.
*/
String resultMapId() default "";
/**
* Returns the statement id that retrieves collection.
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/One.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface One {
/**
* Returns the id that retrieves result map.
*
* @return the id that retrieves result map.
*/
String resultMapId() default "";
/**
* Returns the statement id that retrieves single object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
result.javaType() == void.class ? null : result.javaType(),
result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(),
hasNestedSelect(result) ? nestedSelectId(result) : null,
null,
hasNestedResultMap(result) ? nestedResultMapId(result) : null,
null,
null,
typeHandler,
Expand All @@ -564,6 +564,24 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
}
}

private String nestedResultMapId(Result result) {
String resultMapId = result.one().resultMapId();
if (resultMapId.length() < 1) {
resultMapId = result.many().resultMapId();
}
if (!resultMapId.contains(".")) {
resultMapId = type.getName() + "." + resultMapId;
}
return resultMapId;
}

private boolean hasNestedResultMap(Result result) {
if (result.one().resultMapId().length() > 0 && result.many().resultMapId().length() > 0) {
throw new BuilderException("Cannot use both @One and @Many annotations in the same @Result");
}
return result.one().resultMapId().length() > 0 || result.many().resultMapId().length() > 0;
}

private String nestedSelectId(Result result) {
String nestedSelect = result.one().select();
if (nestedSelect.length() < 1) {
Expand Down

0 comments on commit e05d686

Please sign in to comment.