詳解mybatis多對一關(guān)聯(lián)查詢的方式
根據(jù)ID查詢學(xué)生信息,要求該學(xué)生的教師和班級信息一并查出
第一種關(guān)聯(lián)方式
1.修改實(shí)體類Student,追加關(guān)聯(lián)屬性,用于封裝關(guān)聯(lián)的數(shù)據(jù)
修改完以后重新生成get set方法還有toString方法
private Teacher teacher; private Classes classes;
2.修改TeacherMapper相關(guān)配置
1.接口類 增加
Teacher selectTeacherById(Integer tid);
2.xml映射文件 增加
<sql id="params">tid,tname</sql> <select id="selectTeacherById" resultType="Teacher"> select <include refid="params"></include> from teacher where tid=#{tid} </select>
3.修改ClassesMapper 相關(guān)配置
1.接口類 增加
Classes selectClassesById(Integer cid);
2.xml映射文件 增加
<resultMap type="Classes" id="clsMap"> <id property="cid" column="cid"></id> <result property="cname" column="cname"/> </resultMap> <select id="selectClassesById" resultMap="clsMap"> select * from classes where cid = #{cid} </select>
4.修改StudentMapper 相關(guān)配置
1.接口類 增加
Student selectStudentById(Integer sid);
2.xml映射文件 增加
ps:
多對一關(guān)聯(lián)屬性配置:
property:關(guān)聯(lián)對象名
javaType:關(guān)聯(lián)對象類型
select:引用的關(guān)聯(lián)查詢sql對應(yīng)id名
column:引用的關(guān)聯(lián)查詢sql語句需要的參數(shù)
<resultMap type="Student" id="stuMap"> <id property="sid" column="sid"/> <result property="sname" column="sname"/> <result property="age" column="age"/> <result property="email" column="email"/> <association property="teacher" select="com.yc.dao.TeacherMapper.selectTeacherById" column="tid" javaType="Teacher"></association> <association property="classes" select="com.yc.dao.ClassesMapper.selectClassesById" column="cid" javaType="Classes"></association> </resultMap> <select id="selectStudentById" resultMap="stuMap"> select * from student where sid = #{sid} </select>
5.測試代碼
@Test public void test1() { Student stu = studentMapper.selectStudentById(100001); System.out.println(stu); }
第二種配置方式
1.修改實(shí)體類Student,追加關(guān)聯(lián)屬性,用于封裝關(guān)聯(lián)的數(shù)據(jù)
跟前面一樣的
2.修改studentMapper.xml映射文件
ps:接口里面的方法還是跟原來一樣的
<resultMap type="Student" id="stuMap"> <id property="sid" column="sid" /> <result property="sname" column="sname" /> <result property="age" column="age" /> <result property="email" column="email" /> <association property="teacher" javaType="Teacher"> <id property="tid" column="tid"></id> <result property="tname" column="tname" /> </association> <association property="classes" javaType="Classes"> <id property="cid" column="cid"></id> <result property="cname" column="cname" /> </association> </resultMap> <select id="selectStudentById" resultMap="stuMap"> select * from student s,teacher t,classes c where s.sid=#{sid} and s.tid = t.tid and s.cid=c.cid </select>
小結(jié)一下:個(gè)人感覺第二種方法是會簡單許多,只是說,因?yàn)椴樵冋Z句中,連接條件的限制,當(dāng)cid或者tid為空時(shí),查詢到的數(shù)據(jù)就會是null,而第一種的話,如果只是cid為空,至少還是會顯示student和teacher的相關(guān)信息
第三種配置方式 全局映射
ps:student類和studentmapper接口類不變
1.修改全局配置文件的setting信息
<setting name="autoMappingBehavior" value="FULL" /> <setting name="mapUnderscoreToCamelCase" value="true" />
2. 修改studentMapper.xml映射文件
ps:
1.實(shí)體類的屬性名要和表字段名保存一致,或按照駝峰命名規(guī)則(屬性名:aColumn,字段名:A_COLUMN),settion屬性mapUnderscoreToCamelCase設(shè)置成true
2.關(guān)聯(lián)表的字段名不能有相同的名字
<resultMap type="Student" id="stuMap"> <association property="teacher" javaType="Teacher" /> <association property="classes" javaType="Classes" /> </resultMap> <select id="selectStudentById" resultMap="stuMap"> select * from student s,teacher t,classes c where s.sid = #{sid} and s.tid=t.tid and s.cid=c.cid </select>
還是連接查詢?yōu)榭盏膯栴}~
總結(jié)
到此這篇關(guān)于mybatis多對一關(guān)聯(lián)查詢的文章就介紹到這了,更多相關(guān)mybatis多對一關(guān)聯(lián)查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中MessageDigest來實(shí)現(xiàn)數(shù)據(jù)加密的方法
這篇文章主要介紹了Java中MessageDigest來實(shí)現(xiàn)數(shù)據(jù)加密的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Linux下Java Python啟動(dòng)管理腳本方便程序管理
這篇文章主要為大家介紹了Linux下Java/Python啟動(dòng)管理腳本方便程序管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11如何通過Java實(shí)現(xiàn)加密、解密Word文檔
這篇文章主要介紹了如何通過Java實(shí)現(xiàn)加密、解密Word文檔,對一些重要文檔,常需要對文件進(jìn)行加密,查看文件時(shí),需要正確輸入密碼才能打開文件。下面介紹了一種比較簡單的方法給Word文件加密以及如何給已加密的Word文件解除密碼保護(hù),需要的朋友可以參考下2019-07-07Spring自動(dòng)配置之condition條件判斷下篇
這篇文章主要為大家介紹了SpringBoot?condition條件判斷功能的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08基于Java SSM的健康管理小程序的實(shí)現(xiàn)
本篇文章主要為大家分享了基于SSM健康管理小程序的設(shè)計(jì)與實(shí)現(xiàn)。感興趣的小伙伴可以了解一下2021-11-11SpringBoot詳細(xì)講解視圖整合引擎thymeleaf
這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-06-06Java使用IO流實(shí)現(xiàn)音頻的剪切和拼接
這篇文章主要為大家詳細(xì)介紹了Java使用IO流實(shí)現(xiàn)音頻的剪切和拼接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Maven和IntelliJ IDEA搭建多模塊微服務(wù)的實(shí)現(xiàn)
本文主要介紹了Maven和IntelliJ IDEA搭建多模塊微服務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05