關(guān)鍵字:association 一對一映射(一個班級只有一個班主任)
1
2
3
4
5
6
7
8
9
10
11
|
<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 一對多映射(一個老師有多個學(xué)生)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<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> <!-- 查詢所有的老師級各自的所有學(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 多對一映射(多個人屬于一個國家)
多對一相當(dāng)于一對多,也可以使用collection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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 一對一、一對多和多對多查詢,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/weixin_38048680/article/details/80307466