MyBatis如何實現(xiàn)多表查詢(多對一、一對多)
MyBatis實現(xiàn)多表查詢
一、多對一查詢
數(shù)據(jù)庫的準備
創(chuàng)建兩張表,一張老師表,一張學生表
將老師主鍵id關聯(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 ) //建立主外鍵關聯(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; //學生需要關聯(lián)一個老師 private Teacher teacher; }
@Data public class Teacher { private int id; private String name; }
1、嵌套查詢處理
編寫接口
public interface StudentMapper { //查詢所有學生的信息以及對應老師的信息 public List<Student> getStudent(); }
2. 編寫StudentMapper.xml的查詢語句(重點)
<mapper namespace="dao.StudentMapper"> <!-- 思路: 1. 查詢所有學生的信息 根據(jù)查詢出來的學生tid,尋找對應的老師 --> <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類型,泛型中的約束類型
到此這篇關于MyBatis如何實現(xiàn)多表查詢(多對一、一對多)的文章就介紹到這了,更多相關MyBatis多表查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot yml配置文件調(diào)用過程解析
這篇文章主要介紹了SpringBoot yml配置文件調(diào)用過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法
這篇文章主要介紹了java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法的相關資料,在開發(fā)過程中經(jīng)常會用到String 這個類進行操作,需要的朋友可以參考下2017-08-08Springboot如何通過yml配置文件為靜態(tài)成員變量賦值
這篇文章主要介紹了Springboot如何通過yml配置文件為靜態(tài)成員變量賦值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter
這篇文章主要介紹了dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03