六、多对多查询
程序员文章站
2022-04-24 10:31:10
...
实体类:
public class Role implements Serializable {
private Integer roleId;
private String roleName;
private String roleDesc;
//多对多的关系映射:一个角色可以赋予多个用户
private List<User> users;
@Override
public String toString() {
return "Role{" +
"roleId=" + roleId +
", roleName='" + roleName + '\'' +
", roleDesc='" + roleDesc + '\'' +
'}';
}
}
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private String sex;
private Date birthday;
//多对多的关系映射:一个用户可以具备多个角色
private List<Role> roles;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
'}';
}
}
测查询所有角色,同时获取每个角色的用户信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liaoxiang.dao.IRoleDao">
<!--定义role表的ResultMap, 查询所有角色,并显示角色对应的有哪些人-->
<resultMap id="roleMap" type="role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
<collection property="users" ofType="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="address" property="address"></result>
<result column="sex" property="sex"></result>
<result column="birthday" property="birthday"></result>
</collection>
</resultMap>
<!--查询所有-->
<select id="findAll" resultMap="roleMap">
select u.*,r.id as rid,r.role_name,r.role_desc from role r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
</select>
</mapper>
@Test
public void testFindAll(){
List<Role> roles = roleDao.findAll();
for(Role role : roles){
System.out.println("---------------每个角色的信息-------------");
System.out.println(role);
System.out.println(role.getUsers());
}
}
角色信息:Role{roleId=1, roleName='院长', roleDesc='管理整个学院'}
用户信息:[User{id=41, username='张三', address='绵阳', sex='男', birthday=Tue Feb 27 17:47:08 CST 2018},
User{id=45, username='李四', address='广州', sex='男', birthday=Sun Mar 04 12:04:06 CST 2018}]
角色信息:Role{roleId=2, roleName='总裁', roleDesc='管理整个公司'}
用户信息:[User{id=41, username='张三', address='绵阳', sex='男', birthday=Tue Feb 27 17:47:08 CST 2018}]
角色信息:Role{roleId=3, roleName='校长', roleDesc='管理整个学校'}
用户信息:[]
查询用户信息,同时查询出每个用户的角色信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liaoxiang.dao.IUserDao">
<!-- 定义User的resultMap,查询用户,并且查询出用户的角色信息-->
<resultMap id="userMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!-- 配置角色集合的映射 -->
<collection property="roles" ofType="role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
</collection>
</resultMap>
<!-- 查询所有 -->
<select id="findAll" resultMap="userMap">
select u.*,r.id as rid,r.role_name,r.role_desc from user u
left outer join user_role ur on u.id = ur.uid
left outer join role r on r.id = ur.rid
</select>
</mapper>
@Test
public void testFindAll(){
List<User> users = userDao.findAll();
for(User user : users){
System.out.println("用户信息:"+user);
System.out.println("用户角色:"+user.getRoles());
System.out.println("===================每个用户信息================");
}
}
用户信息:User{id=41, username='张三', address='绵阳', sex='男', birthday=Tue Feb 27 17:47:08 CST 2018}
用户角色:[Role{roleId=1, roleName='院长', roleDesc='管理整个学院'},
Role{roleId=2, roleName='总裁', roleDesc='管理整个公司'}]
用户信息:User{id=45, username='李四', address='广州', sex='男', birthday=Sun Mar 04 12:04:06 CST 2018}
用户角色:[Role{roleId=1, roleName='院长', roleDesc='管理整个学院'}]
用户信息:User{id=46, username='老王', address='北京', sex='男', birthday=Wed Mar 07 17:37:26 CST 2018}
用户角色:[]
用户信息:User{id=49, username='王五', address='*', sex='女', birthday=Mon Jun 03 16:40:00 CST 2019}
用户角色:[]