View Javadoc
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.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    @Select({ "select",
31      "     u.id, u.username, r.id role_id, r.name role_name",
32      "    from user u",
33      "    left join user_role ur on u.id = ur.user_id",
34      "    left join role r on ur.role_id = r.id" })
35    @Results({
36      @Result(id = true, column = "id", property = "id"),
37      @Result(column = "username", property = "username"),
38      @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.RoleDao.roleMap1", columnPrefix = "role_"))
39    })
40    List<User> findAll();
41  
42    @Select({ "select",
43      "     u.id, u.username, r.id role_id, r.name role_name",
44      "    from user u",
45      "    left join user_role ur on u.id = ur.user_id",
46      "    left join role r on ur.role_id = r.id" })
47    @Results({
48      @Result(id = true, column = "id", property = "id"),
49      @Result(column = "username", property = "username"),
50      @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.RoleDao.roleMap2", columnPrefix = "role_"))
51    })
52    List<User> findAll2();
53  
54    @Select({ "select",
55      "     u.id, u.username, r.id role_id, r.name role_name",
56      "    from user u",
57      "    left join user_role ur on u.id = ur.user_id",
58      "    left join role r on ur.role_id = r.id where u.id in (2, 3)" })
59    @Results({
60      @Result(id = true, column = "id", property = "id"),
61      @Result(column = "username", property = "username"),
62      @Result(property = "role", one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.RoleDao.roleMap2", columnPrefix = "role_"))
63    })
64    List<User> findAll3();
65  
66    @Select("select id teacher_id, username teacher_name from user")
67    @Results(id = "userMap", value = {
68      @Result(id = true, column = "teacher_id", property = "id"),
69      @Result(column = "teacher_name", property = "username")
70    })
71    List<User> justUseResult();
72  
73    @Select({ "select",
74      "     u.id, u.username, r.id role_id, r.name role_name,",
75      "     f.id friend_id, f.username, fr.id friend_role_id, fr.name friend_role_name",
76      "    from user u",
77      "    left join user_role ur on u.id = ur.user_id",
78      "    left join role r on ur.role_id = r.id" ,
79      "    left join user f on u.friend_id = f.id",
80      "    left join user_role fur on f.id = fur.user_id",
81      "    left join role fr on fur.role_id = fr.id" ,
82      "    where u.id = #{userId} order by r.id, fr.id"
83      })
84    @Results(id = "userWithFriendMap", value = {
85      @Result(id = true, column = "id", property = "id"),
86      @Result(column = "username", property = "username"),
87      @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.RoleDao.roleMap1", columnPrefix = "role_")),
88      @Result(property = "friend", one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.UserDao.userWithFriendMap", columnPrefix = "friend_"))
89    })
90    User findUserWithFriend(Integer userId);
91  }