Skip to content

Commit 3b816f8

Browse files
authored
Merge pull request #1522 from kazuki43zoo/support-alias-on-providerannotation
Add value attribute on provider annotations
2 parents 1b2918c + d7cf1ea commit 3b816f8

File tree

12 files changed

+126
-16
lines changed

12 files changed

+126
-16
lines changed

src/main/java/org/apache/ibatis/annotations/DeleteProvider.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@
3333
* Specify a type that implements an SQL provider method.
3434
*
3535
* @return a type that implements an SQL provider method
36+
* @since 3.5.2
37+
* @see #type()
3638
*/
37-
Class<?> type();
39+
Class<?> value() default void.class;
40+
41+
/**
42+
* Specify a type that implements an SQL provider method.
43+
* <p>
44+
* This attribute is alias of {@link #value()}.
45+
* </p>
46+
* @return a type that implements an SQL provider method
47+
* @see #value()
48+
*/
49+
Class<?> type() default void.class;
3850

3951
/**
4052
* Specify a method for providing an SQL.

src/main/java/org/apache/ibatis/annotations/InsertProvider.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@
3333
* Specify a type that implements an SQL provider method.
3434
*
3535
* @return a type that implements an SQL provider method
36+
* @since 3.5.2
37+
* @see #type()
3638
*/
37-
Class<?> type();
39+
Class<?> value() default void.class;
40+
41+
/**
42+
* Specify a type that implements an SQL provider method.
43+
* <p>
44+
* This attribute is alias of {@link #value()}.
45+
* </p>
46+
*
47+
* @return a type that implements an SQL provider method
48+
* @see #value()
49+
*/
50+
Class<?> type() default void.class;
3851

3952
/**
4053
* Specify a method for providing an SQL.

src/main/java/org/apache/ibatis/annotations/SelectProvider.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@
3333
* Specify a type that implements an SQL provider method.
3434
*
3535
* @return a type that implements an SQL provider method
36+
* @since 3.5.2
37+
* @see #type()
3638
*/
37-
Class<?> type();
39+
Class<?> value() default void.class;
40+
41+
/**
42+
* Specify a type that implements an SQL provider method.
43+
* <p>
44+
* This attribute is alias of {@link #value()}.
45+
* </p>
46+
*
47+
* @return a type that implements an SQL provider method
48+
* @see #value()
49+
*/
50+
Class<?> type() default void.class;
3851

3952
/**
4053
* Specify a method for providing an SQL.

src/main/java/org/apache/ibatis/annotations/UpdateProvider.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@
3333
* Specify a type that implements an SQL provider method.
3434
*
3535
* @return a type that implements an SQL provider method
36+
* @since 3.5.2
37+
* @see #type()
3638
*/
37-
Class<?> type();
39+
Class<?> value() default void.class;
40+
41+
/**
42+
* Specify a type that implements an SQL provider method.
43+
* <p>
44+
* This attribute is alias of {@link #value()}.
45+
* </p>
46+
*
47+
* @return a type that implements an SQL provider method
48+
* @see #value()
49+
*/
50+
Class<?> type() default void.class;
3851

