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_columnprefix;
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.name 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_columnprefix.RoleDao.roleMap1", columnPrefix = "role_"))
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.name 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_columnprefix.RoleDao.roleMap2", columnPrefix = "role_"))
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.name 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_columnprefix.RoleDao.roleMap2", columnPrefix = "role_"))
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.name role_name,",
84        "  f.id friend_id, f.username, fr.id friend_role_id, fr.name friend_role_name",
85        " from user u",
86        " left join user_role ur on u.id = ur.user_id",
87        " left join role r on ur.role_id = r.id",
88        " left join user f on u.friend_id = f.id",
89        " left join user_role fur on f.id = fur.user_id",
90        " left join role fr on fur.role_id = fr.id",
91        " where u.id = #{userId} order by r.id, fr.id"
92      })
93    @Results(id = "userWithFriendMap", value = {
94        @Result(id = true, column = "id", property = "id"),
95        @Result(column = "username", property = "username"),
96        @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.RoleDao.roleMap1", columnPrefix = "role_")),
97        @Result(property = "friend", one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.UserDao.userWithFriendMap", columnPrefix = "friend_"))
98      })
99    // @formatter:on
100   User findUserWithFriend(Integer userId);
101 
102 }