詳解mybatis多對一關(guān)聯(lián)查詢的方式
根據(jù)ID查詢學(xué)生信息,要求該學(xué)生的教師和班級信息一并查出
第一種關(guān)聯(lián)方式
1.修改實體類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.修改實體類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é)一下:個人感覺第二種方法是會簡單許多,只是說,因為查詢語句中,連接條件的限制,當(dāng)cid或者tid為空時,查詢到的數(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.實體類的屬性名要和表字段名保存一致,或按照駝峰命名規(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)文章
springboot集成nacos無法動態(tài)獲取nacos配置的問題
這篇文章主要介紹了springboot集成nacos無法動態(tài)獲取nacos配置的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Java中的Unsafe在安全領(lǐng)域的使用總結(jié)和復(fù)現(xiàn)(實例詳解)
unsafe里面有很多好用的方法,比如allocateInstance可以直接創(chuàng)建實例對象,defineAnonymousClass可以創(chuàng)建一個VM匿名類(VM?Anonymous?Class),以及直接從內(nèi)存級別修改對象的值。這篇文章主要介紹了Java中的Unsafe在安全領(lǐng)域的一些應(yīng)用總結(jié)和復(fù)現(xiàn),需要的朋友可以參考下2022-03-03
Java 讀取網(wǎng)絡(luò)圖片存儲到本地并生成縮略圖
用Java做開發(fā)經(jīng)常需要處理圖片。本文就來看一下如何保存圖片到本地并生成縮略圖2021-05-05

