gpt4 book ai didi

mybatis 一对一、一对多和多对多查询实例代码

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章mybatis 一对一、一对多和多对多查询实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

关键字: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 一对1、一对多和多对多查询,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。

原文链接:https://blog.csdn.net/weixin_38048680/article/details/80307466 。

最后此篇关于mybatis 一对一、一对多和多对多查询实例代码的文章就讲到这里了,如果你想了解更多关于mybatis 一对一、一对多和多对多查询实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com