关键字: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>
|
关键字:collection 一对多映射(一个老师有多个学生)
?
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>
<!-- 查询所有的老师级各自的所有学生 -->
<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
?
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>
|
总结
以上所述是小编给大家介绍的mybatis 一对一、一对多和多对多查询,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:https://blog.csdn.net/weixin_38048680/article/details/80307466
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
如何在 Linux 中查找映射到 VxVM 磁盘的 SAN LUN
2025-05-27 102 -
企业网站云服务器租赁:VPS主机、独立服务器与网络安全保障方案
2025-06-04 44 -
2025-05-27 92
-
2025-05-27 59
-
2025-06-04 53
热门评论