解决mybatis一对多查询resultMap只返回了一条记录问题
程序员文章站
2022-06-19 10:51:10
问题描述:因为领导的一个需求,需要用到使用resultmap,很久没使用了,结果就除了点意外。就记录下这个问题准备两个类:author(作者)和book(书),数据库创建对应的author->b...
问题描述:因为领导的一个需求,需要用到使用resultmap,很久没使用了,结果就除了点意外。就记录下这个问题
准备两个类:author(作者)和book(书),数据库创建对应的author->book一对多的数据
@data public class author { private integer id; private string name; private string phone; private string address; private list<book> books; } @data public class book { private integer id; private string name; private string press; private bigdecimal price; private integer authorid; }
开始的mapper.xml文件
<resultmap id="bookmap" type="com.example.demo.dto.author"> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="address" property="address"></result> <result column="phone" property="phone"></result> <collection property="books" oftype="com.example.demo.dto.book"> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="press" property="press"></result> <result column="price" property="price"></result> <result column="author_id" property="authorid"></result> </collection> </resultmap> <select id="queryauthorinfo" parametertype="java.lang.string" resultmap="bookmap"> select t1.*,t2.* from author t1 inner join book t2 on t1.id=t2.author_id where t1.id=#{id} </select>
使用postman执行查看结果:
{ "code": "200", "msg": "成功", "data": { "id": 1, "name": "法外狂徒张三", "phone": null, "address": null, "books": [ { "id": 1, "name": "法外狂徒张三", "press": "人民出版社", "price": 10.00, "authorid": 1 } ] } }
发现问题:本来author对应book有两条记录,结果books里面只返回了一条记录。
问题原因:2张表的主键都叫id,所以导致结果不能正确展示。
解决方法:1、主键使用不用的字段名。2、查询sql时使用别名
1、主键使用不用的字段名,涉及到更改数据库,只需要更改其中一个即可 。这里演示将book的id更改为book_id
<resultmap id="bookmap" type="com.example.demo.dto.author"> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="address" property="address"></result> <result column="phone" property="phone"></result> <collection property="books" oftype="com.example.demo.dto.book"> <!---更改book类的id为bookid,数据库book的id更改为book_id--> <id column="book_id" property="bookid"></id> <result column="name" property="name"></result> <result column="press" property="press"></result> <result column="price" property="price"></result> <result column="author_id" property="authorid"></result> </collection> </resultmap> <select id="queryauthorinfo" parametertype="java.lang.string" resultmap="bookmap"> select t1.*,t2.* from author t1 inner join book t2 on t1.id=t2.author_id where t1.id=#{id} </select>
2、查询sql时使用别名。这里演示将查询book时id 更改别名为 bookid
<resultmap id="bookmap" type="com.example.demo.dto.author"> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="address" property="address"></result> <result column="phone" property="phone"></result> <collection property="books" oftype="com.example.demo.dto.book"> <!---这里将column值id更改为别名一致bookid--> <id column="bookid" property="id"></id> <result column="name" property="name"></result> <result column="press" property="press"></result> <result column="price" property="price"></result> <result column="author_id" property="authorid"></result> </collection> </resultmap> <select id="queryauthorinfo" parametertype="java.lang.string" resultmap="bookmap"> <!---这里新增了t2.id as bookid--> select t1.*,t2.id as bookid, t2.* from author t1 inner join book t2 on t1.id=t2.author_id where t1.id=#{id} </select>
到此这篇关于mybatis一对多查询resultmap只返回了一条记录的文章就介绍到这了,更多相关mybatis一对多查询resultmap内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!