




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第mybatis使用collection嵌套查詢的實(shí)現(xiàn)在開發(fā)中,可能會遇到一對多的關(guān)系,這個時候,一條sql語句就難以勝任這個任務(wù)了。只能先執(zhí)行一條sql,然后根據(jù)返回的結(jié)果,再做一次sql關(guān)聯(lián)查詢,這個時候,使用mybatis的collection就可以實(shí)現(xiàn)。
如果第一次查詢返回的是一個list集合,那么,后續(xù)的查詢就是一個for循環(huán)。所以不使用collection的做法,在java語言中,就要分兩次查詢。一般而言,我們的列表查詢都是分頁查詢,所以集合數(shù)據(jù)不會太大,第二次for循環(huán)查詢效率還好。
下面介紹mybatis使用collection嵌套查詢解決這個問題。這里為了簡單,以員工與部門的關(guān)系來做這個實(shí)驗,其實(shí)員工與部門的關(guān)系還應(yīng)該使用一個中間表來關(guān)聯(lián),這里只用兩張表。
表結(jié)構(gòu)與數(shù)據(jù)如下所示:
數(shù)據(jù):
這里采用maven構(gòu)建springboot+mybatis-plus+mysql工程。
dependency
groupIdcom.baomidou/groupId
artifactIdmybatis-plus-boot-starter/artifactId
version3.2.0/version
/dependency
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
/dependency
dependency
groupIdjectlombok/groupId
artifactIdlombok/artifactId
/dependency
定義的實(shí)體:
員工信息:
packagecom.xxx.springboot.mybatis.domain;
importjava.util.List;
importcom.baomidou.mybatisplus.annotation.TableName;
importlombok.Data;
@Data
@TableName("xx_emp")
publicclassEmployee{
privateIntegerid;
privateStringname;
privateintage;
privateListDepartmentdepts;
}
部門信息
packagecom.xxx.springboot.mybatis.domain;
importcom.baomidou.mybatisplus.annotation.TableName;
importlombok.Data;
@Data
@TableName("xx_dept")
publicclassDepartment{
privateIntegerid;
privateStringname;
privateIntegeruserId;
}
Mapper接口
packagecom.xxx.springboot.mybatis.mapper;
importorg.apache.ibatis.annotations.Mapper;
importcom.baomidou.mybatisplus.core.mapper.BaseMapper;
importcom.xxx.springboot.mybatis.domain.Department;
importcom.xxx.springboot.mybatis.domain.Employee;
@Mapper
publicinterfaceEmpMapperextendsBaseMapperEmployee{
EmployeequeryByName(Stringname);
DepartmentqueryByUserId(IntegeruserId);
}
EmpMapper.xml配置文件
xmlversion="1.0"encoding="UTF-8"
!DOCTYPEmapperPUBLIC"-////DTDMapper3.0//EN""/dtd/mybatis-3-mapper.dtd"
mappernamespace="com.xxx.springboot.mybatis.mapper.EmpMapper"
resultMapid="empMap"type="com.xxx.springboot.mybatis.domain.Employee"
idcolumn="id"property="id"/
resultcolumn="name"property="name"/
resultcolumn="age"property="age"/
collectionproperty="depts"javaType="java.util.ArrayList"ofType="com.xxx.springboot.mybatis.domain.Department"
select="com.xxx.springboot.mybatis.mapper.EmpMapper.queryByUserId"column="{userId=id}"/collection
/resultMap
resultMapid="deptMap"type="com.xxx.springboot.mybatis.domain.Department"
idcolumn="id"property="id"/
resultcolumn="name"property="name"/
resultcolumn="user_id"property="userId"/
/resultMap
selectid="queryByName"resultMap="empMap"
SELECT
FROMxx_emp
WHEREname=#{name}
/select
selectid="queryByUserId"resultMap="deptMap"
SELECT
FROMxx_dept
WHEREuser_id=#{userId}
/select
/mapper
測試類:
packagecom.xxx.springboot;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.test.context.SpringBootTest;
importorg.springframework.test.context.junit4.SpringRunner;
importcom.baomidou.mybatisplus.core.toolkit.Wrappers;
importcom.xxx.springboot.mybatis.domain.Employee;
importcom.xxx.springboot.mybatis.mapper.EmpMapper;
@R
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 私自購房合同范本
- 丁橋租房合同范本
- 擺攤工具租賃合同范本
- 關(guān)于logo設(shè)計合同范本
- 裝飾裝修維修合同范本
- 回遷樓回購合同范本
- 房租買賣簡易合同范本
- 高空安裝合同范本
- 購房合同范本哪里看
- 消防栓安全知識培訓(xùn)課件
- 2025重慶對外建設(shè)集團(tuán)招聘41人筆試參考題庫附答案解析
- 高警示藥品風(fēng)險管理
- 精選芭蕾舞男女演員之間的潛規(guī)則匯總
- 慢阻肺隨訪記錄表格模板
- J-STD-020D[1].1中文版
- SF∕T 0124-2021 錄像過程分析技術(shù)規(guī)范
- 四講業(yè)主業(yè)主大會業(yè)主委員會PPT課件
- 永磁渦流傳動器的應(yīng)用示范及產(chǎn)業(yè)化20150706
- 被執(zhí)行人財產(chǎn)申報表
- 復(fù)合活性羥基磷灰石陶瓷的研制及其生物相容性研究
- 《放射物理與防護(hù)》第四章
評論
0/150
提交評論