View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.annotations;
17  
18  import java.lang.annotation.Documented;
19  import java.lang.annotation.ElementType;
20  import java.lang.annotation.Repeatable;
21  import java.lang.annotation.Retention;
22  import java.lang.annotation.RetentionPolicy;
23  import java.lang.annotation.Target;
24  
25  /**
26   * The annotation that specify a method that provide an SQL for retrieving record(s).
27   * <p>
28   * <b>How to use:</b>
29   *
30   * <pre>
31   * public interface UserMapper {
32   *
33   *   &#064;SelectProvider(type = SqlProvider.class, method = "selectById")
34   *   User selectById(int id);
35   *
36   *   public static class SqlProvider {
37   *     public static String selectById() {
38   *       return "SELECT id, name FROM users WHERE id = #{id}";
39   *     }
40   *   }
41   *
42   * }
43   * </pre>
44   *
45   * @author Clinton Begin
46   */
47  @Documented
48  @Retention(RetentionPolicy.RUNTIME)
49  @Target(ElementType.METHOD)
50  @Repeatable(SelectProvider.List.class)
51  public @interface SelectProvider {
52  
53    /**
54     * Specify a type that implements an SQL provider method.
55     *
56     * @return a type that implements an SQL provider method
57     *
58     * @since 3.5.2
59     *
60     * @see #type()
61     */
62    Class<?> value() default void.class;
63  
64    /**
65     * Specify a type that implements an SQL provider method.
66     * <p>
67     * This attribute is alias of {@link #value()}.
68     *
69     * @return a type that implements an SQL provider method
70     *
71     * @see #value()
72     */
73    Class<?> type() default void.class;
74  
75    // @formatter:off
76    /**
77     * Specify a method for providing an SQL.
78     * <p>
79     * Since 3.5.1, this attribute can omit.
80     * <p>
81     * If this attribute omit, the MyBatis will call a method that decide by following rules.
82     *
83     * <ul>
84     *   <li>
85     *     If class that specified the {@link #type()} attribute implements the
86     *     {@link org.apache.ibatis.builder.annotation.ProviderMethodResolver},
87     *     the MyBatis use a method that returned by it.
88     *   </li>
89     *   <li>
90     *     If cannot resolve a method by {@link org.apache.ibatis.builder.annotation.ProviderMethodResolver}
91     *     (= not implement it or it was returned <code>null</code>,
92     *     the MyBatis will search and use a fallback method that named <code>provideSql</code> from
93     *     specified type.
94     *   </li>
95     * </ul>
96     *
97     * @return a method name of method for providing an SQL
98     */
99    // @formatter:on
100   String method() default "";
101 
102   /**
103    * @return A database id that correspond this provider
104    *
105    * @since 3.5.5
106    */
107   String databaseId() default "";
108 
109   /**
110    * Returns whether this select affects DB data.<br>
111    * e.g. RETURNING of PostgreSQL or OUTPUT of MS SQL Server.
112    *
113    * @return {@code true} if this select affects DB data; {@code false} if otherwise
114    *
115    * @since 3.5.12
116    */
117   boolean affectData() default false;
118 
119   /**
120    * The container annotation for {@link SelectProvider}.
121    *
122    * @author Kazuki Shimizu
123    *
124    * @since 3.5.5
125    */
126   @Documented
127   @Retention(RetentionPolicy.RUNTIME)
128   @Target(ElementType.METHOD)
129   @interface List {
130     SelectProvider[] value();
131   }
132 
133 }