描述一下问题:
已知有两个表,一个是user表,一个是address,一(user)对多(address)的关系,在user的实体类里面写属性:

private List<Address> addressList = new ArrayList<Address>();public List<Address> getAddressList() {return addressList;}public void setAddressList(List<Address> addressList) {this.addressList = addressList;}

然后写了一个测试方法,根据用户的编号查询地址信息,一个用户对应多个地址。
配置如下:

<resultMap type="User" id="userAddMap"><id property="id" column="id"/><result property="userName" column="userName"/><result property="userCode" column="userCode"/><result property="userPassword" column="userPassword"/><result property="gender" column="gender"/><result property="birthday" column="birthday"/><result property="phone" column="phone"/><result property="address" column="address"/><result property="userRole" column="userRole"/><result property="createdBy" column="createdBy"/><result property="creationDate" column="creationDate"/><result property="modifyBy" column="modifyBy"/><result property="modifyDate" column="modifyDate"/><result property="roleName" column="roleName"/><collection property="addressList" ofType="Address"><id property="aid" column="id"/><result property="contact" column="contact"/><result property="addressDesc" column="addressDesc"/><result property="postCode" column="postCode"/><result property="tel" column="tel"/><result property="createdBy" column="createdBy"/><result property="creationDate" column="creationDate"/><result property="modifyBy" column="modifyBy"/><result property="modifyDate" column="modifyDate"/><result property="userId" column="userId"/></collection></resultMap>

sql映射如下:

<select id="findUserByidAndAddress" parameterType="Integer" resultMap="userAddMap">SELECT u.*,a.id,a.contact,a.addressdesc,a.postCodeFROM smbms_user u,smbms_address a WHERE u.id = a.userid AND  u.id = #{id} </select>

看起来没有任何问题吧,写了个测试方法:

@Testpublic void findUserByidAndAddress(){SqlSession sqlSession = utils.getSqlSession();User user = sqlSession.getMapper(IUserDao.class).findUserByidAndAddress(1);List<Address> addressList = user.getAddressList();for (Address address : addressList) {System.out.println(address.getAddressDesc());}}

运行结果如图所示:

可是数据库中却有三条记录:

经过自己琢磨,不行。
然后上网查了下,原来是数据库中两个表中的主键都是id,如果配置collection一对多关联的话需要改别名,我就试着改了下,发现,可以了。
改过之后的映射文件如下:

<resultMap type="User" id="userAddMap"><id property="id" column="id"/><result property="userName" column="userName"/><result property="userCode" column="userCode"/><result property="userPassword" column="userPassword"/><result property="gender" column="gender"/><result property="birthday" column="birthday"/><result property="phone" column="phone"/><result property="address" column="address"/><result property="userRole" column="userRole"/><result property="createdBy" column="createdBy"/><result property="creationDate" column="creationDate"/><result property="modifyBy" column="modifyBy"/><result property="modifyDate" column="modifyDate"/><result property="roleName" column="roleName"/><collection property="addressList" ofType="Address"><id property="aid" column="aid"/><result property="contact" column="contact"/><result property="addressDesc" column="addressDesc"/><result property="postCode" column="postCode"/><result property="tel" column="tel"/><result property="createdBy" column="createdBy"/><result property="creationDate" column="creationDate"/><result property="modifyBy" column="modifyBy"/><result property="modifyDate" column="modifyDate"/><result property="userId" column="userId"/></collection></resultMap>

sql语句如下:

 <select id="findUserByidAndAddress" parameterType="Integer" resultMap="userAddMap">SELECT u.*,a.id as aid,a.contact,a.addressdesc,a.postCodeFROM smbms_user u,smbms_address a WHERE u.id = a.userid AND  u.id = #{id} </select>

注意:resultMap中的property对应的是实体类里面的属性,而column严格意义上来说对应的是结果集里面的列名,而不是数据库中的列,比如起别名的话就对应的是别名,不是原来的列,切记切记!!!
欢迎关注:雄雄的小课堂

