問:查詢完成后,結果列只能展示單表內的字段,比如用戶表表中只記錄角色編號,沒有角色名稱。結果列要如何展示在另一個表內的字段——角色名稱呢?
答:通過resultMap映射自定義結果顯示該字段。
1.resultMap的節點與屬性值
id屬性:唯一標識,次id值用於select元素resultMap屬性的引用
type屬性:表示該resultMap的映射結果類型,一般來說resultMap的type與select節點中的parameterType類型一致
result子節點:用於表示一些簡單的屬性
(1)column屬性:表示從數據庫中查詢的字段名
(2)property:表示查詢出來的字段對應的值賦給實體對象的哪個屬性
2.使用方法
(1)SQL映射文件
<select id="getUserListByUser" parameterType="User" resultMap="userAndRole"> select u.*,r.roleName from smbms_user u,smbms_role r where u.userRole = r.id and userName like concat('%',#{userName},'%') and userRole = #{userRole} </select> <resultMap type="User" id="userAndRole"> <result property="userRole" column="roleName"/> </resultMap>
(2)UserMapper接口
public List<User> getUserListByUser(User user);
(3)User.java
public class User { private Integer id; //用戶編號 private String userCode; //用戶編碼 private String userName; //用戶名 private String userPassword; //密碼 private Integer gender; //性別 private Date birthday; //生日 private String phone; //電話 private String address; //地址 private String userRole; //角色id //配合resultMap顯示另一個表內的字段:角色名稱 //添加getter與setter方法 private String userRoleName; // private Integer createdBy; //創建人 private Date creationDate; //創建日期 private Integer modifyBy; //修改人 private Date modifyDate; //修改日期 //用戶對應角色,可以通過用戶訪問到角色的所有信息,復雜映射 private Role role; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getUserRole() { return userRole; } public void setUserRole(String userRole) { this.userRole = userRole; } public Integer getCreatedBy() { return createdBy; } public void setCreatedBy(Integer createdBy) { this.createdBy = createdBy; } public Date getCreationDate() { return creationDate; } public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } public Integer getModifyBy() { return modifyBy; } public void setModifyBy(Integer modifyBy) { this.modifyBy = modifyBy; } public Date getModifyDate() { return modifyDate; } public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate; } public String getUserRoleName() { return userRoleName; } public void setUserRoleName(String userRoleName) { this.userRoleName = userRoleName; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } }
(4)UserService.java
@Test public void testGetUserListByUser(){ SqlSession session = null; List<User> userList = null; try { session = MybatisUtil.createSqlSession(); User user1 = new User(); user1.setUserName("用戶"); user1.setUserRole("1"); userList=session.getMapper(UserMapper.class).getUserListByUser(user1); session.commit(); } catch (Exception e) { e.printStackTrace(); if (session != null) { session.rollback(); } } finally { if (session != null) { session.close(); } } for(User user : userList){ log.debug("user =====> "+user.getUserName()+"\t"+user.getUserRoleName()); } }
3.步驟總結
(1)首先在User類加入userRoleName屬性,加入該屬性的getter與setter方法
(2)修改UserMapper接口中的對應查詢方法
(3)修改UserMapper.xml中<select>中的resultType屬性為resultMap,注意其值為resultMap節點的id屬性值
(4)注意resultMap中的property屬性值與column屬性值
(5)若要使用自動映射,則實體對象的屬性名要與數據庫字段名一致,否則就自定義結果映射(如本例)