您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么解決mybatis一對多查詢resultMap只返回了一條記錄問題”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
問題描述:因為領導的一個需求,需要用到使用resultMap,很久沒使用了,結果就除了點意外。就記錄下這個問題
準備兩個類:author(作者)和book(書),數(shù)據(jù)庫創(chuà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í)行查看結果:
{ "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對應book有兩條記錄,結果books里面只返回了一條記錄。
問題原因:2張表的主鍵都叫id,所以導致結果不能正確展示。
解決方法: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>
“怎么解決mybatis一對多查詢resultMap只返回了一條記錄問題”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。