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.Retention;
21  import java.lang.annotation.RetentionPolicy;
22  import java.lang.annotation.Target;
23  
24  import org.apache.ibatis.type.JdbcType;
25  import org.apache.ibatis.type.TypeHandler;
26  import org.apache.ibatis.type.UnknownTypeHandler;
27  
28  // @formatter:off
29  /**
30   * The annotation that be grouping conditional mapping definitions.
31   * <p>
32   * <b>How to use:</b>
33   *
34   * <pre>
35   * public interface UserMapper {
36   *   &#064;Select("SELECT id, name, type FROM users ORDER BY id")
37   *   &#064;TypeDiscriminator(
38   *     column = "type",
39   *     javaType = String.class,
40   *     cases = {
41   *       &#064;Case(value = "1", type = PremiumUser.class),
42   *       &#064;Case(value = "2", type = GeneralUser.class),
43   *       &#064;Case(value = "3", type = TemporaryUser.class)
44   *     }
45   *   )
46   *   List&lt;User&gt; selectAll();
47   * }
48   * </pre>
49   *
50   * @author Clinton Begin
51   */
52  // @formatter:on
53  @Documented
54  @Retention(RetentionPolicy.RUNTIME)
55  @Target(ElementType.METHOD)
56  public @interface TypeDiscriminator {
57  
58    /**
59     * Returns the column name(column label) that hold conditional value.
60     *
61     * @return the column name(column label)
62     */
63    String column();
64  
65    /**
66     * Return the java type for conditional value.
67     *
68     * @return the java type
69     */
70    Class<?> javaType() default void.class;
71  
72    /**
73     * Return the jdbc type for column that hold conditional value.
74     *
75     * @return the jdbc type
76     */
77    JdbcType jdbcType() default JdbcType.UNDEFINED;
78  
79    /**
80     * Returns the {@link TypeHandler} type for retrieving a column value from result set.
81     *
82     * @return the {@link TypeHandler} type
83     */
84    Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
85  
86    /**
87     * Returns conditional mapping definitions.
88     *
89     * @return conditional mapping definitions
90     */
91    Case[] cases();
92  }