Requirements: Gets the user's user information and address lists
Modify user entity class to add a reference collection.
/ **
* eradicate user id, get the address information in this role
* @param userID
* @return
* /
public the User getUserListAddressByUserID (@Param ( "userID") Integer userID);
<The resultMap type = "the User" ID = "userAddressList">
<ID Property = "ID" column = "ID" />
<Result Property = "userCode" column = "userCode" />
<Result Property = "the userName" column = "the userName" />
<Property result = "userRole" column = "userRole" />
<-! List collection class User class referenced in order to reuse the same, the results may be mapped using resultMap collection elements mentioned outside, and the association of this usage is the same ->
<= Collection Property "as addressList" ofType = "the Address" >
<Property ID = "ID" column = "B_ID" />
<Property Result = "Contact" column = "Contact" />
<Property Result = "addressDesc" column = "addressDesc" />
<result property="tel" column="tel" />
</collection>
</resultMap>
<select id="getUserListAddressByUserID" resultMap="userAddressList" parameterType="Integer">
SELECT a.*,b.id as b_id,b.contact,b.addressdesc,b.tel from smbms_user a,smbms_address b where a.id=b.userid and a.id=#{userID}
</select>
. 1 Package cn.smbms.pojo; 2 . 3 Import java.util.Date; . 4 Import java.util.List; . 5 . 6 public class the User { . 7 Private Integer ID; // ID . 8 Private String userCode; // user code . 9 Private userName String; // username 10 Private String the userPassword; // user password 11 Private Integer gender; // sex 12 Private a Date Birthday; // date of birth 13 Private String Phone; // telephone 14 Private String address; // address 15 Private Integer userRole; // user roles 16 Private Integer createdBy; // creator 17 Private a Date creationDate; // Created 18 Private Integer modifyBy; // updater 19 Private a Date ModifyDate; // updated 20 Private role role; // user roles 21 private List<Address> addressList;//一个用户有多个地址列表 22 23 24 25 public List<Address> getAddressList() { 26 return addressList; 27 } 28 public void setAddressList(List<Address> addressList) { 29 this.addressList = addressList; 30 } 31 public Role getRole() { 32 return role; 33 } 34 public void setRole(Role role) { 35 this.role = role; 36 } 37 public Integer getId() { 38 return id; 39 } 40 public void setId(Integer id) { 41 this.id = id; 42 } 43 public String getUserCode() { 44 return userCode; 45 } 46 public void setUserCode(String userCode) { 47 this.userCode = userCode; 48 } 49 public String getUserName() { 50 return userName; 51 } 52 public void setUserName(String userName) { 53 this.userName = userName; 54 } 55 public String getUserPassword() { 56 return userPassword; 57 } 58 public void setUserPassword(String userPassword) { 59 this.userPassword = userPassword; 60 } 61 public Integer getGender() { 62 return gender; 63 } 64 public void setGender(Integer gender) { 65 this.gender = gender; 66 } 67 public Date getBirthday() { 68 return birthday; 69 } 70 public void setBirthday(Date birthday) { 71 this.birthday = birthday; 72 } 73 public String getPhone() { 74 return phone; 75 } 76 public void setPhone(String phone) { 77 this.phone = phone; 78 } 79 public String getAddress() { 80 return address; 81 } 82 public void setAddress(String address) { 83 this.address = address; 84 } 85 public Integer getUserRole() { 86 return userRole; 87 } 88 public void setUserRole(Integer userRole) { 89 this.userRole = userRole; 90 } 91 public Integer getCreatedBy() { 92 return createdBy; 93 } 94 public void setCreatedBy(Integer createdBy) { 95 this.createdBy = createdBy; 96 } 97 public Date getCreationDate() { 98 return creationDate; 99 } 100 public void setCreationDate(Date creationDate) { 101 this.creationDate = creationDate; 102 } 103 public Integer getModifyBy() { 104 return modifyBy; 105 } 106 public void setModifyBy(Integer modifyBy) { 107 this.modifyBy = modifyBy; 108 } 109 public Date getModifyDate() { 110 return modifyDate; 111 } 112 public void setModifyDate(Date modifyDate) { 113 this.modifyDate = modifyDate; 114 } 115 }
1 @Test 2 public void testGetUserListAddressByUserId(){ 3 SqlSession sqlSession = null; 4 List<User> userList = new ArrayList<User>(); 5 User user=new User(); 6 try { 7 sqlSession = MyBatisUtil.createSqlSession(); 8 9 10 user = sqlSession.getMapper(UserMapper.class).getUserListAddressByUserID(1); 11 12 } catch(Exception E) { 13 is // the TODO: Exception handle 14 e.printStackTrace (); 15 } the finally { 16 MyBatisUtil.closeSqlSession (SQLSESSION); . 17 } 18 is property class // remove role are possible 19 logger.debug("testGetUserListByRoleId roleid: " + user.getUserCode() + " and userName: " + user.getUserName()+"and userRoleName:"+11111111); The wording of the console 20 // output line of the log is not out, because of the use of the class attribute Role 21 // logger.debug("testGetUserListByRoleId roleid: " + user.getUserCode() + " and userName: " + user.getUserName()+"and userRoleName:"+user.getRole().getRoleName()); 22 for(Address address: user.getAddressList()){ 23 logger.debug("testGetUserListAddressByUserId contact: " + address.getContact() + " and addressDesc: " + address.getAddressDesc()+"and tel:"+address.getTel()); 24 } 25 26 }