3952
/**
4053
* Specify a method for providing an SQL.

src/main/java/org/apache/ibatis/builder/annotation/ProviderSqlSource.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.ibatis.builder.annotation;
1717

18+
import java.lang.annotation.Annotation;
19+
import java.lang.reflect.InvocationTargetException;
1820
import java.lang.reflect.Method;
1921
import java.lang.reflect.Modifier;
2022
import java.util.Map;
@@ -59,7 +61,7 @@ public ProviderSqlSource(Configuration configuration, Object provider, Class<?>
5961
this.configuration = configuration;
6062
Lang lang = mapperMethod == null ? null : mapperMethod.getAnnotation(Lang.class);
6163
this.languageDriver = configuration.getLanguageDriver(lang == null ? null : lang.value());
62-
this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);
64+
this.providerType = getProviderType(provider, mapperMethod);
6365
providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);
6466

6567
if (providerMethodName.length() == 0 && ProviderMethodResolver.class.isAssignableFrom(this.providerType)) {
@@ -175,4 +177,21 @@ private String invokeProviderMethod(Object... args) throws Exception {
175177
return sql != null ? sql.toString() : null;
176178
}
177179

180+
private Class<?> getProviderType(Object providerAnnotation, Method mapperMethod)
181+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
182+
Class<?> type = (Class<?>) providerAnnotation.getClass().getMethod("type").invoke(providerAnnotation);
183+
Class<?> value = (Class<?>) providerAnnotation.getClass().getMethod("value").invoke(providerAnnotation);
184+
if (value == void.class && type == void.class) {
185+
throw new BuilderException("Please specify either 'value' or 'type' attribute of @"
186+
+ ((Annotation) providerAnnotation).annotationType().getSimpleName()
187+
+ " at the '" + mapperMethod.toString() + "'.");
188+
}
189+
if (value != void.class && type != void.class && value != type) {
190+
throw new BuilderException("Cannot specify different class on 'value' and 'type' attribute of @"
191+
+ ((Annotation) providerAnnotation).annotationType().getSimpleName()
192+
+ " at the '" + mapperMethod.toString() + "'.");
193+
}
194+
return value == void.class ? type : value;
195+
}
196+
178197
}

src/site/es/xdoc/java-api.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ void rollback(boolean force)</source>
454454
<td>Estas anotaciones SQL alternativas te permiten especificar un nombre de clases y un método que devolverán la SQL que debe ejecutarse (Since 3.4.6, you can specify the <code>CharSequence</code> instead of <code>String</code> as a method return type).
455455
Cuando se ejecute el método MyBatis instanciará la clase y ejecutará el método especificados en el provider. You can pass objects that passed to arguments of a mapper method, "Mapper interface type", "Mapper method" and "Database ID"
456456
via the <code>ProviderContext</code>(available since MyBatis 3.4.5 or later) as method argument.(In MyBatis 3.4 or later, it's allow multiple parameters)
457-
Atributos: type, method. El atributo type es el nombre completamente cualificado de una clase.
457+
Atributos: value, type y method.
458+
El atributo value y type es el nombre completamente cualificado de una clase
459+
(The <code>type</code> attribute is alias for <code>value</code>, you must be specify either one).
458460
El method es el nombre un método de dicha clase
459461
(Since 3.5.1, you can omit <code>method</code> attribute, the MyBatis will resolve a target method via the
460462
<code>ProviderMethodResolver</code> interface.

src/site/ja/xdoc/java-api.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ void rollback(boolean force)</source>
466466
<td>これらのアノテーションは動的 SQL を生成するためのものです。実行時に指定されたメソッドが呼び出され、メソッドから返された SQL ステートメントが実行されます (MyBatis 3.4.6以降では、メソッドの返り値として <code>String</code> ではなく <code>CharSequence</code> を指定することができます)。
467467
マップドステートメントを実行する際、プロバイダーによって指定したクラスのインスタンスが作成され、指定されたメソッドが実行されます。
468468
なお、メソッド引数にはMapperメソッドの引数に渡したオブジェクトに加え、<code>ProviderContext</code>(MyBatis 3.4.5以降で利用可能)を介して「Mapperインタフェースの型」「Mapperメソッド」「データベースID」を渡すことができます。(MyBatis 3.4以降では、複数の引数を渡すことができます)
469-
キー: <code>type</code>, <code>method</code>. <code>type</code> にはクラスオブジェクト、<code>method</code> にはメソッド名を指定します
469+
キー: <code>value</code>, <code>type</code>, <code>method</code>.
470+
<code>value</code> と <code>type</code> にはクラスオブジェクトを指定します
471+
(<code>type</code> は <code>value</code> の別名で、どちらか一方を指定する必要があります)。
472+
<code>method</code> にはメソッド名を指定します
470473
(MyBatis 3.5.1以降では、<code>method</code> 属性を省略することができます。その際MyBatisは、<code>ProviderMethodResolver</code> インタフェースを介して対象メソッドの解決を試み、
471474
対象メソッドが解決できない場合は、<code>provideSql</code>という名前のメソッドを代替メソッドとして利用します)。
472475
<span class="label important">NOTE</span> 次の章で、クリーンで可読性の高いコードで動的 SQL を構築するためのクラスについて説明します。

src/site/ko/xdoc/java-api.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,8 @@ void rollback(boolean force)</source>
588588
<td>실행시 SQL 을 리턴할 클래스 과 메소드명을 명시하도록 해주는 대체수단의 애노테이션이다 (Since 3.4.6, you can specify the <code>CharSequence</code> instead of <code>String</code> as a method return type).
589589
매핑된 구문을 실행할 때 마이바티스는 클래스의 인스턴스를 만들고 메소드를 실행한다.
590590
Mapper 메서드의 인수인 "Mapper interface type" and "Database ID" 과 <code>ProviderContext</code>(Mybatis 3.4.5 부터) 를 이용한 "Mapper method" 로 전달 된 객체를 메서드 매개변수로 전달할 수 있다.(마이바티스 3.4이상에서는 복수 파라미터를 허용한다.)
591-
사용가능한 속성들 : type, method.
592-
type 속성은 클래스.
591+
사용가능한 속성들 : value, type, method.
592+
value and type 속성은 클래스 (The <code>type</code> attribute is alias for <code>value</code>, you must be specify either one).
593593
method 속성은 메소드명이다
594594
(Since 3.5.1, you can omit <code>method</code> attribute, the MyBatis will resolve a target method via the
595595
<code>ProviderMethodResolver</code> interface.

src/site/xdoc/java-api.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,9 @@ void rollback(boolean force)</source>
502502
You can pass objects that passed to arguments of a mapper method, "Mapper interface type", "Mapper method" and "Database ID"
503503
via the <code>ProviderContext</code>(available since MyBatis 3.4.5 or later) as method argument.
504504
(In MyBatis 3.4 or later, it's allow multiple parameters)
505-
Attributes: <code>type</code>, <code>method</code>. The <code>type</code> attribute is a class.
505+
Attributes: <code>value</code>, <code>type</code> and <code>method</code>.
506+
The <code>value</code> and <code>type</code> attribute is a class
507+
(The <code>type</code> attribute is alias for <code>value</code>, you must be specify either one).
506508
The <code>method</code> is the name of the method on that class
507509
(Since 3.5.1, you can omit <code>method</code> attribute, the MyBatis will resolve a target method via the
508510
<code>ProviderMethodResolver</code> interface.

src/site/zh/xdoc/java-api.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ void rollback(boolean force)</source>
455455
</ul>
456456
</td>
457457
       <td>允许构建动态 SQL。这些备选的 SQL 注解允许你指定类名和返回在运行时执行的 SQL 语句的方法。(自从MyBatis 3.4.6开始,你可以用 <code>CharSequence</code> 代替 <code>String</code> 来返回类型返回值了。)当执行映射语句的时候,MyBatis 会实例化类并执行方法,类和方法就是填入了注解的值。你可以把已经传递给映射方法了的对象作为参数,"Mapper interface type" 和 "Mapper method" and "Database ID" 会经过 <code>ProviderContext</code> (仅在MyBatis 3.4.5及以上支持)作为参数值。(MyBatis 3.4及以上的版本,支持多参数传入)
458-
属性有: <code>type</code>, <code>method</code>。
459-
<code>type</code> 属性需填入类。
458+
属性有: <code>value</code>, <code>type</code>, <code>method</code>。
459+
<code>value</code> and <code>type</code> 属性需填入类(The <code>type</code> attribute is alias for <code>value</code>, you must be specify either one)
460460
<code>method</code> 需填入该类定义了的方法名
461461
(Since 3.5.1, you can omit <code>method</code> attribute, the MyBatis will resolve a target method via the
462462
<code>ProviderMethodResolver</code> interface.

0 commit comments

Comments
 (0)