mybatis 一對(duì)一、一對(duì)多和多對(duì)多查詢(xún)實(shí)例代碼
關(guān)鍵字:association 一對(duì)一映射(一個(gè)班級(jí)只有一個(gè)班主任)
<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>
關(guān)鍵字:collection 一對(duì)多映射(一個(gè)老師有多個(gè)學(xué)生)
<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>
<!-- 查詢(xún)所有的老師級(jí)各自的所有學(xué)生 -->
<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>
關(guān)鍵字:association 多對(duì)一映射(多個(gè)人屬于一個(gè)國(guó)家)
多對(duì)一相當(dāng)于一對(duì)多,也可以使用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>
總結(jié)
以上所述是小編給大家介紹的mybatis 一對(duì)一、一對(duì)多和多對(duì)多查詢(xún),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Mybatis中的高級(jí)映射一對(duì)一、一對(duì)多、多對(duì)多
- 解決mybatis plus 一對(duì)多分頁(yè)查詢(xún)問(wèn)題
- MybatisPlus實(shí)現(xiàn)對(duì)象嵌套關(guān)聯(lián)查詢(xún)一對(duì)多List集合查詢(xún)
- MyBatisPlus 一對(duì)多、多對(duì)一、多對(duì)多的完美解決方案
- MyBatis如何實(shí)現(xiàn)多表查詢(xún)(多對(duì)一、一對(duì)多)
- 解決mybatis一對(duì)多關(guān)聯(lián)查詢(xún)多條數(shù)據(jù)只顯示一條的問(wèn)題
- Mybatis一對(duì)多與多對(duì)一查詢(xún)處理詳解
- mybatis一對(duì)多查詢(xún)功能
- MyBatis一對(duì)多關(guān)系映射方式小結(jié)
相關(guān)文章
SpringBoot如何優(yōu)雅地使用Swagger2
這篇文章主要介紹了SpringBoot如何優(yōu)雅地使用Swagger2,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
SpringBoot實(shí)現(xiàn)事務(wù)鉤子函數(shù)的示例
本文主要介紹了SpringBoot實(shí)現(xiàn)事務(wù)鉤子函數(shù)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08
java實(shí)現(xiàn)flappy Bird小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)flappy Bird小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

