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 }

测试结果:

mybatis学习日记-连表查询-一对多

 

上一篇:Java处理ztree数据


下一篇:django-jwt | django缓存