Skip to content

Commit

Permalink
Change to use the Methd#getAnnotationsByType for getting arrays of @R…
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuki43zoo committed Oct 31, 2019
1 parent 8409126 commit 4bc8e95
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 100 deletions.
Expand Up @@ -39,7 +39,6 @@
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.CacheNamespaceRef;
import org.apache.ibatis.annotations.Case;
import org.apache.ibatis.annotations.ConstructorArgs;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
Expand Down Expand Up @@ -228,13 +227,11 @@ private void parseCacheRef() {

private String parseResultMap(Method method) {
Class<?> returnType = getReturnType(method);
ConstructorArgs args = method.getAnnotation(ConstructorArgs.class);
Arg arg = method.getAnnotation(Arg.class);
Results results = method.getAnnotation(Results.class);
Result result = method.getAnnotation(Result.class);
Arg[] args = method.getAnnotationsByType(Arg.class);
Result[] results = method.getAnnotationsByType(Result.class);
TypeDiscriminator typeDiscriminator = method.getAnnotation(TypeDiscriminator.class);
String resultMapId = generateResultMapName(method);
applyResultMap(resultMapId, returnType, argsIf(args, arg, method), resultsIf(results, result, method), typeDiscriminator);
applyResultMap(resultMapId, returnType, args, results, typeDiscriminator);
return resultMapId;
}

Expand Down Expand Up @@ -628,26 +625,6 @@ private String nullOrEmpty(String value) {
return value == null || value.trim().length() == 0 ? null : value;
}

private Result[] resultsIf(Results results, Result result, Method method) {
if (results != null && result != null) {
throw new BuilderException("Cannot use both @Results and @Result annotations with together at '" + method + "'.");
}
if (result != null) {
return new Result[]{result};
}
return results == null ? new Result[0] : results.value();
}

private Arg[] argsIf(ConstructorArgs args, Arg arg, Method method) {
if (args != null && arg != null) {
throw new BuilderException("Cannot use both @ConstructorArgs and @Arg annotations with together at '" + method + "'.");
}
if (arg != null) {
return new Arg[]{arg};
}
return args == null ? new Arg[0] : args.value();
}

private KeyGenerator handleSelectKeyAnnotation(SelectKey selectKeyAnnotation, String baseStatementId, Class<?> parameterTypeClass, LanguageDriver languageDriver) {
String id = baseStatementId + SelectKeyGenerator.SELECT_KEY_SUFFIX;
Class<?> resultTypeClass = selectKeyAnnotation.resultType();
Expand Down
34 changes: 26 additions & 8 deletions src/test/java/org/apache/ibatis/binding/BindingTest.java
Expand Up @@ -743,20 +743,38 @@ void shouldMapUsingSingleRepeatableAnnotation() {
}

@Test
void shouldErrorWhenSpecifyBothArgAndConstructorArgs() {
void shouldMapWhenSpecifyBothArgAndConstructorArgs() {
try (SqlSession session = sqlSessionFactory.openSession()) {
BuilderException exception = Assertions.assertThrows(BuilderException.class,()
-> session.getConfiguration().addMapper(WrongArgAndConstructorArgsMapper.class));
Assertions.assertEquals("Cannot use both @ConstructorArgs and @Arg annotations with together at 'public abstract org.apache.ibatis.domain.blog.Author org.apache.ibatis.binding.WrongArgAndConstructorArgsMapper.selectAuthor(int)'.", exception.getMessage());
BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
Author author = new Author(-1, "cbegin", "******", "cbegin@nowhere.com", "N/A", Section.NEWS);
mapper.insertAuthor(author);
Author author2 = mapper.selectAuthorUsingBothArgAndConstructorArgs(author.getId());
assertNotNull(author2);
assertEquals(author.getId(), author2.getId());
assertEquals(author.getUsername(), author2.getUsername());
assertEquals(author.getPassword(), author2.getPassword());
assertEquals(author.getBio(), author2.getBio());
assertEquals(author.getEmail(), author2.getEmail());
assertEquals(author.getFavouriteSection(), author2.getFavouriteSection());
session.rollback();
}
}

@Test
void shouldErrorWhenSpecifyBothResultAndResults() {
void shouldMapWhenSpecifyBothResultAndResults() {
try (SqlSession session = sqlSessionFactory.openSession()) {
BuilderException exception = Assertions.assertThrows(BuilderException.class,()
-> session.getConfiguration().addMapper(WrongResultAndResultsMapper.class));
Assertions.assertEquals("Cannot use both @Results and @Result annotations with together at 'public abstract org.apache.ibatis.domain.blog.Author org.apache.ibatis.binding.WrongResultAndResultsMapper.selectAuthor(int)'.", exception.getMessage());
BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
Author author = new Author(-1, "cbegin", "******", "cbegin@nowhere.com", "N/A", Section.NEWS);
mapper.insertAuthor(author);
Author author2 = mapper.selectAuthorUsingBothResultAndResults(author.getId());
assertNotNull(author2);
assertEquals(author.getId(), author2.getId());
assertEquals(author.getUsername(), author2.getUsername());
assertNull(author2.getPassword());
assertNull(author2.getBio());
assertNull(author2.getEmail());
assertNull(author2.getFavouriteSection());
session.rollback();
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/apache/ibatis/binding/BoundAuthorMapper.java
Expand Up @@ -139,6 +139,40 @@ public interface BoundAuthorMapper {

//======================================================

@ConstructorArgs({
@Arg(column = "AUTHOR_ID", javaType = Integer.class),
@Arg(column = "AUTHOR_USERNAME", javaType = String.class),
@Arg(column = "AUTHOR_PASSWORD", javaType = String.class),
@Arg(column = "AUTHOR_EMAIL", javaType = String.class),
@Arg(column = "AUTHOR_BIO", javaType = String.class)
})
@Arg(column = "AUTHOR_SECTION", javaType = Section.class)
@Select({
"SELECT ",
" ID as AUTHOR_ID,",
" USERNAME as AUTHOR_USERNAME,",
" PASSWORD as AUTHOR_PASSWORD,",
" EMAIL as AUTHOR_EMAIL,",
" BIO as AUTHOR_BIO," +
" FAVOURITE_SECTION as AUTHOR_SECTION",
"FROM AUTHOR WHERE ID = #{id}"})
Author selectAuthorUsingBothArgAndConstructorArgs(int id);

//======================================================

@Results(
@Result(property = "id", column = "AUTHOR_ID")
)
@Result(property = "username", column = "AUTHOR_USERNAME")
@Select({
"SELECT ",
" ID as AUTHOR_ID,",
" USERNAME as AUTHOR_USERNAME",
"FROM AUTHOR WHERE ID = #{id}"})
Author selectAuthorUsingBothResultAndResults(int id);

//======================================================

List<Post> findThreeSpecificPosts(@Param("one") int one,
RowBounds rowBounds,
@Param("two") int two,
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 4bc8e95

Please sign in to comment.