網站首頁 編程語言 正文
采用老師和學生的案例來講解,每個處理兩種方法
多對一
-
多個學生對應一個老師
-
如果對于學生這邊,就是一個多對一的現象,即從學生這邊關聯一個老師!
?兩種方法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qi.dao.StudentMapper">
<!--按照結果進行嵌套處理-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid=t.id
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
<!--=============================================================-->
<!--
1.查詢所有的學生信息
2.根據查詢出來的學生的tid,尋找對應的老師!(子查詢)
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--復雜的屬性我們需要單獨處理
對象:association
集合:collection
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid}
</select>
</mapper>
上面的sql語句難寫點,resultMap簡單
下面的sql語句簡單,是子查詢
一對多
-
一個老師擁有多個學生
-
如果對于老師這邊,就是一個一對多的現象,即從一個老師下面擁有一群學生(集合)!
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qi.dao.TeacherMapper">
<!--按結果嵌套查詢-->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid=t.id and t.id=#{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--復雜的屬性我們需要單獨處理 對象:association 集合:collection-->
<!--javaType="" 指定屬性的類型-->
<!--集合中的泛型信息,我們使用ofType獲取-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
<!--======================================================================-->
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from mybatis.teacher where id =#{tid}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from mybatis.student where tid =#{tid}
</select>
</mapper>
大家對照著去理解,有點基礎的應該都能看得懂,照貓畫虎就行!
原文鏈接:https://blog.csdn.net/m0_73944607/article/details/134764374
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-05-27 Python/R語言分別實現斐波那契數列的示例詳解_python
- 2022-07-19 Ribbon負載均衡深入探究
- 2022-03-14 YUV420SP to JPEG
- 2022-06-14 Golang使用ini庫讀取配置詳情_Golang
- 2023-07-15 監聽鼠標mouse事件冒泡處理
- 2022-02-20 千分位保留兩位小數,出現“toFixed() is not a function”的解決辦法
- 2023-01-11 Android開發中父組件調用子組件方法demo_Android
- 2022-11-30 詳解Golang中字符串的使用_Golang
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支