MyBatis3.X復(fù)雜Sql查詢(xún)的語(yǔ)句
MyBatis3.X復(fù)雜Sql查詢(xún)
MyBatis3.X的resultMap
1.Mybatis的sql語(yǔ)句返回的結(jié)果有兩種
- resultType
- 查詢(xún)出的字段在相應(yīng)的pojo中必須有和它相同的字段對(duì)應(yīng),或者基本數(shù)據(jù)類(lèi)型
- 適合簡(jiǎn)單查詢(xún)
- resultMap
需要自定義字段,或者多表查詢(xún),一對(duì)多等關(guān)系,比resultType更強(qiáng)大
適合復(fù)雜查詢(xún)
<resultMap id="VideoResultMap" type="Video"> <!-- id 指定查詢(xún)列列的唯⼀一標(biāo)示 column 數(shù)據(jù)庫(kù)字段的名稱(chēng) property pojo類(lèi)的名稱(chēng) --> <id column="id" property="id" jdbcType="INTEGER" /> <result column="video_tile" property="title" jdbcType="VARCHAR" /> <result column="summary" property="summary" jdbcType="VARCHAR" /> <result column="cover_img" property="coverImg" jdbcType="VARCHAR" /> </resultMap> <select id="selectBaseFieldByIdWithResultMap" resultMap="VideoResultMap"> select id , title as video_tile, summary, cover_img from video where id = #{video_id} </select>
ResultMap復(fù)雜對(duì)象一對(duì)一查詢(xún)結(jié)果映射之a(chǎn)ssociation
association:映射到POJO的某個(gè)復(fù)雜類(lèi)型屬性,比如訂單order對(duì)象里面包含user對(duì)象
<!-- 名稱(chēng)空間,需要保存全局唯一,最好是和dao層的Java接口一致 可以映射sql語(yǔ)句到對(duì)應(yīng)的方法名參數(shù)和返回值 mybatis是使用接口動(dòng)態(tài)代理 --> <mapper namespace="net.xiaotiancai.online_class.dao.VideoOrderMapper"> <resultMap id="VideoOrderResultMap" type="VideoOrder"> <id column="id" property="id"></id> <result column="user_id" property="userId"></result> <result column="out_trade_no" property="outTradeNo"></result> <result column="state" property="state"></result> <result column="total_fee" property="totalFee"></result> <result column="video_id" property="videoId"></result> <result column="video_title" property="videoTitle"></result> <!-- 配置屬性一對(duì)一 property對(duì)應(yīng)videoOrder里面的User javaType對(duì)應(yīng)這個(gè)屬性的類(lèi)型 --> <association property="user" javaType="User"> <id column="user_id" property="id"></id> <result column="name" property="name"></result> <result column="head_img" property="headImg"></result> <result column="phone" property="phone"></result> </association> </resultMap> <!--一對(duì)一訂單查詢(xún),訂單內(nèi)部包含用戶(hù)屬性--> <select id="queryVideoOrderList" resultMap="VideoOrderResultMap"> select o.id id, o.user_id, o.out_trade_no, o.state, o.total_fee, o.video_id, o.video_title, u.name, u.head_img, u.phone from video_order o left join user u on o.user_id = u.id </select> </mapper>
代碼
// resultmap association關(guān)聯(lián)查詢(xún) VideoOrderMapper videoOrderMapper =sqlSession.getMapper(VideoOrderMapper.class); List<VideoOrder> videoOrderList = videoOrderMapper.queryVideoOrderList(); System.out.println(videoOrderList.toString());
ResultMap復(fù)雜對(duì)象一對(duì)多查詢(xún)結(jié)果映射之collection
collection: 一對(duì)多查詢(xún)結(jié)果查詢(xún)映射,比如user有多個(gè)訂單
<resultMap id="UserOrderResultMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="headImg" column="head_img"/> <result property="phone" column="phone"/> <!-- property 填寫(xiě)pojo類(lèi)中集合類(lèi)屬性的名稱(chēng) ofType 集合⾥里里⾯面的pojo對(duì)象 --> <collection property="videoOrderList" ofType="VideoOrder"> <!--配置主鍵,管理理order的唯⼀一標(biāo)識(shí)--> <id column="order_id" property="id"/> <result column="user_id" property="userId"/> <result column="out_trade_no" property="outTradeNo"/> <result column="state" property="state"/> <result column="total_fee" property="totalFee"/> <result column="video_id" property="videoId"/> <result column="video_title" property="videoTitle"/> <result column="video_img" property="videoImg"/> </collection> </resultMap> <select id="queryUserOrder" resultMap="UserOrderResultMap"> select u.id, u.name, u.head_img, u.phone, o.id order_id, o.out_trade_no, o.user_id, o.state, o.total_fee, o.video_id, o.video_title from user u left join video_order o on u.id = o.user_id </select>
代碼
// resultmap association關(guān)聯(lián)查詢(xún) VideoOrderMapper videoOrderMapper =sqlSession.getMapper(VideoOrderMapper.class); //resultmap collection測(cè)試 List<User> userList = videoOrderMapper.queryUserOrder(); System.out.println(userList.toString());
Mybatis3.X ResultMap復(fù)雜對(duì)象查詢(xún)總結(jié)
總結(jié)ResultMap的復(fù)雜對(duì)象查詢(xún)
- association映射的是一個(gè)pojo類(lèi),處理一對(duì)一的關(guān)聯(lián)關(guān)系。
- collection映射的一個(gè)集合列表,處理的是一對(duì)多的關(guān)聯(lián)關(guān)系。
- 模板
<!--column不做限制,可以為任意表的字段,而property須為type定義的pojo屬性--> <resultMap id=“唯一的標(biāo)識(shí)”type=“映射的pojo對(duì)象"> <id column="表的主鍵字段,或查詢(xún)語(yǔ)句中的別名字段”jdbcrype="字段類(lèi)型”property=“映射pojo對(duì)象的主鍵屬性"/> <result column="表的一個(gè)字段”jdbcrype="字段類(lèi)型”property=“映射到pojo對(duì)象的一個(gè)屬性"/> <association property="pojo的一個(gè)對(duì)象屬性”javarype="pojo關(guān)聯(lián)的pojo對(duì)象"> <id column="關(guān)聯(lián)pojo對(duì)象對(duì)應(yīng)表的主鍵字段”jdbcrype="字段類(lèi)型”property="關(guān)聯(lián)pojo對(duì)象的屬性"/> <result column="表的字段”jdbcrype="字段類(lèi)型”property="關(guān)聯(lián)pojo對(duì)象的屬性"/> </association> <!--集合中的property 需要為oftype定義的pojo對(duì)象的屬性--> <collection property="pojo的集合屬性名稱(chēng)”ofType="集合中單個(gè)的pojo對(duì)象類(lèi)型"> <id column="集合中pojo對(duì)象對(duì)應(yīng)在表的主鍵字段”jdbcrype="字段類(lèi)型”property=“集合中pojo對(duì)象的主鍵屬性”/> <result column="任意表的字段”jdbcrype="字段類(lèi)型”property="集合中的pojo對(duì)象的屬性”/> </collection> </resultMap>
到此這篇關(guān)于MyBatis3.X復(fù)雜Sql查詢(xún)的文章就介紹到這了,更多相關(guān)MyBatis復(fù)雜Sql查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot快速搭建ftpserver服務(wù)端的詳細(xì)步驟
基于springboot,使用ftpserver快速搭建一個(gè)FTP服務(wù)端,搭建過(guò)程很簡(jiǎn)單,我們把過(guò)程分成4個(gè)步驟,一分鐘內(nèi)快速完成構(gòu)建,感興趣的朋友跟隨小編一起看看吧2023-11-11詳解SpringMVC中的四種跳轉(zhuǎn)方式、視圖解析器問(wèn)題
這篇文章主要介紹了SpringMVC的四種跳轉(zhuǎn)方式、視圖解析器,springmvc核心配置文件和視圖解析器的使用,添加視圖解析器,通過(guò)案例講解四種跳轉(zhuǎn)方式,需要的朋友可以參考下2022-10-10新建Maven工程出現(xiàn)Process?Terminated的問(wèn)題解決
當(dāng)Maven出現(xiàn)"Process terminated"錯(cuò)誤時(shí),這通常是由于配置文件或路徑錯(cuò)誤導(dǎo)致的,本文主要介紹了新建Maven工程出現(xiàn)Process?Terminated的問(wèn)題解決,感興趣的可以了解一下2024-04-04SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢(xún)功能
這篇文章主要介紹了SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢(xún)功能,pringBoot分頁(yè)查詢(xún)的兩種寫(xiě)法,一種是手動(dòng)實(shí)現(xiàn),另一種是使用框架實(shí)現(xiàn),現(xiàn)在我將具體的實(shí)現(xiàn)流程分享一下,需要的朋友可以參考下2023-11-11JDBC中使用Java8的日期LocalDate和LocalDateTime操作mysql、postgresql
這篇文章主要給大家介紹了關(guān)于JDBC中如何使用Java8的日期LocalDate和LocalDateTime的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09JVM內(nèi)存結(jié)構(gòu):程序計(jì)數(shù)器、虛擬機(jī)棧、本地方法棧
JVM 基本上是每家招聘公司都會(huì)問(wèn)到的問(wèn)題,它們會(huì)這么無(wú)聊問(wèn)這些不切實(shí)際的問(wèn)題嗎?很顯然不是。由 JVM 引發(fā)的故障問(wèn)題,無(wú)論在我們開(kāi)發(fā)過(guò)程中還是生產(chǎn)環(huán)境下都是非常常見(jiàn)的2021-06-06