1 /* 2 * Copyright 2009-2024 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 * @SelectKey(statement = "SELECT identity('users')", keyProperty = "id", before = true, resultType = int.class) 35 * @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 * 59 * @return property names that separate with comma(',') 60 */ 61 String keyProperty(); 62 63 /** 64 * Returns column names that retrieves a key value. 65 * <p> 66 * If you specify multiple column, please separate using comma(','). 67 * 68 * @return column names that separate with comma(',') 69 */ 70 String keyColumn() default ""; 71 72 /** 73 * Returns whether retrieves a key value before executing insert/update statement. 74 * 75 * @return {@code true} if execute before; {@code false} if otherwise 76 */ 77 boolean before(); 78 79 /** 80 * Returns the key value type. 81 * 82 * @return the key value type 83 */ 84 Class<?> resultType(); 85 86 /** 87 * Returns the statement type to use. 88 * 89 * @return the statement type 90 */ 91 StatementType statementType() default StatementType.PREPARED; 92 93 /** 94 * @return A database id that correspond this select key 95 * 96 * @since 3.5.5 97 */ 98 String databaseId() default ""; 99 100 /** 101 * The container annotation for {@link SelectKey}. 102 * 103 * @author Kazuki Shimizu 104 * 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 }