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.submitted.annotion_many_one_add_resultmapid;
17  
18  import java.util.List;
19  
20  import org.apache.ibatis.annotations.Many;
21  import org.apache.ibatis.annotations.One;
22  import org.apache.ibatis.annotations.Result;
23  import org.apache.ibatis.annotations.Results;
24  import org.apache.ibatis.annotations.Select;
25  
26  /**
27   * @author lvyang
28   */
29  public interface UserDao {
30    // @formatter:off
31    @Select({"select",
32        "     u.id, u.username, r.id role_id, r.role_name",
33        "    from user u",
34        "    left join user_role ur on u.id = ur.user_id",
35        "    left join role r on ur.role_id = r.id"})
36    @Results({
37        @Result(id = true, column = "id", property = "id"),
38        @Result(column = "username", property = "username"),
39        @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap1"))
40      })
41    // @formatter:on
42    List<User> findAll();
43  
44    // @formatter:off
45    @Select({"select",
46        "     u.id, u.username, r.id role_id, r.role_name",
47        "    from user u",
48        "    left join user_role ur on u.id = ur.user_id",
49        "    left join role r on ur.role_id = r.id"})
50    @Results({
51        @Result(id = true, column = "id", property = "id"),
52        @Result(column = "username", property = "username"),
53        @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2"))
54      })
55    // @formatter:on
56    List<User> findAll2();
57  
58    // @formatter:off
59    @Select({"select",
60        "     u.id, u.username, r.id role_id, r.role_name",
61        "    from user u",
62        "    left join user_role ur on u.id = ur.user_id",
63        "    left join role r on ur.role_id = r.id where u.id in (2, 3)"})
64    @Results({
65        @Result(id = true, column = "id", property = "id"),
66        @Result(column = "username", property = "username"),
67        @Result(property = "role", one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2"))
68      })
69    // @formatter:on
70    List<User> findAll3();
71  
72    // @formatter:off
73    @Select("select id teacher_id, username teacher_name from user")
74    @Results(id = "userMap", value = {
75        @Result(id = true, column = "teacher_id", property = "id"),
76        @Result(column = "teacher_name", property = "username")
77      })
78    // @formatter:on
79    List<User> justUseResult();
80  
81    // @formatter:off
82    @Select({"select",
83        "u.id, u.username, r.id role_id, r.role_name, ut.id teacher_id, ut.username teacher_name",
84        "from user u",
85        "left join user_role ur on u.id = ur.user_id",
86        "left join role r on ur.role_id = r.id",
87        "left join user ut on ut.id != u.id",
88        "where role_id = 3"})
89    @Results({
90        @Result(id = true, column = "id", property = "id"),
91        @Result(column = "username", property = "username"),
92        @Result(property = "role", one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2")),
93        @Result(property = "teachers", many = @Many(resultMap = "userMap"))
94      })
95    // @formatter:on
96    User findHeadmaster();
97  }