mybatis 一對一、一對多和多對多查詢實例代碼
關鍵字:association 一對一映射(一個班級只有一個班主任)
<select id="getClass" parameterType="int" resultMap="ClassesResultMap"> select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id} </select> <resultMap type="com.lcb.user.Classes" id="ClassesResultMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="com.lcb.user.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> </resultMap>
關鍵字:collection 一對多映射(一個老師有多個學生)
<resultMap type="Teacher" id="teacherMaps"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="class_name" property="className"/> <collection property="students" ofType="Student" select="getStudents" column="id"> </collection> </resultMap> <!-- 查詢所有的老師級各自的所有學生 --> <select id="getAllTeacher" parameterType="Teacher" resultMap="teacherMaps"> SELECT t.id, t.NAME, t.class_name FROM teacher t </select> <select id="getStudents" parameterType="int" resultType="Student"> select s.id, s. NAME, s.class_name as className from student s where teacher_id = #{id} </select>
關鍵字:association 多對一映射(多個人屬于一個國家)
多對一相當于一對多,也可以使用collection
<select id="selectCountry" resultType="Country"> select cid,cname from country where cid=#{ooo} </select> <resultMap type="People" id="peopleMapper2"> <id column="pid" property="pid"/> <result column="pname" property="pname"/> <association property="country" javaType="Country" select="selectCountry" column="countryId" /> </resultMap> <select id="selectById2" resultMap="peopleMapper2"> select pid,pname,countryId from people where pid = #{xxx} </select>
總結
以上所述是小編給大家介紹的mybatis 一對一、一對多和多對多查詢,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
java 中的static關鍵字和final關鍵字的不同之處
java 中的static關鍵字和final關鍵字的不同之處,需要的朋友可以參考一下2013-03-03Java8特性之用Stream流代替For循環(huán)操作詳解
這篇文章主要介紹了Stream流代替For循環(huán)進行輸出,這樣可以使代碼更簡潔,希望對大家有所幫助。一起跟隨小編過來看看吧2021-09-09使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅動方法
這篇文章主要介紹了使用java web 在jsp文件及Class中連接MySQL和SQLserver的驅動方法的相關資料,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2016-10-10基于Springboot執(zhí)行多個定時任務并動態(tài)獲取定時任務信息
這篇文章主要為大家詳細介紹了基于Springboot執(zhí)行多個定時任務并動態(tài)獲取定時任務信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04SpringBoot創(chuàng)建WebService方法詳解
這篇文章主要介紹了SpringBoot如何創(chuàng)建WebService,文中有詳細的實現(xiàn)步驟以及示例代碼,對學習或工作有一定的幫助,需要的朋友跟著小編一起來學習吧2023-05-05springboot CommandLineRunner接口實現(xiàn)自動任務加載功能
這篇文章主要介紹了springboot CommandLineRunner接口實現(xiàn)自動任務加載功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05