MyBatis如何實現(xiàn)多表查詢(多對一、一對多)
MyBatis實現(xiàn)多表查詢
一、多對一查詢
數(shù)據(jù)庫的準備

創(chuàng)建兩張表,一張老師表,一張學生表
將老師主鍵id關(guān)聯(lián)學生外鍵tid
創(chuàng)建sql的語句
create table teacher( id int primary key, teacher_name varchar(30) not null ) insert into teacher(id,teacher_name) values (1,'毛老師') create table student( id int primary key, student_name varchar(30) not null, tid int default null ) //建立主外鍵關(guān)聯(lián) alter table student add constraint teacher_student_id foreign key (tid) references teacher(id) insert into student values (1,'小明',1) insert into student values (2,'小毛',1) insert into student values (3,'小紅',1) insert into student values (4,'大黃',1) insert into student values (5,'超兒',1)
項目結(jié)構(gòu)

使用Lombok插件,創(chuàng)建實體類。
(提高整潔度,主要想toulan)
@Data
public class Student {
private int id;
private String name;
//學生需要關(guān)聯(lián)一個老師
private Teacher teacher;
}
@Data
public class Teacher {
private int id;
private String name;
}
1、嵌套查詢處理
編寫接口
public interface StudentMapper {
//查詢所有學生的信息以及對應(yīng)老師的信息
public List<Student> getStudent();
}
2. 編寫StudentMapper.xml的查詢語句(重點)
<mapper namespace="dao.StudentMapper">
<!-- 思路:
1. 查詢所有學生的信息
根據(jù)查詢出來的學生tid,尋找對應(yīng)的老師
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="pojo.Student">
<!-- 復雜的屬性需要單獨處理 是對象就使用association,是集合就使用collection-->
<!-- select 子查詢 -->
<result property="name" column="student_name"/>
<association property="teacher" column="tid" javaType="pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="pojo.Teacher">
select * from teacher where id=#{id}
</select>
測試類
@Test
public void getStudent(){
SqlSession sqlSession = Mybatisutil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
測試結(jié)果

2、聯(lián)合查詢處理
編寫接口
//按照結(jié)果嵌套查詢 public List<Student> getStudent2();
2. 編寫StudentMapper.xml的查詢語句(重點)
<!-- 按照結(jié)果嵌套處理-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.student_name sname,t.teacher_name tname
from student s,teacher t
where s.tid=t.id
</select>
<resultMap id="StudentTeacher2" type="pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="pojo.Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
編寫測試類
@Test
public void getStudent(){
SqlSession sqlSession = Mybatisutil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent2();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
測試結(jié)果

二、一對多查詢
更改實體類
@Data
public class Student {
private int id;
private String name;
private int tid;
}
@Data
public class Teacher {
private int id;
private String name;
//一個老師擁有多個學生
private List<Student> students;
}
1、嵌套查詢處理 編寫接口
Teacher getTeacher2(@Param("tid") int id);
由于字段不一致,要做映射
主要TeacherMapper.xml的查詢語句(重點)
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from teacher where id=#{tid}
</select>
<resultMap id="TeacherStudent2" type="pojo.Teacher">
<result property="name" column="teacher_name"/>
<collection property="students" javaType="ArrayList" ofType="pojo.Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="pojo.Student">
select * from student where tid=#{tid}
</select>
測試類
@Test
public void getTeacher(){
SqlSession sqlSession = Mybatisutil.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher2(1);
System.out.println(teacher);
sqlSession.close();
}
測試結(jié)果:

Teacher(id=0, name=毛老師, students=[Student(id=1, name=null, tid=1), Student(id=2, name=null, tid=1), Student(id=3, name=null, tid=1), Student(id=4, name=null, tid=1), Student(id=5, name=null, tid=1)])
2、聯(lián)合查詢處理
編寫接口
//獲取指定老師下的所有學生及老師的信息
Teacher getTeacher(@Param("tid") int id);
由于字段不一致,要做映射
主要TeacherMapper.xml的查詢語句(重點)
<!-- 按結(jié)果嵌套查詢-->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.student_name sname,t.teacher_name tname,t.id tid
from student s,teacher t
where s.tid=t.id and t.id=#{tid}
</select>
<resultMap id="TeacherStudent" type="pojo.Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!-- 復雜的屬性需要單獨處理 是對象就使用association,是集合就使用collection
javaType="" 指定的屬性類型
集合中的泛型信息,使用ofType獲取-->
<collection property="students" ofType="pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
測試類
@Test
public void getTeacher(){
SqlSession sqlSession = Mybatisutil.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
測試結(jié)果:

Teacher(id=1, name=毛老師, students=[Student(id=1, name=小明, tid=1), Student(id=2, name=小毛, tid=1), Student(id=3, name=小紅, tid=1), Student(id=4, name=大黃, tid=1), Student(id=5, name=超兒, tid=1)])
總結(jié):
本章就使用了簡單的兩張表聯(lián)合查詢,介紹簡單的使用,更復雜的多表聯(lián)合主要在編寫sql的時候難度大點,或者是嵌套查詢要更嚴謹點
官方文檔也給了詳細的非常復雜的多表查詢?nèi)缦拢?mybatis,這么復雜的看的我頭疼
<!-- 非常復雜的語句 -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio,
A.favourite_section as author_favourite_section,
P.id as post_id,
P.blog_id as post_blog_id,
P.author_id as post_author_id,
P.created_on as post_created_on,
P.section as post_section,
P.subject as post_subject,
P.draft as draft,
P.body as post_body,
C.id as comment_id,
C.post_id as comment_post_id,
C.name as comment_name,
C.comment as comment_text,
T.id as tag_id,
T.name as tag_name
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Post P on B.id = P.blog_id
left outer join Comment C on P.id = C.post_id
left outer join Post_Tag PT on PT.post_id = P.id
left outer join Tag T on PT.tag_id = T.id
where B.id = #{id}
</select>
在我們編寫的時候注意點:
- 不要忘記注冊Mapper.xml
- 在初學的時候盡量不要給實體類取別名,為了不要混淆,加深理解
- 實體類字段要和數(shù)據(jù)庫字段一致,如果不一致,那就要用result標簽做映射
- 復雜的屬性需要單獨處理,是對象就使用association,是集合就使用collection來映射
javaType="" 指定的屬性類型|
集合中的泛型信息,使用ofType獲取
多注意復雜屬性的嵌套使用
JavaType & ofType
- JavaType 用來指定實體類中屬性的類型
- ofType 用來指定映射到List或者集合中的實體類pojo類型,泛型中的約束類型
到此這篇關(guān)于MyBatis如何實現(xiàn)多表查詢(多對一、一對多)的文章就介紹到這了,更多相關(guān)MyBatis多表查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot多數(shù)據(jù)源配置方式以及報錯問題的解決
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置方式以及報錯問題的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot在容器中創(chuàng)建實例@Component和@bean有什么區(qū)別
這篇文章主要介紹了SpringBoot在容器中創(chuàng)建實例@Component和@bean有什么區(qū)別,在Spring Boot中,@Component注解和@Bean注解都可以用于創(chuàng)建bean。它們的主要區(qū)別在于它們的作用范圍和創(chuàng)建方式2023-03-03
淺試仿?mapstruct實現(xiàn)微服務(wù)編排框架詳解
這篇文章主要為大家介紹了淺試仿?mapstruct實現(xiàn)微服務(wù)編排框架詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Java中基于推、拉模式的sentinel規(guī)則持久化詳解
這篇文章主要介紹了Java中基于推、拉模式的sentinel規(guī)則持久化詳解,推模式是sentinelDashboard?把規(guī)則推給Nacos,Nacos監(jiān)聽規(guī)則的變化推給微服務(wù),拉模式是sentinelDashboard?把規(guī)則直接給微服務(wù),?Nacos定時的同步微服務(wù)端的規(guī)則,需要的朋友可以參考下2023-09-09
SpringBoot工程下Lombok的應(yīng)用教程詳解
這篇文章主要給大家介紹了關(guān)于SpringBoot工程下Lombok應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
Java創(chuàng)建類模式_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java創(chuàng)建類模式的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
MyBatis關(guān)閉一級緩存的兩種方式(分注解和xml兩種方式)
這篇文章主要介紹了MyBatis關(guān)閉一級緩存的兩種方式(分注解和xml兩種方式),mybatis默認開啟一級緩存,執(zhí)行2次相同sql,但是第一次查詢sql結(jié)果會加工處理這個時候需要關(guān)閉一級緩存,本文給大家詳細講解需要的朋友可以參考下2022-11-11

