美文网首页
编程改变世界(9)-- mybatis中的多对多映射

编程改变世界(9)-- mybatis中的多对多映射

作者: 天空之小程 | 来源:发表于2019-03-23 18:05 被阅读0次

    上次我们说了一对多查询,这次我们来说一下多对多查询
    这样的场景大家一定不陌生,一名同学可以选多门课,而一门课又有多个同学选,这就是典型的多对多关系(多对多就可以理解为两个一对一的组合)
    这次我们需要创建一个课程表tb_lesson表和一个选课表tb_ls,如下


    image.png
    image.png

    然后建立相应的pojo类Lesson

    package com.pojo;
    
    import java.util.List;
    
    public class Lesson {
        private int id;
        private String name;
        private List<Student> students;
    
        public Lesson() {
        }
    
    
        public Lesson(int id, String name, List<Student> students) {
            this.id = id;
            this.name = name;
            this.students = students;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Student> getStudents() {
            return students;
        }
    
        public void setStudents(List<Student> students) {
            this.students = students;
        }
    
        @Override
        public String toString() {
            return "Lesson{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", students=" + students +
                    '}';
        }
    }
    
    

    下一步写lessonMapper

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="mapper.lessonMapper">
    
        <select id="selectLesson" resultMap="lessonMap">
            select * from tb_student s
            inner join tb_ls ls on s.sid=ls.s_id
            inner join tb_lesson l on l.id=ls.l_id
        </select>
        
        <resultMap id="lessonMap" type="Lesson">
            <id column="id" property="id"/>
            <result column="lname" property="name"/>
            <collection property="students" ofType="Student">
                <id column="sid" property="sid"/>
                <result column="name" property="name"/>
            </collection>
        </resultMap>
        
    
    </mapper>
    

    测试类

    package com.test;
    
    
    import com.dao.ClassesDao;
    import com.dao.LessonDao;
    import com.pojo.Classes;
    import com.pojo.Lesson;
    import com.pojo.Student;
    
    import java.util.List;
    
    public class Test {
    
        public static void main(String[] args) {
    
            LessonDao lessonDao = new LessonDao();
            List<Lesson> list = lessonDao.findLesson();
            for (Lesson lesson : list) {
                System.out.println(lesson.getName());
                List<Student> students = lesson.getStudents();
                for (Student student : students) {
                    System.out.println(student);
                }
            }
        }
    }
    
    image.png

    如果以学生表为主表,则Student类改为

    package com.pojo;
    
    import java.util.List;
    
    public class Student {
        
        private int sid;
        private String name;
        private Classes classes;
        private List<Lesson> lessons;
    
        public Student() {
        }
    
    
        public Student(int sid, String name, Classes classes, List<Lesson> lessons) {
            this.sid = sid;
            this.name = name;
            this.classes = classes;
            this.lessons = lessons;
        }
    
        public int getSid() {
            return sid;
        }
    
        public void setSid(int sid) {
            this.sid = sid;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Classes getClasses() {
            return classes;
        }
    
        public void setClasses(Classes classes) {
            this.classes = classes;
        }
    
        public List<Lesson> getLessons() {
            return lessons;
        }
    
        public void setLessons(List<Lesson> lessons) {
            this.lessons = lessons;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "sid=" + sid +
                    ", name='" + name + '\'' +
                    ", classes=" + classes +
                    ", lessons=" + lessons +
                    '}';
        }
    }
    
    

    studentMapper改为

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="mapper.studentMapper">
        <select id="findStudentByClassesId" resultType="Student">
            select * from tb_student where c_id=#{id}
        </select>
    
        <select id="findAll" resultMap="studentMap">
            select * from tb_student s
            inner join tb_ls ls on s.sid=ls.s_id
            inner join tb_lesson l on l.id=ls.l_id
        </select>
    
        <resultMap id="studentMap" type="Student">
            <id column="sid" property="sid"/>
            <result column="name" property="name"/>
            <collection property="lessons" ofType="Lesson">
                <id column="id" property="id"/>
                <result column="lname" property="name"/>
            </collection>
        </resultMap>
    </mapper>
    

    测试类

    package com.test;
    
    
    import com.dao.ClassesDao;
    import com.dao.LessonDao;
    import com.dao.StudentDao;
    import com.pojo.Classes;
    import com.pojo.Lesson;
    import com.pojo.Student;
    
    import java.util.List;
    
    public class Test {
    
        public static void main(String[] args) {
    
    
            StudentDao studentDao = new StudentDao();
            List<Student> students = studentDao.findStudent();
            for (Student student:students){
                System.out.println(student.getName());
                List<Lesson> lessons = student.getLessons();
                for (Lesson lesson:lessons){
                    System.out.println(lesson);
                }
            }
        }
    }
    

    结果


    image.png

    相关文章

      网友评论

          本文标题:编程改变世界(9)-- mybatis中的多对多映射

          本文链接:https://www.haomeiwen.com/subject/kwrsvqtx.html