1 /*
2 * Copyright 2009-2026 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 static org.apache.ibatis.annotations.AnnotationConstants.NULL_TYPE_DISCRIMINATOR;
19
20 import java.lang.annotation.Documented;
21 import java.lang.annotation.ElementType;
22 import java.lang.annotation.Repeatable;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 import java.lang.annotation.Target;
26
27 /**
28 * Annotation for defining result maps that are not directly tied to select statements.
29 * <p>
30 * A named result map can be used to define a result map that can be reused with multiple statements. The named result
31 * map must be attached to a type (typically a mapper interface).
32 * </p>
33 * <p>
34 * Multiple <code>NamedResultMap</code> annotations can be attached to a type.
35 * </p>
36 * <p>
37 * Users of the annotation can declare any combination of property mappings, constructor arguments, and/or a type
38 * discriminator. At least one of the three must be declared.
39 * </p>
40 * <p>
41 * <b>How to use:</b>
42 *
43 * <pre>
44 * @NamedResultMap(id = UserMapper.userResultId, javaType = User.class, propertyMappings = {
45 * @Result(property = "id", column = "id", id = true),
46 * @Result(property = "name", column = "name"),
47 * @Result(property = "email" column = "id", one = @One(select = "selectUserEmailById", fetchType = FetchType.LAZY)),
48 * @Result(property = "telephoneNumbers" column = "id", many = @Many(select = "selectAllUserTelephoneNumberById", fetchType = FetchType.LAZY))
49 * })
50 * public interface UserMapper {
51 * String userResultId = "userResult";
52 *
53 * @Select("SELECT id, name FROM users WHERE id = #{id}")
54 * @ResultMap(userResultId)
55 * User selectById(int id);
56 *
57 * @Select("SELECT id, name FROM users")
58 * @ResultMap(userResultId)
59 * List<User> selectAll();
60 * }
61 * </pre>
62 *
63 * @since 3.6.0
64 */
65 @Documented
66 @Retention(RetentionPolicy.RUNTIME)
67 @Target(ElementType.TYPE)
68 @Repeatable(NamedResultMaps.class)
69 public @interface NamedResultMap {
70 /**
71 * Returns the result map id.
72 *
73 * @return the result map id
74 */
75 String id();
76
77 /**
78 * Returns the Java type created with this result map.
79 *
80 * @return the Java type that should be created with this result map
81 */
82 Class<?> javaType();
83
84 /**
85 * Returns mapping definitions for constructor arguments.
86 *
87 * @return mapping definitions
88 */
89 Arg[] constructorArguments() default {};
90
91 /**
92 * Returns mapping definitions for properties.
93 *
94 * @return mapping definitions
95 */
96 Result[] propertyMappings() default {};
97
98 /**
99 * Returns the type discriminator for this result map. The default is effectively null - meaning no type discriminator
100 * is used.
101 *
102 * @return the type discriminator for this result map
103 */
104 TypeDiscriminator typeDiscriminator() default @TypeDiscriminator(column = NULL_TYPE_DISCRIMINATOR, cases = {});
105 }