Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support repeatable annotation on @Arg and @Result #1698

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/org/apache/ibatis/annotations/Arg.java
Expand Up @@ -16,6 +16,8 @@
package org.apache.ibatis.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -32,7 +34,8 @@
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
@Target(ElementType.METHOD)
@Repeatable(ConstructorArgs.class)
public @interface Arg {

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/apache/ibatis/annotations/Result.java
Expand Up @@ -16,6 +16,8 @@
package org.apache.ibatis.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -32,7 +34,8 @@
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
@Target(ElementType.METHOD)
@Repeatable(Results.class)
public @interface Result {
/**
* Returns whether id column or not.
Expand Down
Expand Up @@ -229,10 +229,12 @@ 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);
TypeDiscriminator typeDiscriminator = method.getAnnotation(TypeDiscriminator.class);
String resultMapId = generateResultMapName(method);
applyResultMap(resultMapId, returnType, argsIf(args), resultsIf(results), typeDiscriminator);
applyResultMap(resultMapId, returnType, argsIf(args, arg, method), resultsIf(results, result, method), typeDiscriminator);
kazuki43zoo marked this conversation as resolved.
Show resolved Hide resolved
return resultMapId;
}

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

private Result[] resultsIf(Results results) {
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) {
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();
}

Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/apache/ibatis/binding/BindingTest.java
Expand Up @@ -36,6 +36,7 @@
import net.sf.cglib.proxy.Factory;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.domain.blog.Author;
import org.apache.ibatis.domain.blog.Blog;
Expand Down Expand Up @@ -688,4 +689,76 @@ void registeredMappers() {
assertTrue(mapperClasses.contains(BoundAuthorMapper.class));
}

@Test
void shouldMapPropertiesUsingRepeatableAnnotation() {
try (SqlSession session = sqlSessionFactory.openSession()) {
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.selectAuthorMapToPropertiesUsingRepeatable(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());
session.rollback();
}
}

@Test
void shouldMapConstructorUsingRepeatableAnnotation() {
try (SqlSession session = sqlSessionFactory.openSession()) {
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.selectAuthorMapToConstructorUsingRepeatable(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 shouldMapUsingSingleRepeatableAnnotation() {
try (SqlSession session = sqlSessionFactory.openSession()) {
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.selectAuthorUsingSingleRepeatable(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();
}
}

@Test
void shouldErrorWhenSpecifyBothArgAndConstructorArgs() {
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());
}
}

@Test
void shouldErrorWhenSpecifyBothResultAndResults() {
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());
}
}

}

52 changes: 51 additions & 1 deletion src/test/java/org/apache/ibatis/binding/BoundAuthorMapper.java
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,6 +68,23 @@ public interface BoundAuthorMapper {

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

@Result(property = "id", column = "AUTHOR_ID", id = true)
@Result(property = "username", column = "AUTHOR_USERNAME")
@Result(property = "password", column = "AUTHOR_PASSWORD")
@Result(property = "email", column = "AUTHOR_EMAIL")
@Result(property = "bio", column = "AUTHOR_BIO")
@Select({
"SELECT ",
" ID as AUTHOR_ID,",
" USERNAME as AUTHOR_USERNAME,",
" PASSWORD as AUTHOR_PASSWORD,",
" EMAIL as AUTHOR_EMAIL,",
" BIO as AUTHOR_BIO",
"FROM AUTHOR WHERE ID = #{id}"})
Author selectAuthorMapToPropertiesUsingRepeatable(int id);

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

@ConstructorArgs({
@Arg(column = "AUTHOR_ID", javaType = Integer.class),
@Arg(column = "AUTHOR_USERNAME", javaType = String.class),
Expand All @@ -89,6 +106,39 @@ public interface BoundAuthorMapper {

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

@Arg(column = "AUTHOR_ID", javaType = Integer.class, id = true)
@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 selectAuthorMapToConstructorUsingRepeatable(int id);

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

@Arg(column = "AUTHOR_ID", javaType = int.class)
@Result(property = "username", column = "AUTHOR_USERNAME")
@Select({
"SELECT ",
" ID as AUTHOR_ID,",
" USERNAME as AUTHOR_USERNAME,",
" PASSWORD as AUTHOR_PASSWORD,",
" EMAIL as AUTHOR_EMAIL,",
" BIO as AUTHOR_BIO",
"FROM AUTHOR WHERE ID = #{id}"})
Author selectAuthorUsingSingleRepeatable(int id);

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

List<Post> findThreeSpecificPosts(@Param("one") int one,
RowBounds rowBounds,
@Param("two") int two,
Expand Down
@@ -0,0 +1,33 @@
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.binding;

import org.apache.ibatis.annotations.Arg;
import org.apache.ibatis.annotations.ConstructorArgs;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.domain.blog.Author;

public interface WrongArgAndConstructorArgsMapper {

@ConstructorArgs(@Arg(column = "AUTHOR_ID", javaType = int.class))
@Arg(column = "AUTHOR_ID", javaType = int.class)
@Select({
"SELECT ",
" ID as AUTHOR_ID",
"FROM AUTHOR WHERE ID = #{id}"})
Author selectAuthor(int id);

}
@@ -0,0 +1,33 @@
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.binding;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.domain.blog.Author;

public interface WrongResultAndResultsMapper {

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

}