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