mybatis框架-使用resultMap实现高级结果映射,collection属性的使用

Posted dongyaotou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis框架-使用resultMap实现高级结果映射,collection属性的使用相关的知识,希望对你有一定的参考价值。

需求:获取指定用户的用户信息和地址列表  

修改user实体类  添加集合的引用。

/**
* 根绝用户id,获取该角色下的地址信息
* @param userID
* @return
*/
public User getUserListAddressByUserID(@Param("userID")Integer userID);

 

<resultMap type="User" id="userAddressList">
<id property="id" column="id"/>
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="userRole" column="userRole" />
<!--User类中引用的List集合类 同样为了复用,也可以将collection的映射结果使用resultMap元素提到外边,这个和association的用法是相同的 -->
<collection property="addressList" ofType="Address">
<id property="id" column="b_id"/>
<result property="contact" column="contact" />
<result property="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 User {
  7     private Integer id; //id 
  8     private String userCode; //用户编码
  9     private String userName; //用户名称
 10     private String userPassword; //用户密码
 11     private Integer gender;  //性别
 12     private Date birthday;  //出生日期
 13     private String phone;   //电话
 14     private String address; //地址
 15     private Integer userRole;    //用户角色
 16     private Integer createdBy;   //创建者
 17     private Date creationDate; //创建时间
 18     private Integer modifyBy;     //更新者
 19     private Date modifyDate;   //更新时间
 20     private Role role;//用户角色
 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             // TODO: handle exception
14             e.printStackTrace();
15         }finally{
16             MyBatisUtil.closeSqlSession(sqlSession);
17         }
18         //去掉role类的属性是可以的
19         logger.debug("testGetUserListByRoleId roleid: " + user.getUserCode() + " and userName: " + user.getUserName()+"and userRoleName:"+11111111);
20         //这种写法控制台一行日志是输不出来的,因为用到了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     }

以上是关于mybatis框架-使用resultMap实现高级结果映射,collection属性的使用的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis学习笔记—高级映射,延迟加载

mybatis中的resultMap 的高级映射

Mybatis 高级结果映射 ResultMap Association Collection

Mybatis 高级结果映射 ResultMap Association Collection

MyBatis框架-ResultMap节点

MyBatis高级