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  import org.apache.ibatis.mapping.StatementType;
26  
27  /**
28   * The annotation that specify an SQL for retrieving a key value.
29   * <p>
30   * <b>How to use:</b>
31   *
32   * <pre>
33   * public interface UserMapper {
34   *   &#064;SelectKey(statement = "SELECT identity('users')", keyProperty = "id", before = true, resultType = int.class)
35   *   &#064;Insert("INSERT INTO users (id, name) VALUES(#{id}, #{name})")
36   *   boolean insert(User user);
37   * }
38   * </pre>
39   *
40   * @author Clinton Begin
41   */
42  @Documented
43  @Retention(RetentionPolicy.RUNTIME)
44  @Target(ElementType.METHOD)
45  @Repeatable(SelectKey.List.class)
46  public @interface SelectKey {
47    /**
48     * Returns an SQL for retrieving a key value.
49     *
50     * @return an SQL for retrieving a key value
51     */
52    String[] statement();
53  
54    /**
55     * Returns property names that holds a key value.
56     * <p>
57     * If you specify multiple property, please separate using comma(',').
58     * </p>
59     *
60     * @return property names that separate with comma(',')
61     */
62    String keyProperty();
63  
64    /**
65     * Returns column names that retrieves a key value.
66     * <p>
67     * If you specify multiple column, please separate using comma(',').
68     * </p>
69     *
70     * @return column names that separate with comma(',')
71     */
72    String keyColumn() default "";
73  
74    /**
75     * Returns whether retrieves a key value before executing insert/update statement.
76     *
77     * @return {@code true} if execute before; {@code false} if otherwise
78     */
79    boolean before();
80  
81    /**
82     * Returns the key value type.
83     *
84     * @return the key value type
85     */
86    Class<?> resultType();
87  
88    /**
89     * Returns the statement type to use.
90     *
91     * @return the statement type
92     */
93    StatementType statementType() default StatementType.PREPARED;
94  
95    /**
96     * @return A database id that correspond this select key
97     *
98     * @since 3.5.5
99     */
100   String databaseId() default "";
101 
102   /**
103    * The container annotation for {@link SelectKey}.
104    *
105    * @author Kazuki Shimizu
106    *
107    * @since 3.5.5
108    */
109   @Documented
110   @Retention(RetentionPolicy.RUNTIME)
111   @Target(ElementType.METHOD)
112   @interface List {
113     SelectKey[] value();
114   }
115 
116 }