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

修改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>
 package cn.smbms.pojo;

 import java.util.Date;
import java.util.List; public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private Role role;//用户角色
private List<Address> addressList;//一个用户有多个地址列表 public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.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 Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer 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;
}
}
 @Test
public void testGetUserListAddressByUserId(){
SqlSession sqlSession = null;
List<User> userList = new ArrayList<User>();
User user=new User();
try {
sqlSession = MyBatisUtil.createSqlSession(); user = sqlSession.getMapper(UserMapper.class).getUserListAddressByUserID(1); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
//去掉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 } }

mybatis框架-使用resultMap实现高级结果映射,collection属性的使用的更多相关文章

  1. mybatis框架-使用resultMap实现高级结果映射,association属性

    需求:查询数特定角色下的所有用户列表 首先需要在在User类中引用Role类,因为引用了复杂的数据类型,所以要使用association属性进行映射,其实起主要作用的还是resultMap属性. /* ...

  2. (转)MyBatis框架的学习(五)——一对一关联映射和一对多关联映射

    http://blog.csdn.net/yerenyuan_pku/article/details/71894172 在实际开发中我们不可能只是对单表进行操作,必然要操作多表,本文就来讲解多表操作中 ...

  3. 使用resultMap实现高级结果映射

    使用resultMap实现高级结果映射 resultMap的属性: 1.属性 id:resultMap的唯一标识.type:resulMap的映射结果类型(一般为Java实体类).2.子节点 id:一 ...

  4. MyBatis 入门到精通(三) 高级结果映射

    MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程序,也将是非常 ...

  5. oracle使用resultMap实现高级结果映射

    resultMap的属性: 1.属性 id:resultMap的唯一标识.type:resulMap的映射结果类型(一般为Java实体类).2.子节点 id:一般对应数据库的主键 id,设置此项可以提 ...

  6. MyBatis(八):高级结果映射

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  7. Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)

    本篇主要介绍mybatis反射工具中用到的三个属性工具类:PropertyTokenizer.PropertyNamer.PropertyCopier. PropertyTokenizer: 主要用来 ...

  8. MyBatis框架之SQL映射和动态SQL

    使用MyBatis实现条件查询 1.SQL映射文件: MyBatis真正的强大之处就在于SQL映射语句,MyBatis专注于SQL,对于开发人员来说也是极大限度的进行SQL调优,以保证性能.下面是SQ ...

  9. mybatis学习记录四——输入、输出映射

    6      输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 6.1.1     需求 完成用户信息的综合查询,需要传入查询条件很 ...

随机推荐

  1. 通过欧拉计划学Rust(第1~6题)

    最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,看来想准确理解MOVE的机制,还需要对Rust有深刻的理解,所以开始了Rust的快速入门学习. 看了一下网上有关Rust的介绍,都 ...

  2. 文件上传之靶场upload-labs (11-20)

    第十一关 strrpos() 函数查找字符串在另一字符串中最后一次出现的位置 substr() 函数返回字符串的一部分 文件保存的方式是上传路径+随机时间+截取的文件后缀 其中上传路径可控,可以利用这 ...

  3. Feign切换client到okhttp无法生效天坑!(附带发生的原因)

    提示:如果只看如何解决问题,请看文章的末尾如何解决这个问题 1. 场景描述 最近项目中使用了feign当做http请求工具来使用.相对于httpclient.resttemplate来说,fegin用 ...

  4. 关于优秀的视频播放器 - PotPlayer

    播放器设置 直接截图: 其他重要功能 1. 切换语音:Alt + A 谢谢浏览!

  5. 前端学习:学习笔记(JS部分)

    前端学习:学习笔记(JS部分) 前端学习:JS学习总结(图解)    JS的简介 JS基本语法 JS内置对象 JS的函数 JS的事件 JS的BOM JS的DOM JS的简介 新建步骤 <body ...

  6. C#4.0的十种语法糖

    https://www.cnblogs.com/dotnet261010/p/6055092.html

  7. 【翻译】Tusdotnet中文文档(3)自定义功能和相关技术

    自定义功能和相关技术 本篇按照如下结构翻译 自定义功能 自定义数据仓库 相关技术 架构和总体概念 自定义数据仓库 tusdotnet附带一个存储库TusDiskStore,它将文件保存在磁盘上的一个目 ...

  8. C# HttpWebRequest和WebClient的区别 通过WebClient/HttpWebRequest实现http的post/get方法

    一 HttpWebReques1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();2,其Method指定了请求类型,这里用的GET,还 ...

  9. Python - 注释 - 第四天

    注释 确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释: Python中单行注释以 # 开头,例如: # 这是一个注释 print('Hello Pytho ...

  10. ASP.NET MVC IOC 之 Autofac(三)-webform中应用

    在webform中应用autofac,只有global中的写法不一样,其他使用方式都一样 nuget上引用: global中的写法: private void AutoFacRegister() { ...