MyBatis07-(多對一、一對多)

愛喝椰汁的木木發表於2020-10-28

專案結構圖

在這裡插入圖片描述

環境搭建

1.建立資料庫

CREATE TABLE `teacher` (
	`id` INT(10) NOT NULL,
	`name` VARCHAR(30) DEFAULT NULL,
	PRIMARY KEY (`id`)
) ENGINE = INNODB DEFAULT CHARSET=utf8

INSERT INTO teacher(`id`,`name`) VALUES (1,'李老師');

CREATE TABLE `student` (
	`id` INT(10) NOT NULL,
	`name` VARCHAR(30) DEFAULT NULL,
	`tid` INT(10) DEFAULT NULL,
	PRIMARY KEY (`id`),
	KEY `fktid` (`tid`),
	CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE= INNODB DEFAULT CHARSET=utf8

INSERT INTO student(`id`,`name`,`tid`) VALUES ('1','小明','1');
INSERT INTO student(`id`,`name`,`tid`) VALUES ('2','小張','1');
INSERT INTO student(`id`,`name`,`tid`) VALUES ('3','小李','1');
INSERT INTO student(`id`,`name`,`tid`) VALUES ('4','小紅','1');
INSERT INTO student(`id`,`name`,`tid`) VALUES ('5','小王','1');

2.複製db.properties,mybatis-config.xml,MybatisUtils

3.建立實體類(使用Lombok外掛,需要引入maven依賴)
Student

package com.codeyancy.pojo;
import lombok.Data;

@Data
public class Student {
    private int id;
    private String name;

    //學生需要關聯一個老師
    private Teacher teacher;
}

Teacher

package com.codeyancy.pojo;

import lombok.Data;

@Data
public class Teacher {
    private int id;
    private String name;
}

4.編寫實體類對應的Mapper介面 【兩個】
StudentMapper

package com.codeyancy.dao;

import com.codeyancy.pojo.Student;

import java.util.List;

public interface StudentMapper {

}

TeacherMapper

package com.codeyancy.dao;

import com.codeyancy.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface TeacherMapper {

}

5.編寫Mapper介面對應的 mapper.xml配置檔案 【兩個】
StudentMapper.xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.codeyancy.dao.StudentMapper">

</mapper>

TeacherMapper.xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.codeyancy.dao.TeacherMapper">

</mapper>

6.修改mybatis-config.xml

    <mappers>
        <mapper resource="com/codeyancy/dao/TeacherMapper.xml "/>
        <mapper resource="com/codeyancy/dao/StudentMapper.xml " />
    </mappers>

環境搭建成功

多對一

多對一的理解:

  • 多個學生對應一個老師
  • 如果對於學生這邊,就是一個多對一的現象,即從學生這邊關聯一個老師!

按查詢巢狀處理 步驟(子查詢)

1.編寫StudentMapper

package com.codeyancy.dao;

import com.codeyancy.pojo.Student;

import java.util.List;

public interface StudentMapper {

    //查詢所有的學生資訊,以及對應的老師的資訊
    public List<Student> getStudent();
}

2.編寫StudentMapper.xml

    <!--
    思路:
    1.查詢所有學生的資訊
    2.根據查詢出來的學生的tid,尋找對應的老師     子查詢
    -->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from mybatis.student;
    </select>
    
    <resultMap id="StudentTeacher" type="Student">
        <!--result只能針對單個屬性-->
        <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 mybatis.teacher where id = #{tid};
    </select>

3.MyTest進行測試

    @Test
    public void testStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent();

        for (Student student : studentList) {
            System.out.println(student);
        }

        sqlSession.close();
    }

4.測試結果
在這裡插入圖片描述

按結果巢狀處理 步驟(連表查詢)

1.編寫StudentMapper

public List<Student> getStudent2();

2.編寫StudentMapper.xml

    <!--按照結果巢狀處理-->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname
        from mybatis.student s,mybatis.teacher t
        where s.tid = t.id;
    </select>
    
    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

3.MyTest進行測試

    @Test
    public void testStudent2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent2();

        for (Student student : studentList) {
            System.out.println(student);
        }

        sqlSession.close();

    }

4.測試結果

在這裡插入圖片描述

一對多

相關文章