解決mybatis一對多查詢resultMap只返回了一條記錄問題
問題描述:因為領(lǐng)導的一個需求,需要用到使用resultMap,很久沒使用了,結(jié)果就除了點意外。就記錄下這個問題
準備兩個類:author(作者)和book(書),數(shù)據(jù)庫創(chuàng)建對應(yīng)的author->book一對多的數(shù)據(jù)
@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執(zhí)行查看結(jié)果:
{ "code": "200", "msg": "成功", "data": { "id": 1, "name": "法外狂徒張三", "phone": null, "address": null, "books": [ { "id": 1, "name": "法外狂徒張三", "press": "人民出版社", "price": 10.00, "authorId": 1 } ] } }
發(fā)現(xiàn)問題:本來author對應(yīng)book有兩條記錄,結(jié)果books里面只返回了一條記錄。
問題原因:2張表的主鍵都叫id,所以導致結(jié)果不能正確展示。
解決方法:1、主鍵使用不用的字段名。2、查詢sql時使用別名
1、主鍵使用不用的字段名,涉及到更改數(shù)據(jù)庫,只需要更改其中一個即可 。這里演示將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,數(shù)據(jù)庫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>
到此這篇關(guān)于mybatis一對多查詢resultMap只返回了一條記錄的文章就介紹到這了,更多相關(guān)mybatis一對多查詢resultMap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解析Mybatis對sql表的一對多查詢問題
- 關(guān)于mybatis一對一查詢一對多查詢遇到的問題
- MybatisPlus實現(xiàn)對象嵌套關(guān)聯(lián)查詢一對多List集合查詢
- springboot使用mybatis一對多的關(guān)聯(lián)查詢問題記錄
- 在MyBatis中實現(xiàn)一對多查詢和多對一查詢的方式詳解(各兩種方式)
- 基于mybatis一對多查詢內(nèi)層排序的問題
- Mybatis一對多和多對一處理的深入講解
- springboot整合mybatis實現(xiàn)簡單的一對多級聯(lián)查詢功能
- MyBatis圖文并茂講解注解開發(fā)一對多查詢
相關(guān)文章
使用Springboot實現(xiàn)OAuth服務(wù)的示例詳解
OAuth(Open Authorization)是一個開放標準,用于授權(quán)第三方應(yīng)用程序訪問用戶資源,而不需要共享用戶憑證。本文主要介紹了如何使用Springboot實現(xiàn)一個OAuth服務(wù),需要的可以參考一下2023-05-05Spring?Native打包本地鏡像的操作方法(無需通過Graal的maven插件buildtools)
這篇文章主要介紹了Spring?Native打包本地鏡像,無需通過Graal的maven插件buildtools,本文探索一下,如果不通過這個插件來生成鏡像,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-02-02SpringBoot響應(yīng)處理實現(xiàn)流程詳解
這篇文章主要介紹了SpringBoot響應(yīng)處理實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-10-10java使用mybatis調(diào)用存儲過程返回一個游標結(jié)果集方式
這篇文章主要介紹了java使用mybatis調(diào)用存儲過程返回一個游標結(jié)果集方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01