mybatis框架-使用resultMap实现高级结果映射,association属性
需求:查询数特定角色下的所有用户列表
首先需要在在User类中引用Role类,因为引用了复杂的数据类型,所以要使用association属性进行映射,其实起主要作用的还是resultMap属性。
/**
* 根绝用户的角色id,获取该角色下的所有用户的信息
* @param roleid
* @return
*/
public List<User> getUserListByRoleID(@Param("userRole1") Integer roleid);
<resultMap type="User" id="UserRoleResult">
<id property="id" column="id"/>
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="userRole" column="userRole" />
<!--User类中引用的Role类 -->
<association property="role" javaType="Role" resultMap="roleResult"/>
</resultMap>
<resultMap type="Role" id="roleResult">
<id property="id" column="b_id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</resultMap>
<!--根绝用户的角色id,获取该角色下的所有用户的信息 -->
<select id="getUserListByRoleID" resultMap="UserRoleResult" parameterType="Integer">
SELECT a.*,b.id as b_id,b.rolename from smbms_user a,smbms_role b where a.userrole=b.id and b.id=#{userRole1}
</select>
1 package cn.smbms.pojo; 2 3 import java.util.Date; 4 5 public class User { 6 private Integer id; //id 7 private String userCode; //用户编码 8 private String userName; //用户名称 9 private String userPassword; //用户密码 10 private Integer gender; //性别 11 private Date birthday; //出生日期 12 private String phone; //电话 13 private String address; //地址 14 private Integer userRole; //用户角色 15 private Integer createdBy; //创建者 16 private Date creationDate; //创建时间 17 private Integer modifyBy; //更新者 18 private Date modifyDate; //更新时间 19 private Role role;//用户角色 20 21 22 public Role getRole() { 23 return role; 24 } 25 public void setRole(Role role) { 26 this.role = role; 27 } 28 public Integer getId() { 29 return id; 30 } 31 public void setId(Integer id) { 32 this.id = id; 33 } 34 public String getUserCode() { 35 return userCode; 36 } 37 public void setUserCode(String userCode) { 38 this.userCode = userCode; 39 } 40 public String getUserName() { 41 return userName; 42 } 43 public void setUserName(String userName) { 44 this.userName = userName; 45 } 46 public String getUserPassword() { 47 return userPassword; 48 } 49 public void setUserPassword(String userPassword) { 50 this.userPassword = userPassword; 51 } 52 public Integer getGender() { 53 return gender; 54 } 55 public void setGender(Integer gender) { 56 this.gender = gender; 57 } 58 public Date getBirthday() { 59 return birthday; 60 } 61 public void setBirthday(Date birthday) { 62 this.birthday = birthday; 63 } 64 public String getPhone() { 65 return phone; 66 } 67 public void setPhone(String phone) { 68 this.phone = phone; 69 } 70 public String getAddress() { 71 return address; 72 } 73 public void setAddress(String address) { 74 this.address = address; 75 } 76 public Integer getUserRole() { 77 return userRole; 78 } 79 public void setUserRole(Integer userRole) { 80 this.userRole = userRole; 81 } 82 public Integer getCreatedBy() { 83 return createdBy; 84 } 85 public void setCreatedBy(Integer createdBy) { 86 this.createdBy = createdBy; 87 } 88 public Date getCreationDate() { 89 return creationDate; 90 } 91 public void setCreationDate(Date creationDate) { 92 this.creationDate = creationDate; 93 } 94 public Integer getModifyBy() { 95 return modifyBy; 96 } 97 public void setModifyBy(Integer modifyBy) { 98 this.modifyBy = modifyBy; 99 } 100 public Date getModifyDate() { 101 return modifyDate; 102 } 103 public void setModifyDate(Date modifyDate) { 104 this.modifyDate = modifyDate; 105 } 106 }
1 @Test 2 public void testGetUserListByRoleId(){ 3 SqlSession sqlSession = null; 4 List<User> userList = new ArrayList<User>(); 5 try { 6 sqlSession = MyBatisUtil.createSqlSession(); 7 8 9 userList = sqlSession.getMapper(UserMapper.class).getUserListByRoleID(2); 10 11 } catch (Exception e) { 12 // TODO: handle exception 13 e.printStackTrace(); 14 }finally{ 15 MyBatisUtil.closeSqlSession(sqlSession); 16 } 17 for(User user: userList){ 18 logger.debug("testGetUserListByRoleId roleid: " + user.getUserCode() + " and userName: " + user.getUserName()+"and userRoleName:"+user.getRole().getRoleName()); 19 } 20 }