欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis一对多和多对一的处理

程序员文章站 2022-06-22 17:16:47
文章目录一、多对一的处理1.数据库建表2.student和teacher的实体类3.StudentMapper接口4.StudentMapper.xml文件配置二、一对多的处理一、多对一的处理多位student对应一位teacher,我们需要实现的操作是,查询同学的同时也能查到对应的老师。1.数据库建表teacher表CREATE TABLE `teacher` (`id` int(15) NOT NULL ,`name` varchar(25),PRIMARY KEY (`id...


一、多对一的处理

多位student对应一位teacher,我们需要实现的操作是,查询同学的同时也能查到对应的老师。
Mybatis一对多和多对一的处理

1.数据库建表

  • teacher表
CREATE TABLE `teacher` (
`id`  int(15) NOT NULL ,
`name`  varchar(25),
PRIMARY KEY (`id`)
);
  • student表
CREATE TABLE `student` (
`id`  int(15) NOT NULL ,
`name`  varchar(25),
`tid`  int(15) NOT NULL ,
PRIMARY KEY (`id`),
CONSTRAINT `tid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
)
;

2.student和teacher的实体类

  • Student.java
package dao.pojo;

public class Student {
    private int id;
    private String name;
    private Teacher teacher;

    public Student() {
    }

    public Student(int id, String name, Teacher teacher) {
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }
    

    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 Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", teacher=" + teacher +
                '}';
    }
}

  • Teacher.java
package dao.pojo;

public class Teacher {
    private int id;
    private String name;

    public Teacher() {
    }

    public Teacher(int id, String name) {
        this.id = id;
        this.name = name;
    }

    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;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

3.StudentMapper接口

我们的操作暂时在student上,不需要Teacher接口。

package dao.userdao;

import dao.pojo.Student;

import java.util.List;

public interface StudentMapper {
    
    List<Student> getStudents();
}

4.StudentMapper.xml文件配置

  • 原来的查询teacher查出来是null
<?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="dao.userdao.StudentMapper">

    <select id="getStudents" resultType="student">
    select * from test_student
    </select>

</mapper>

Mybatis一对多和多对一的处理

  • 两种解决办法:按照查询嵌套处理和按照结果嵌套处理
  • 按照查询嵌套处理:

StudentMapper.xml

<?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="dao.userdao.StudentMapper">

    <!--
    思路:1.查询所有的学生信息
         2.根据查询出来的学生的tid,寻找对应的老师
    -->
    
    <select id="getStudents" resultMap="StudentTeacher">
        select * from test_student
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性,我们需要单独处理  对象:association  集合:collection-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="Teacher">
        select * from test_teacher where id=#{id}
    </select>

</mapper>

Mybatis一对多和多对一的处理

  • 按照结果嵌套处理:

StudentMapper.xml

<?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="dao.userdao.StudentMapper">

    <select id="getStudents" resultMap="StudentTeacher">
        select s.id sid,s.name sname,t.id stid,t.name tname
        from test_student s,test_teacher t
        where s.tid = t.id
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" column="tname" javaType="Teacher">
            <result property="id" column="stid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>

</mapper>

Mybatis一对多和多对一的处理

二、一对多的处理

一位teacher管理多位student,我们需要实现的操作是,查询老师的同时也能查到对应的所有学生。

Mybatis一对多和多对一的处理

1.student和teacher的实体类

  • Student.java
package dao.pojo;

public class Student {
    private int id;
    private String name;
    private int tid;

    public Student() {
    }

    public Student(int id, String name, int tid) {
        this.id = id;
        this.name = name;
        this.tid = tid;
    }

    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 int getTid() {
        return tid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", tid=" + tid +
                '}';
    }
}

  • Teacher.java
package dao.pojo;

import java.util.List;

public class Teacher {
    private int id;
    private String name;
    private List<Student> students;

    public Teacher() {
    }

    public Teacher(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 "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", students=" + students +
                '}';
    }
}

2.TeacherMapper接口

package dao.userdao;


import dao.pojo.Teacher;
import org.apache.ibatis.annotations.Param;

public interface TeacherMapper {

    //根据老师的id,查询老师和他管理的学生
    Teacher getTeacherById(@Param("id") int id);
}

3.TeacherMapper.xml文件配置

  • 按照结果嵌套处理:

TeacherMapper.xml

<?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="dao.userdao.TeacherMapper">

    <select id="getTeacherById" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.id tid,t.name tname
        from test_student s,test_teacher t 
        where s.tid=t.id and t.id=#{id}
    </select>
    
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--
             复杂的属性,我们需要单独处理   对象: association   集合:collection
             javaType=""  指定属性的类型
             集合中的泛型信息,我们使用ofType获取
        -->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

</mapper>

Mybatis一对多和多对一的处理

  • 按照查询嵌套处理

TeacherMapper.xml

<?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="dao.userdao.TeacherMapper">

    <select id="getTeacherById" resultMap="TeacherStudent">
        select * from test_teacher where id=#{id}
    </select>

    <resultMap id="TeacherStudent" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentsByTeacherId" column="id"/>
    </resultMap>

    <select id="getStudentsByTeacherId" resultType="student" >
        select * from test_student where tid=#{id}
    </select>
    


</mapper>

Mybatis一对多和多对一的处理

小结

  1. 关联-association (多对一)
  2. 集合-collection (一对多)
  3. javaType & ofType
  • javaType:用来指定实体类中属性的类型
  • ofType:用来指定映射到List或集合中的pojo类型,泛型中的约束型

本文地址:https://blog.csdn.net/qq_43466788/article/details/108568065