SSM15.3【Mybatis:Mybatis的多表操作之多对多】

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

复制代码
 1 package com.haifei.domain;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 /**
 7  * user表
 8  *  sys_user_role表
 9  */
10 public class User {
11 
12     private int id;
13     private String username;
14     private String password;
15     private Date birthday;
16 
17    
18     //描述的是当前用户具备哪些角色
19     private List<Role> roleList;
20     public List<Role> getRoleList() {
21         return roleList;
22     }
23     public void setRoleList(List<Role> roleList) {
24         this.roleList = roleList;
25     }
26 
27     public Date getBirthday() {
28         return birthday;
29     }
30 
31     public void setBirthday(Date birthday) {
32         this.birthday = birthday;
33     }
34 
35     public int getId() {
36         return id;
37     }
38 
39     public void setId(int id) {
40         this.id = id;
41     }
42 
43     public String getUsername() {
44         return username;
45     }
46 
47     public void setUsername(String username) {
48         this.username = username;
49     }
50 
51     public String getPassword() {
52         return password;
53     }
54 
55     public void setPassword(String password) {
56         this.password = password;
57     }
58 
59     //m:n
60     @Override
61     public String toString() {
62         return "User{" +
63                 "id=" + id +
64                 ", username='" + username + '\'' +
65                 ", password='" + password + '\'' +
66                 ", birthday=" + birthday +
67                 ", roleList=" + roleList +
68                 '}';
69     }
70 }
复制代码
复制代码
 1 package com.haifei.domain;
 2 
 3 /**
 4  * sys_role表
 5  *  sys_user_role表
 6  */
 7 public class Role {
 8 
 9     private int id;
10     private String roleName;
11     private String roleDesc;
12 
13     public int getId() {
14         return id;
15     }
16 
17     public void setId(int id) {
18         this.id = id;
19     }
20 
21     public String getRoleName() {
22         return roleName;
23     }
24 
25     public void setRoleName(String roleName) {
26         this.roleName = roleName;
27     }
28 
29     public String getRoleDesc() {
30         return roleDesc;
31     }
32 
33     public void setRoleDesc(String roleDesc) {
34         this.roleDesc = roleDesc;
35     }
36 
37     @Override
38     public String toString() {
39         return "Role{" +
40                 "id=" + id +
41                 ", roleName='" + roleName + '\'' +
42                 ", roleDesc='" + roleDesc + '\'' +
43                 '}';
44     }
45 }
复制代码
复制代码
 1 package com.haifei.mapper;
 2 
 3 import com.haifei.domain.User;
 4 
 5 import java.util.List;
 6 
 7 public interface UserMapper {
 8 
 9     public List<User> findUserAndRoleAll();
10 
11 }
复制代码
复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 
 4 <mapper namespace="com.haifei.mapper.UserMapper">
 5 
 6     <resultMap id="userRoleMap" type="user">
 7         <!--user的信息-->
 8         <id column="userId" property="id"></id>
 9         <result column="username" property="username"></result>
10         <result column="password" property="password"></result>
11         <result column="birthday" property="birthday"></result>
12         <!--user内部的roleList信息-->
13         <collection property="roleList" ofType="role">
14             <id column="roleId" property="id"></id>
15             <result column="roleName" property="roleName"></result>
16             <result column="roleDesc" property="roleDesc"></result>
17         </collection>
18     </resultMap>
19 
20     <select id="findUserAndRoleAll" resultMap="userRoleMap">
21        SELECT * FROM user u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id
22     </select>
23 </mapper>
复制代码

 

复制代码
 1 /**
 2      * 多对多查询
 3      *  场景:每个用户都可以拥有多种角色
 4      * @throws IOException
 5      */
 6     @Test
 7     public void test3() throws IOException {
 8         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
 9         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
10         SqlSession sqlSession = sqlSessionFactory.openSession();
11 
12         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
13         List<User> userList = mapper.findUserAndRoleAll();
14         for (User user : userList) {
15             System.out.println(user);
16         }
17         /*
18         User{id=1, username='zhangsan', password='123', birthday=Sat Jul 24 20:00:29 CST 2021, roleList=[Role{id=1, roleName='院长', roleDesc='负责全面工作'}, Role{id=2, roleName='研究员', roleDesc='课程研发工作'}]}
19         User{id=2, username='lisi', password='345', birthday=Sat Jul 24 20:00:29 CST 2021, roleList=[Role{id=2, roleName='研究员', roleDesc='课程研发工作'}, Role{id=3, roleName='讲师', roleDesc='授课工作'}]}
20          */
21 
22         sqlSession.close();
23     }
复制代码

 

posted @   yub4by  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示