MyBatis的collection和association的使用解讀
寫在前面
MyBatis涉及到多表關(guān)聯(lián)查詢的時(shí)候,有一個(gè)非常實(shí)用的工具,可以無(wú)縫封裝object、array,將結(jié)果返回指定格式的json數(shù)據(jù)。
在這里提供手把手教學(xué),一起看collection和association如何使用。
表結(jié)構(gòu)及數(shù)據(jù)準(zhǔn)備
CREATE TABLE `test_school` ( `id` varchar(20) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test_class` ( `id` varchar(20) NOT NULL, `name` varchar(255) DEFAULT NULL, `school_id` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test_teacher` ( `id` varchar(20) NOT NULL, `name` varchar(255) DEFAULT NULL, `class_id` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
數(shù)據(jù)與關(guān)系
分別在三張表中簡(jiǎn)單的創(chuàng)建了幾條數(shù)據(jù),來(lái)簡(jiǎn)單的表示數(shù)據(jù)之間的關(guān)系。
我們可以看出,school與class的數(shù)據(jù)之間是一對(duì)多的關(guān)系;class與teacher之間的關(guān)系是一對(duì)一的關(guān)系。
如果只是單純的使用sql關(guān)聯(lián)查詢時(shí),會(huì)查出來(lái)三條數(shù)據(jù),其中school的數(shù)據(jù)是重復(fù)的,因?yàn)閘eft join嘛。
關(guān)鍵:MyBatis的xml
SchoolMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.boot.security.server.dao.SchoolDao"> <resultMap id="SchoolMap" type="com.boot.security.server.model.TestSchool"> <id column="id" property="id" /> <result column="name" property="name" /> <collection property="testClasses" columnPrefix="tc_" resultMap="com.boot.security.server.dao.ClassDao.ClassMap"/> </resultMap> <select id="list" resultMap="SchoolMap"> select ts.id,ts.name, tc.id AS tc_id, tc.name AS tc_name, tc.school_id AS tc_school_id, tt.id AS tc_tt_id, tt.name AS tc_tt_name, tt.class_id AS tc_tt_class_id from test_school ts left join test_class tc on ts.id=tc.school_id left join test_teacher tt on tc.id=tt.class_id </select> </mapper>
ClassMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.boot.security.server.dao.ClassDao"> <resultMap id="ClassMap" type="com.boot.security.server.model.TestClass"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="school_id" property="schoolId" /> <association property="testTeacher" columnPrefix="tt_" resultMap="com.boot.security.server.dao.TeacherDao.TeacherMap"/> </resultMap> </mapper>
TeacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.boot.security.server.dao.TeacherDao"> <resultMap id="TeacherMap" type="com.boot.security.server.model.TestTeacher"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="class_id" property="classId" /> </resultMap> </mapper>
注意!SchoolMapper.xml中的collection與ClassMapper.xml中的association!
注意!注意查詢的sql起的別名,與collection和association中的columnPrefix屬性需要對(duì)應(yīng)
其中service、controller這里省略了,我們直接調(diào)用SchoolMapper.xml中的list方法,查看一下執(zhí)行結(jié)果!
執(zhí)行結(jié)果
我們可以看到,class被封裝成了一個(gè)數(shù)組(因?yàn)槲覀冊(cè)赟choolMapper.xml中用collection封裝);
teacher被封裝成一個(gè)對(duì)象(因?yàn)槲覀冊(cè)贑lassMapper.xml中用association封裝)。
總結(jié)
MyBatis提供了非常強(qiáng)大的對(duì)象、數(shù)組封裝方式,直接將查詢出來(lái)的結(jié)果封裝成對(duì)應(yīng)的格式。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Mybatis中collection和association的使用區(qū)別詳解
- mybatis利用association或collection傳遞多參數(shù)子查詢
- Mybatis之a(chǎn)ssociation和collection用法
- 在Mybatis中association標(biāo)簽多層嵌套的問(wèn)題
- mybatis中一對(duì)一關(guān)系association標(biāo)簽的使用
- MyBatis中association的基本使用方法
- mybatis的association傳遞參數(shù)問(wèn)題示例
- Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用
- mybatis中association標(biāo)簽的使用解讀
- MyBatis使用嵌套查詢collection和association的實(shí)現(xiàn)
- Mybatis的association使用子查詢結(jié)果錯(cuò)誤的問(wèn)題解決
相關(guān)文章
Java將Word文檔轉(zhuǎn)換為PDF文件的幾種常用方法總結(jié)
這篇文章主要介紹了Java將Word文檔轉(zhuǎn)換為PDF文件的四種常用方法,分別使用ApachePOI+iText、Aspose.Words?for?Java、Docx4j和JODConverter,這些庫(kù)各有優(yōu)點(diǎn),但在使用時(shí)需要注意庫(kù)與Java環(huán)境的兼容性、安裝所需依賴、轉(zhuǎn)換速度和資源消耗,需要的朋友可以參考下2024-10-10SpringBoot集成RabbitMQ的方法(死信隊(duì)列)
這篇文章主要介紹了SpringBoot集成RabbitMQ的方法(死信隊(duì)列),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05HashMap原理及手寫實(shí)現(xiàn)部分區(qū)塊鏈特征
這篇文章主要為大家介紹了HashMap原理及手寫實(shí)現(xiàn)部分區(qū)塊鏈特征,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09