mybatis报错:查询一对多或多对多时只返回一条数据的问题
2024-10-19 03:31:59
问题:
使用映射文件实现查询一对多或多对多时只返回一条数据问题
解决方法:
导致这种情况出现的问题是因为两个表中的主键是一样所以出现了数据覆盖问题。
解决方式一:修改数据库表中的主键(这种方法比较麻烦)。
解决方式二:在查询语句中修改一个表的主键使其不一致即可,例子:
表User主键为id,表Book主键为id,映射结果集时设置表Book的主键id为b_id即可。
<resultMap id="resultUserMap" type="books.com.boot.model.User">
<id property="id" column="id"/>
<result property="userName" column="user_name"/>
<result property="userPassword" column="user_password"/>
<result property="createUser" column="createuser"/>
<result property="createTime" column="createtime"/>
<result property="remark" column="remark"/>
<collection property="books" column="author" ofType="Book" >
<id property="id" column="b_id"/>
<result property="title" column="title"/>
<result property="author" column="author"/>
<result property="isbn" column="isbn"/>
<result property="isbnTime" column="isbn_time"/>
<result property="publisher" column="publisher"/>
<result property="decription" column="description"/>
<result property="createUser" column="createuser"/>
<result property="createTime" column="createtime"/>
</collection>
</resultMap>
备注:
属性property对应model类的属性名,column对应数据库表中的列名,其中id元素中的column可以修改为任意别名,result元素中的column列名必须与表列名一致。
目前id元素和result元素的区别就在这方面,其他有待补充。
最新文章
- jquery easyui-datagrid/treegrid 清空数据参考
- POJ3208:Apocalypse Someday
- 录制简单的自动化测试工具SlikMobile初体验
- 纯手工打造(不使用IDE)java web 项目
- repo: 创建local manifest以及如何添加app到CM/Android build系统中
- 应中DOS中断显示字符串(摘自《汇编语言》王爽)
- solr 相似查询-MoreLikeThis
- 1019.Line Painting(线段树 离散化)
- virtualbox+centos 7 实现宿主机器互通
- myeclipse2015 安装配置
- 我的学习之路_第二十章_JDBC
- cookie和session(一)
- 在ARC下,assign和weak的区别
- django-restframework之缓存系统
- MD5 Hashing in Java
- three.js 第二篇:场景 相机 渲染器 物体之间的关系
- python自学第12天 模块
- 返回Json格式结果
- C# 编写WCF简单的服务端与客户端
- Git文件状态