1. 程式人生 > 其它 >mybatis學習日記-連表查詢-一對多

mybatis學習日記-連表查詢-一對多

按照結果集查詢:

dao層:

package com.fu.dao;

import com.fu.pojo.Student;

import java.util.List;

public interface StudentMapper {

}
package com.fu.dao;

import com.fu.pojo.Teacher;

import java.util.List;

public interface TeacherMapper {
    //查詢一個老師的資訊,及其所教的學生的資訊
    public List<Teacher> getTeacherList();
}

dao層對應的xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <mapper namespace="com.fu.dao.TeacherMapper">
 7     <resultMap id="teacher-student" type="teacher">
 8
<result column="tid" property="id"/> 9 <result column="tname" property="name"/> 10 11 <!-- 一對多:使用collection,property為實體類中的欄位,在collection中使用ofType匹配實體類的欄位, 12 在 association中,使用javaType匹配實體類的欄位 13 複雜屬性我們需要單獨處理:物件使用:association,集合使用:collection
14 javaType指定屬性的型別! 15 集合中的泛型資訊,我們使用offType獲取 16 --> 17 <collection property="student" ofType="student"> 18 <result column="sid" property="id"/> 19 <result column="sname" property="name"/> 20 <result column="stid" property="tid"/> 21 </collection> 22 </resultMap> 23 24 <select id="getTeacherList" resultMap="teacher-student"> 25 SELECT t.`id` AS tid,t.`name` AS tname,s.`id` AS sid,s.`name` AS sname,s.`tid` AS stid 26 FROM teacher AS t,student AS s 27 WHERE t.`id`=s.`tid` 28 </select> 29 30 </mapper>

pojo層:

 1 package com.fu.pojo;
 2 
 3 import org.apache.ibatis.type.Alias;
 4 
 5 @Alias("student")
 6 public class Student {
 7     private int id;
 8     private String name;
 9     private int tid;
10 
11     public Student(int id, String name, int tid) {
12         this.id = id;
13         this.name = name;
14         this.tid = tid;
15     }
16 
17     public int getId() {
18         return id;
19     }
20 
21     public void setId(int id) {
22         this.id = id;
23     }
24 
25     public String getName() {
26         return name;
27     }
28 
29     public void setName(String name) {
30         this.name = name;
31     }
32 
33     public int getTid() {
34         return tid;
35     }
36 
37     public void setTid(int tid) {
38         this.tid = tid;
39     }
40 
41     @Override
42     public String toString() {
43         return "Student{" +
44                 "id=" + id +
45                 ", name='" + name + '\'' +
46                 ", tid=" + tid +
47                 '}';
48     }
49 }
 1 package com.fu.pojo;
 2 
 3 import org.apache.ibatis.type.Alias;
 4 
 5 import java.util.List;
 6 
 7 @Alias("teacher")
 8 public class Teacher {
 9     private int id;
10     private String name;
11     private List<Student> students;
12 
13     public Teacher(int id, String name, List<Student> students) {
14         this.id = id;
15         this.name = name;
16         this.students = students;
17     }
18 
19     public int getId() {
20         return id;
21     }
22 
23     public void setId(int id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public List<Student> getStudents() {
36         return students;
37     }
38 
39     public void setStudents(List<Student> student) {
40         this.students = student;
41     }
42 
43     @Override
44     public String toString() {
45         return "Teacher{" +
46                 "id=" + id +
47                 ", name='" + name + '\'' +
48                 ", students=" + students +
49                 '}';
50     }
51 
52     public Teacher() {
53     }
54 }

測試類:

 1 package com.fu.dao;
 2 
 3 import com.fu.pojo.Teacher;
 4 import com.fu.utils.MybatisUtils;
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.junit.Test;
 7 
 8 
 9 import java.util.List;
10 
11 public class MyTest {
12 
13     @Test
14     public void getTeacherListTest(){
15 
16         //獲取sqlSession
17         SqlSession sqlSession = MybatisUtils.getSqlSession();
18         TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
19         List<Teacher> teacherList = mapper.getTeacherList();
20         for (Teacher teacher : teacherList) {
21             System.out.println(teacher);
22         }
23         sqlSession.close();
24     }
25 }

測試結果: