




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
主講人:吳佳云延遲符關聯(lián)映射1:n動態(tài)SQL與關聯(lián)映射關聯(lián)映射1:n關聯(lián)映射1:n指一對多的關系,這種關系很常見,例如班級與學生、部門與員工等的關系就是常見的一對多關系。一對多關聯(lián)映射主要使用<collection…./>來實現(xiàn)映射配置,以下以班級和學生為例,講解如何配置一對多雙向關聯(lián),其物理模型如圖所示。關聯(lián)映射1:n(1)數(shù)據(jù)庫準備CREATETABLE`tb_student`(
`id`intNOTNULL,
`name`varchar(45)DEFAULTNULL,
`sex`varchar(45)DEFAULTNULL,
`age`intDEFAULTNULL,
`classId`intDEFAULTNULL,
PRIMARYKEY(`id`),
KEY`fk_classId_idx`(`classId`),
CONSTRAINT`fk_classId`FOREIGNKEY(`classId`)REFERENCES`tb_class`(`id`)
)ENGINE=InnoDBCREATETABLE`tb_class`(
`id`intNOTNULL,
`code`varchar(45)DEFAULTNULL,
`name`varchar(45)DEFAULTNULL,
PRIMARYKEY(`id`)
)ENGINE=InnoDB關聯(lián)映射1:n(2)實體類StuClass.javaStudent.java@Data
publicclassStuClass{
/**班級ID*/
privateintid;
/**班級名稱*/
privateStringname;
/**班級編號*/
privateStringcode;
}@Data
publicclassStudent{
/**ID*/
privateIntegerid;
/**班級ID*/
privateIntegerclassId;
/**姓名*/
privateStringname;
/**性別*/
privateStringsex;
/**年齡*/
privateIntegerage;
}StudentVo.java@Data
publicclassStudentVo{
/**ID*/
privateIntegerid;
/**姓名*/
privateStringname;
/**性別*/
privateStringsex;
/**年齡*/
privateIntegerage;
/**班級*/
privateStuClassstuClass;
}關聯(lián)映射1:n(3)創(chuàng)建XML映射文件StuClassMapper.xmlmybatis-config.xml<mappernamespace="com.my.ccit.mapper.StuClassMapper">
<resultMapid="stuClassVoMap"type="com.my.ccit.vo.StuClassVo">
<idproperty="id"column="id"/>
<resultproperty="cardId"column="cardId"/>
<resultproperty="code"column="code"/>
<resultproperty="name"column="name"/>
<!--一對多映射:collection-->
<collectionproperty="students"javaType="ArrayList"column="id"
ofType="com.my.ccit.entity.Student"
select="com.my.ccit.mapper.StudentMapper.getByClassId"
fetchType="lazy">
<idproperty="id"column="id"/>
<resultproperty="name"column="name"/>
<resultproperty="sex"column="sex"/>
<resultproperty="age"column="age"/>
</collection>
</resultMap>
<selectid="getById"parameterType="int"resultMap="stuClassVoMap">
select*fromtb_class
whereid=#{id}
</select>
</mapper><!--配置懶加載-->
<settings>
<!--打開延遲加載的開關-->
<settingname="lazyLoadingEnabled"value="true"/>
<!--將積極加載改為消息加載,即按需加載-->
<settingname="aggressiveLazyLoading"value="false"/>
</settings>關聯(lián)映射1:n(3)創(chuàng)建XML映射文件StudentMapper.xml<mappernamespace="com.my.ccit.mapper.StudentMapper">
<resultMapid="StudentVoMap"type="com.my.ccit.vo.StudentVo">
<idproperty="id"column="sid"/>
<resultproperty="name"column="sname"/>
<resultproperty="sex"column="ssex"/>
<resultproperty="age"column="sage"/>
<!--多對一映射:association-->
<associationproperty="stuClass"column="classId"
javaType="com.my.ccit.entity.StuClass">
<idproperty="id"column="cid"/>
<resultproperty="code"column="ccode"/>
<resultproperty="name"column="cname"/>
</association>
</resultMap>
<!--根據(jù)學生id查詢學生信息-->
<selectid="getById"parameterType="int"resultMap="StudentVoMap">
selectc.idascid,c.codeasccode,ascname,
s.idassid,assname,s.sexasssex,s.ageassage,s.classIdassclassId
fromtb_classc,tb_students
wherec.id=s.classId
ands.id=#{id}
</select>
<!--根據(jù)班級id查詢學生信息-->
<selectid="getByClassId"parameterType="int"resultType="com.my.ccit.entity.Student">
select*fromtb_student
whereclassId=#{classId}
</select>
</mapper>其中,getById的查詢語句是多表聯(lián)合查詢,查詢的結(jié)果通過<column…./>元素和<property…./>元素完成了數(shù)據(jù)字段到java對象的映射。<association…./>元素用于配置多對一關聯(lián)映射。關聯(lián)映射1:n(4)創(chuàng)建Mapper接口StuClassMapper.javaStudentMapper.java@Mapper
publicinterfaceStuClassMapper{
StuClassVogetById(Integerid);
}@Mapper
publicinterfaceStudentMapper{
StudentVogetById(Integerid);
List<Student>getByClassId(IntegerclassId);
}關聯(lián)映射1:n(5)編寫測試類1@Test
publicvoidtestGetStudentById(){
//1.通過工具類生成SqlSession對象
SqlSessionsession=MyBatisUtils.getSession();
StudentMapperstudentMapper=session.getMapper(StudentMapper.class);
//2.根據(jù)學生id查詢學生信息
System.out.println("根據(jù)學生id=1,查詢學生信息結(jié)果:");
StudentVostudentVo=studentMapper.getById(1);
System.out.println(studentVo);
//3.關閉SqlSession
session.close();
}根據(jù)學生id查詢學生信息關聯(lián)映射1:n(5)編寫測試類2根據(jù)班級id查詢信息@Test
publicvoidtestGetClassById(){
//1.通過工具類生成SqlSession對象
SqlSessionsession=MyBa
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 個人租房押付三合同
- 廣告設計制作合同協(xié)議書
- 客車駕駛員聘用協(xié)議書
- 分期付款設備買賣合同
- 物資倉庫裝修施工方案
- 下部結(jié)構施工方案
- 宿遷住宅防水施工方案
- 安徽省部分學校2024-2025學年高三下學期3月調(diào)研考試歷史試題(原卷版+解析版)
- 暖氣片施工方案
- 泡沫箱和紙箱加工生產(chǎn)線環(huán)評報告表
- 名人介紹l梁啟超
- 幼兒繪本故事:波西和皮普大怪獸
- 譯林版五年級英語下冊 Unit 5 第2課時 教學課件PPT小學公開課
- 全套電子課件:混凝土結(jié)構設計
- 數(shù)據(jù)結(jié)構英文教學課件:chapter2 Array
- 新版PEP小學英語3-6年級單詞表(共14頁)
- 2022年城市軌道交通行車值班員三級考試題庫(附答案)
- 入門級新概念英語青少版A unit8
- 應用隨機過程PPT課件
- 鋁合金門窗檢測資料
- 腫瘤學總論ppt課件
評論
0/150
提交評論