MyBatis多表連接查詢的實(shí)例教程
多表連接的兩種方式(數(shù)據(jù)庫(kù)邏輯模型):
1.一對(duì)一關(guān)系
2.一對(duì)多關(guān)系
一、通過(guò) resultMap 和 association 實(shí)現(xiàn)一對(duì)一關(guān)系
在 mapper.xml 文件里面的代碼:
<resultMap type="com.pojo.TRecruitment" id="tRecruitmentCollegeResultMap"> <id property="id" column="id" /> <result property="title" column="title" /> <result property="litimg" column="litimg" /> <result property="publishedTime" column="published_time" /> <result property="author" column="author" /> <result property="collegeId" column="college_id" /> <result property="type" column="type" /> <result property="details" column="details" /> <!-- association :配置一對(duì)一屬性 --> <!-- property:實(shí)體類中里面的 TCollege 屬性名 --> <!-- javaType:屬性類型 --> <association property="tCollege" javaType="com.pojo.TCollege" > <!-- id:聲明主鍵,表示 college_id 是關(guān)聯(lián)查詢對(duì)象的唯一標(biāo)識(shí)--> <id property="collegeId" column="college_id" /> <result property="collegeName" column="college_name" /> <result property="collegeImg" column="college_img" /> </association> </resultMap> <!-- 一對(duì)一關(guān)聯(lián),查詢訂單,訂單內(nèi)部包含用戶屬性 --> <select id="querytTRecruitmentResultMap" resultMap="tRecruitmentCollegeResultMap"> SELECT r.id, r.title, r.litimg, r.published_time, r.author, r.type, r.details, c.college_name FROM `t_recruitment` r LEFT JOIN `t_college` c ON r.college_id = c.college_id </select>
在 mapper.java 文件里面寫接口:
List<TRecruitment> querytTRecruitmentResultMap();
在對(duì)應(yīng)的實(shí)體類中聲明另外一個(gè)實(shí)體類:
二、通過(guò) resultMap 和 collection 實(shí)現(xiàn)一對(duì)多關(guān)系
xml 文件:
<!-- 一個(gè)用戶,擁有多個(gè)訂單 --> <resultMap type="User" id="UserAndOrdersResultMap"> <!-- 先配置 User 的屬性 --> <id column="id" property="id" /> <result column="username" property="username" /> <result column="birthday" property="birthday" /> <result column="sex" property="sex" /> <result column="address" property="address" /> <!-- 再配置 Orders 集合 --> <collection property="ordersList" ofType="Orders"> <id column="oid" property="id" /> <result column="user_id" property="userId" /> <result column="number" property="number" /> <result column="createtime" property="createtime" /> </collection> </resultMap> <select id="findUserAndOrders" resultMap="UserAndOrdersResultMap"> SELECT u.*, o.`id` oid, o.`number`, o.`createtime` FROM USER u, orders o WHERE u.`id` = o.`user_id`; </select>
總結(jié)
到此這篇關(guān)于MyBatis多表連接查詢的文章就介紹到這了,更多相關(guān)MyBatis多表連接查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java mybatis框架實(shí)現(xiàn)多表關(guān)系查詢功能
- Java MyBatis 多表查詢?cè)斀?/a>
- MyBatis如何實(shí)現(xiàn)多表查詢(多對(duì)一、一對(duì)多)
- Mybatis注解開發(fā)單表、多表操作的實(shí)現(xiàn)代碼
- java開發(fā)MyBatis中常用plus實(shí)體類注解符詳解
- 基于Spring整合mybatis注解掃描是否成功的問(wèn)題
- springboot整合mybatis-plus基于注解實(shí)現(xiàn)一對(duì)一(一對(duì)多)查詢功能
- Java Mybatis框架多表操作與注解開發(fā)詳解分析
相關(guān)文章
Mybatis傳單個(gè)參數(shù)和<if>標(biāo)簽同時(shí)使用的問(wèn)題及解決方法
這篇文章主要介紹了Mybatis傳單個(gè)參數(shù)和<if>標(biāo)簽同時(shí)使用的問(wèn)題及解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05linux下idea、pycharm等輸入中文拼音時(shí)滿3個(gè)字母后無(wú)法繼續(xù)拼音輸入的問(wèn)題
這篇文章主要介紹了linux下idea、pycharm等輸入中文拼音時(shí)滿3個(gè)字母后無(wú)法繼續(xù)拼音輸入的問(wèn)題,本文通過(guò)圖文并茂的形式給大家分享解決方法,需要的朋友可以參考下2021-04-04RabbitMq消息防丟失功能實(shí)現(xiàn)方式講解
這篇文章主要介紹了RabbitMq消息防丟失功能實(shí)現(xiàn),RabbitMQ中,消息丟失可以簡(jiǎn)單的分為兩種:客戶端丟失和服務(wù)端丟失。針對(duì)這兩種消息丟失,RabbitMQ都給出了相應(yīng)的解決方案2023-01-01SpringBoot項(xiàng)目執(zhí)行腳本 自動(dòng)拉取最新代碼并重啟的實(shí)例內(nèi)容
在本篇文章里小編給大家整理的是一篇關(guān)于SpringBoot項(xiàng)目執(zhí)行腳本 自動(dòng)拉取最新代碼并重啟的實(shí)例內(nèi)容,有需要的朋友們參考下。2019-12-12Spring Cloud使用Feign實(shí)現(xiàn)Form表單提交的示例
本篇文章主要介紹了Spring Cloud使用Feign實(shí)現(xiàn)Form表單提交的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03如何解決IDEA.properties文件中文亂碼問(wèn)題
這篇文章主要介紹了如何解決IDEA.properties文件中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12