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 /** 26 * The annotation that specify a method that provide an SQL for inserting record(s). 27 * 28 * <p> 29 * <b>How to use:</b> 30 * 31 * <pre> 32 * public interface UserMapper { 33 * 34 * @InsertProvider(type = SqlProvider.class, method = "insert") 35 * void insert(User user); 36 * 37 * public static class SqlProvider { 38 * public static String insert() { 39 * return "INSERT INTO users (id, name) VALUES(#{id}, #{name})"; 40 * } 41 * } 42 * 43 * } 44 * </pre> 45 * 46 * @author Clinton Begin 47 */ 48 @Documented 49 @Retention(RetentionPolicy.RUNTIME) 50 @Target(ElementType.METHOD) 51 @Repeatable(InsertProvider.List.class) 52 public @interface InsertProvider { 53 54 /** 55 * Specify a type that implements an SQL provider method. 56 * 57 * @return a type that implements an SQL provider method 58 * @since 3.5.2 59 * @see #type() 60 */ 61 Class<?> value() default void.class; 62 63 /** 64 * Specify a type that implements an SQL provider method. 65 * <p> 66 * This attribute is alias of {@link #value()}. 67 * </p> 68 * 69 * @return a type that implements an SQL provider method 70 * @see #value() 71 */ 72 Class<?> type() default void.class; 73 74 /** 75 * Specify a method for providing an SQL. 76 * 77 * <p> 78 * Since 3.5.1, this attribute can omit. 79 * If this attribute omit, the MyBatis will call a method that decide by following rules. 80 * <ul> 81 * <li> 82 * If class that specified the {@link #type()} attribute implements the 83 * {@link org.apache.ibatis.builder.annotation.ProviderMethodResolver}, 84 * the MyBatis use a method that returned by it 85 * </li> 86 * <li> 87 * If cannot resolve a method by {@link org.apache.ibatis.builder.annotation.ProviderMethodResolver}(= not implement it or it was returned {@code null}), 88 * the MyBatis will search and use a fallback method that named {@code provideSql} from specified type 89 * </li> 90 * </ul> 91 * 92 * @return a method name of method for providing an SQL 93 */ 94 String method() default ""; 95 96 /** 97 * @return A database id that correspond this provider 98 * @since 3.5.5 99 */ 100 String databaseId() default ""; 101 102 /** 103 * The container annotation for {@link InsertProvider}. 104 * @author Kazuki Shimizu 105 * @since 3.5.5 106 */ 107 @Documented 108 @Retention(RetentionPolicy.RUNTIME) 109 @Target(ElementType.METHOD) 110 @interface List { 111 InsertProvider[] value(); 112 } 113 114 }