mybatis中,collection配置后查询只显示一条记录相关推荐

  1. mysql 只显示第一条记录_MySQL:此种查询结果,怎么仅保留第一条记录?

    select id, value from test; 查询结果如下 id value yy 123 zz 234 zz 456 仅想保留id字段的第一条记录,在5.7以下版本中使用"sel ...

  2. mybatis中collection中的ofType=“String“时

    mybatis中collection中的ofType="String"时 DTO: package com.example.mybatis.entity;import java.u ...

  3. MyBatis中使用流式查询避免数据量过大导致OOM

    欢迎关注方志朋的博客,回复"666"获面试宝典 今天mybatis查询数据库中大量的数据,程序抛出: java.lang.OutOfMemoryError: Java heap s ...

  4. mybatis中collection嵌套使用

    mybatis中collection嵌套使用 @author 无忧少年 @createTime 2019/10/25 1. 问题 刚完成的项目,突然又改了需求,在为了不大改动原先代码的原则下,得使用m ...

  5. rowbounds分页oracle,Oracle使用MyBatis中RowBounds实现分页查询功能

    Oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用MyBatis中的RowBounds进行分页查询,非常方便. 使用MyBatis中的RowBounds进行 ...

  6. 当ubuntu系统安装好或导入后ifconfig只显示lo的解决方法

    当ubuntu系统安装好或导入后ifconfig只显示lo的解决方法 1.先用命令查看网卡信息 ifconfig -a 在这里可以看到启用的和未启用的网卡信息,通过如下命令启用网卡: ifconfig ...

  7. el+vue实战 ② 在el-table中的每一行加上头像/图片;去掉div标签自动换行问题;el-table表格中实现字数限制,只显示一行

    处理之前: 处理之后: 1.在el-table中的每一行加上头像/图片 <el-table-column prop="customerName" label="姓名 ...

  8. 144显示器只有60_windows10系统中144hz显示器刷新率只显示60hz怎么办

    很多用户为了在游戏中有更好的显示效果,都会喜欢购买144HZ的显示器来使用,但是有不少用户发现在windows10系统中使用144hz显示器刷新率只显示60hz,这是什么情况呢,经过分析是win10会 ...

  9. SQl语句查询重复数据 只显示其中一条

    SQL查询重复数据,只显示其中一条 有重复数据主要有以下几种情况: 1.存在两条完全相同的纪录 这是最简单的一种情况,用关键字distinct就可以去掉 example: select distinc ...

最新文章

  1. 时序数据库连载系列: 时序数据库一哥InfluxDB之存储机制解析
  2. Mangofile.PersonalPlus5(x86) Crack
  3. Node中require方法的简单实现
  4. XamarinEssentials教程获取首选项的值
  5. vue2.0 + element ui 实现表格穿梭框
  6. IOS开发--深拷贝与浅拷贝(mutableCopy与Copy)详解
  7. 招聘行业颠覆者【伯小乐】| 手摸手产品研究院
  8. 【Linux系统编程】进程同步与互斥:POSIX有名信号量
  9. 大数据学习笔记22:MR案例——双MR统计总利润并排序
  10. 华为杯数学建模2020什么时候出结果_关于东北三省数学建模联赛及“华为杯”研究生数学建模竞赛的通知...
  11. 【图像融合】简单加权融合
  12. json处理为字符串,主要函数,dumps和loads
  13. Ubuntu 下的PDF阅读器
  14. 特征选择mRMR算法实现全解
  15. python3 爬虫https的坑 -- 已解决
  16. 数据库点滴之T-SQL面试语句,练练手
  17. 房子出租后房东能随意进入租户家中吗?
  18. 【软件安装】远程桌面软件系列
  19. 银保监会计算机类专业知识,国家公务员局2019中国银保监会国考内容:计算机类专业知识...
  20. 调用织梦DedeCMS当前栏目名称和栏目链接地址的方法

热门文章

  1. 2021年度训练联盟热身训练赛第五场 H题In-place Sorting+贪心构造
  2. A Mini Locomotive POJ - 1976(动态规划+思维)
  3. python resample函数_python时序分析之重采集(resample)
  4. mysql文献综述_文献综述随笔(二十)
  5. php files得不到导入的文件,PHP判断文件是否被引入的方法get_included_files用法示例...
  6. 蓝桥杯 人物相关性分析 二分
  7. 2019-03-22-算法-进化(环形链表)
  8. 2019-03-19-算法-进化(报数)
  9. 主席树有关的一些题目(持续更新)
  10. 2021牛客暑期多校训练营7 xay loves monotonicity 线段树区间合并