詳解mybatis多對一關(guān)聯(lián)查詢的方式
根據(jù)ID查詢學生信息,要求該學生的教師和班級信息一并查出
第一種關(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é)一下:個人感覺第二種方法是會簡單許多,只是說,因為查詢語句中,連接條件的限制,當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)文章
Java中MessageDigest來實現(xiàn)數(shù)據(jù)加密的方法
這篇文章主要介紹了Java中MessageDigest來實現(xiàn)數(shù)據(jù)加密的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05Maven和IntelliJ IDEA搭建多模塊微服務(wù)的實現(xiàn)
本文主要介紹了Maven和IntelliJ IDEA搭建多模塊微服務(wù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-05-05