三. 员工到角色的一对多的嵌套select 查询
UserMapper.java 接口:
public User getByIdWithSelect(int id);
RoleMapper.java 接口:
public List<Role> findRoleByUserId(@Param(value="userId") int userId);
UserMapper.xml sql语句:
<resultMap type="user" id="userResultMapWithSelect"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="description" column="description"/> <collection property="roles" ofType="role" column="id" select="com.yjl.mapper.RoleMapper.findRoleByUserId"></collection> </resultMap> <!-- 嵌套select --> <select id="getByIdWithSelect" parameterType="int" resultMap="userResultMapWithSelect"> select * from user where id=#{id} </select>
RoleMapper.xml 中sql语句:
<resultMap type="role" id="roleResultMap"> <id property="id" column="id"/> <result property="name" column="name"/> </resultMap> <select id="findRoleByUserId" parameterType="int" resultMap="roleResultMap"> select r.* from user_role t,role r where t.roleId=r.id and t.userId=#{userId} </select>
测试方法:
@Test public void getByIdWithSelectTest(){ SqlSession sqlSession=SqlSessionFactoryUtils.getSession(); UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //查询该员工所具有的角色 User user=userMapper.getByIdWithSelect(1); System.out.println(user); List<Role> roleList=user.getRoles(); roleList.forEach(n ->System.out.println(n)); }
四. user 到role 的嵌套结果 查询
UserMapper.java 接口
public User getByIdWithResult(int id);
UserMapper.xml sql
<resultMap type="user" id="userResultMapWithResult"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="description" column="description"/> <collection property="roles" javaType="ArrayList" ofType="role"> <id property="id" column="rid"/> <result property="name" column="rname"/> </collection> </resultMap> <!-- 用具体的语句,而不是 * 这样的。 --> <select id="getByIdWithResult" parameterType="int" resultMap="userResultMapWithResult"> select u.*,r.id as rId,r.name as rname from user u,role r ,user_role ur where u.id=ur.userId and r.id=ur.roleId and ur.userId=#{id} </select>
测试方法:
@Test public void getByIdWithResultTest(){ SqlSession sqlSession=SqlSessionFactoryUtils.getSession(); UserMapper userMapper=sqlSession.getMapper(UserMapper.class); User user=userMapper.getByIdWithResult(1); System.out.println(user); List<Role> roleList=user.getRoles(); roleList.forEach(n ->System.out.println(n)); }
可以发现,多对多实际上就是两个一对多的关系。 完全按照一对多的模式进行做。
但是一般,员工和角色表时不但会有userId,roleId, 还常常会有 created_by,created_time, 即创建人和创建时间等字段,这个时候就不能用上面的方式, 需要引入另外一个实体 UserRole 实体, 转换成两个一对多的关系, User与UserRole 是一对多的关系,Role 与UserRole 也是一对多的关系。
五. UserRolee 实体类的两个一对多关联
User 表 和Role 表,保持不变。
对UserRole 表,添加两个字段。
那么User.java 实体类就改变成了:
package com.yjl.pojo; import java.util.List; /** @author:yuejl @date: 2019年6月15日 上午11:11:02 @Description Mybatis 使用的基本类 User */ public class User { /** * @param id id编号,自增 * @param name 姓名 * @param age 年龄 * @param sex 性别 * @param description 描述 */ private Integer id; private String name; private Integer age; private String sex; private String description; //引入员工角色的多个对象集合。。 private List<UserRole> userRole; public User(){ } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public List<UserRole> getUserRole() { return userRole; } public void setUserRole(List<UserRole> userRole) { this.userRole = userRole; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", description=" + description + "]"; } }
其中的 Role.java 变成了:
package com.yjl.pojo; import java.util.List; /** @author:两个蝴蝶飞 @date: 2019年3月2日 下午6:18:51 @Description 角色组 */ public class Role { /** * @param id 角色编号 * @param name 用户的名称 */ private Integer id; private String name; public Role() { } public Role(String name) { this.name = name; } /** * @param userRole 用户角色实体 是一对多的关系 */ private List<UserRole> userRole; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<UserRole> getUserRole() { return userRole; } public void setUserRole(List<UserRole> userRole) { this.userRole = userRole; } @Override public String toString() { return "Role [id=" + id + ", name=" + name + "]"; } }
新增加一个UserRole.java 类:
package com.yjl.pojo; import java.util.Date; /** @atuhor:yuejl @Description: 类描述 */ public class UserRole { private User user; private Role role; private String created_by; private Date created_date; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } public String getCreated_by() { return created_by; } public void setCreated_by(String created_by) { this.created_by = created_by; } public Date getCreated_date() { return created_date; } public void setCreated_date(Date created_date) { this.created_date = created_date; } @Override public String toString() { return "UserRole [user=" + user + ", role=" + role + ", created_by=" + created_by + ", created_date=" + created_date.toLocaleString() + "]"; } }