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

描述一下问题:
已知有两个表,一个是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严格意义上来说对应的是结果集里面的列名,而不是数据库中的列,比如起别名的话就对应的是别名,不是原来的列,切记切记!!!
欢迎关注:雄雄的小课堂

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/325511.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java中的List你真的会用吗

转载自 Java中的List你真的会用吗 List是Java中比较常用的集合类&#xff0c;关于List接口有很多实现类&#xff0c;本文就来简单介绍下其中几个重点的实现ArrayList、LinkedList和Vector之间的关系和区别。 List List 是一个接口&#xff0c;它继承于Collection的接口。它…

Android 全局字体设置 例如楷体

1、在res下新建资源文件目录font&#xff0c;把字体文件拷贝到font文件夹中 2、在AndroidManifest.xml中的application节点下&#xff0c;设置全局style&#xff0c;引入字体文件 <item name"android:fontFamily">font/pingfang_sc_regular</item>或者

JS中闭包的简介

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>01_理解闭包</title> </head> <body> <!-- 1. 如何产生闭包?* 当一个嵌套的内部(子)函数引用了嵌套的外部(父)函数的变量(函数)…

.Net Core中使用ref和Spanamp;lt;Tamp;gt;提高程序性能

一、前言 其实说到ref&#xff0c;很多同学对它已经有所了解&#xff0c;ref是C# 7.0的一个语言特性&#xff0c;它为开发人员提供了返回本地变量引用和值引用的机制。Span 也是建立在ref语法基础上的一个复杂的数据类型&#xff0c;在文章的后半部分&#xff0c;我会有一个例…

微服务为什么选Spring Cloud

转载自 微服务为什么选Spring Cloud 现如今微服务架构十分流行&#xff0c;而采用微服务构建系统也会带来更清晰的业务划分和可扩展性。同时&#xff0c;支持微服务的技术栈也是多种多样的&#xff0c;本系列文章主要介绍这些技术中的翘楚——Spring Cloud。这是序篇&#x…

压力与动力是否成正比?

昨天在班里测试了下&#xff0c;检测他们数据库学的怎么样&#xff0c;看他们平时在课堂上的互动挺棒。看了下题&#xff0c;不是很难&#xff0c;满怀着愉悦的心情去打印了50份&#xff0c;挨个分发下去&#xff0c;由于我18级那边有课要上&#xff0c;所以这边的考试就辛苦王…

Android 获取屏幕宽度和高度直接转换为DP

WindowManager wm (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);DisplayMetrics dm new DisplayMetrics();wm.getDefaultDisplay().getMetrics(dm);int width dm.widthPixels; // 屏幕宽度&#xff08;像素&#xff09;int height dm.…

[开源] 基于ABP,Hangfire的开源Sharepoint文件同步解决方案----SuperRocket.SPSync

&#xff08;一&#xff09;项目背景 Sharepoint是微软的一个产品&#xff0c;很多公司都在使用它&#xff0c;也有很多公司以前使用它&#xff0c;现在可能需要移植到别的平台&#xff0c;也可能只是移植其中的文件存储&#xff0c;比如说移植到微软云&#xff0c;或者亚马逊云…

Spring Boot 2.x 启动全过程源码分析(上)入口类剖析

转载自 Spring Boot 2.x 启动全过程源码分析&#xff08;上&#xff09;入口类剖析 Spring Boot 的应用教程我们已经分享过很多了&#xff0c;今天来通过源码来分析下它的启动过程&#xff0c;探究下 Spring Boot 为什么这么简便的奥秘。 本篇基于 Spring Boot 2.0.3 版本进…

永远不要、不要、不要、不要放弃

Never, never, never, never give up. 永远不要、不要、不要、不要放弃。今天来写一下18级学生们的状态吧&#xff0c;最近主要是解决了1班的三大问题&#xff0c;第一&#xff0c;上机测试问题。第二&#xff0c;周一到四期间学习任务安排问题。第三&#xff0c;学习氛围的进一…

JS的时间定时器

<script>var t null;t setTimeout(time, 1000); //開始运行function time() {clearTimeout(t); //清除定时器dt new Date();var y dt.getFullYear();var mt dt.getMonth() 1;var day dt.getDate();var h dt.getHours(); //获取时var m dt.getMinutes(); //获取分…

Android隐藏状态栏和标题栏

requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题栏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 隐藏状态栏setContentView(R.layout.activity_ceshi);

微软正式发布XAML Standard与.NET Standard 2.0:现已提供下载

微软在本月早些时候召开的 Build 2017 开发者大会上的披露的 XAML Standard 和 .NET Standard 2.0&#xff0c;现已正式发布。新工具旨在为开发者们带来“基于同一标准的跨平台 XAML 语言结构”&#xff08;基于 UWP 和 Xamarin.Forms&#xff09;&#xff0c;以及基于社区反馈…

Spring Boot 2.X 来临,本文将带你起飞

转载自 Spring Boot 2.X 来临&#xff0c;本文将带你起飞 当前互联网技术盛行&#xff0c;以Spring 框架为主导的Java 互联网技术成了主流&#xff0c;而基于Spring 技术衍生出来的Spring Boot&#xff0c;采用了“约定优于配置”的思想&#xff0c;极大地简化了Spring 框架…

时间胶囊——给未来的留言板

时间胶囊”是一个给未来的留言板&#xff0c;你可以为自己&#xff0c;朋友&#xff0c;爱人&#xff0c;家人&#xff0c;或者任何人留下你现在想对他们说的话、图片&#xff0c;将来某一天&#xff0c;他们将来这里打开“时间胶囊”读到你的留言&#xff01;那么“时间胶囊”…

Echarts五步法加初体验

使用步骤&#xff1a; 引入echarts 插件文件到html页面中准备一个具备大小的DOM容器 <div id"main" style"width: 600px;height:400px;"></div>初始化echarts实例对象 var myChart echarts.init(document.getElementById(main));指定配置项…

Android软键盘弹出时,覆盖布局,不是把布局顶上去的解决方法

方法一&#xff1a; 在你的activity中的oncreate中setContentView之前写上这个代码 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);方法二&#xff1a; 如果想activity页面整体上移&#xff0c;在项目的AndroidManifest.xml文件中界面对应…

win7禁用其他软件只启用自定义软件的方法

gpedit.msc 用户配置里的管理模板→系统→右侧窗口里的“只运行。。。。”或“不运行。。。。” 然后输入&#xff1a; notepad.exe winword.exe powerpnt.exe excel.exe typeeasy.exe mmc.exe 360se.exe Hbuilde.exe

深入理解C#:编程技巧总结(一)

以下总结参阅了&#xff1a;MSDN文档、《C#高级编程》、《C#本质论》、前辈们的博客等资料&#xff0c;如有不正确的地方&#xff0c;请帮忙及时指出&#xff01;以免误导&#xff01; 1..实现多态性的两种方式&#xff1a;继承抽象类、实现接口 其实就是协变的应用&#xff…

史上最全 69 道 Spring 面试题和答案

转载自 史上最全 69 道 Spring 面试题和答案 目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程&#xff08;AOP&#xff09; Spring MVC Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架。Spring主要…