解決mybatis一對(duì)多查詢(xún)r(jià)esultMap只返回了一條記錄問(wèn)題
問(wèn)題描述:因?yàn)轭I(lǐng)導(dǎo)的一個(gè)需求,需要用到使用resultMap,很久沒(méi)使用了,結(jié)果就除了點(diǎn)意外。就記錄下這個(gè)問(wèn)題
準(zhǔn)備兩個(gè)類(lèi):author(作者)和book(書(shū)),數(shù)據(jù)庫(kù)創(chuàng)建對(duì)應(yīng)的author->book一對(duì)多的數(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;
}
開(kāi)始的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)問(wèn)題:本來(lái)author對(duì)應(yīng)book有兩條記錄,結(jié)果books里面只返回了一條記錄。
問(wèn)題原因:2張表的主鍵都叫id,所以導(dǎo)致結(jié)果不能正確展示。
解決方法:1、主鍵使用不用的字段名。2、查詢(xún)sql時(shí)使用別名
1、主鍵使用不用的字段名,涉及到更改數(shù)據(jù)庫(kù),只需要更改其中一個(gè)即可 。這里演示將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類(lèi)的id為bookId,數(shù)據(jù)庫(kù)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、查詢(xún)sql時(shí)使用別名。這里演示將查詢(xún)book時(shí)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一對(duì)多查詢(xún)r(jià)esultMap只返回了一條記錄的文章就介紹到這了,更多相關(guān)mybatis一對(duì)多查詢(xún)r(jià)esultMap內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解析Mybatis對(duì)sql表的一對(duì)多查詢(xún)問(wèn)題
- 關(guān)于mybatis一對(duì)一查詢(xún)一對(duì)多查詢(xún)遇到的問(wèn)題
- MybatisPlus實(shí)現(xiàn)對(duì)象嵌套關(guān)聯(lián)查詢(xún)一對(duì)多List集合查詢(xún)
- springboot使用mybatis一對(duì)多的關(guān)聯(lián)查詢(xún)問(wèn)題記錄
- 在MyBatis中實(shí)現(xiàn)一對(duì)多查詢(xún)和多對(duì)一查詢(xún)的方式詳解(各兩種方式)
- 基于mybatis一對(duì)多查詢(xún)內(nèi)層排序的問(wèn)題
- Mybatis一對(duì)多和多對(duì)一處理的深入講解
- springboot整合mybatis實(shí)現(xiàn)簡(jiǎn)單的一對(duì)多級(jí)聯(lián)查詢(xún)功能
- MyBatis圖文并茂講解注解開(kāi)發(fā)一對(duì)多查詢(xún)
相關(guān)文章
Spring手動(dòng)生成web.xml配置文件過(guò)程詳解
這篇文章主要介紹了Spring手動(dòng)生成web.xml配置文件過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
RocketMQ?Broker消息如何刷盤(pán)源碼解析
這篇文章主要為大家介紹了RocketMQ?Broker消息如何刷盤(pán)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
使用Springboot實(shí)現(xiàn)OAuth服務(wù)的示例詳解
OAuth(Open Authorization)是一個(gè)開(kāi)放標(biāo)準(zhǔn),用于授權(quán)第三方應(yīng)用程序訪問(wèn)用戶(hù)資源,而不需要共享用戶(hù)憑證。本文主要介紹了如何使用Springboot實(shí)現(xiàn)一個(gè)OAuth服務(wù),需要的可以參考一下2023-05-05
Spring?Native打包本地鏡像的操作方法(無(wú)需通過(guò)Graal的maven插件buildtools)
這篇文章主要介紹了Spring?Native打包本地鏡像,無(wú)需通過(guò)Graal的maven插件buildtools,本文探索一下,如果不通過(guò)這個(gè)插件來(lái)生成鏡像,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
SpringBoot響應(yīng)處理實(shí)現(xiàn)流程詳解
這篇文章主要介紹了SpringBoot響應(yīng)處理實(shí)現(xiàn)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
java使用mybatis調(diào)用存儲(chǔ)過(guò)程返回一個(gè)游標(biāo)結(jié)果集方式
這篇文章主要介紹了java使用mybatis調(diào)用存儲(chǔ)過(guò)程返回一個(gè)游標(biāo)結(jié)果集方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

