Mybatis关系映射笔记

一对一关系映射

一个旅客对应一个护照
一个护照对应一个旅客(实现方式反过来就行)略

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Passenger {

    private Integer id;
    private String name;
    //'f'女,'m'男
    private char sex;
    private Date birthday;

    //旅客对应的护照信息: 关系属性
    private Passport passport;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Passport {

    private Integer id;
    private String nationality;
    private Date expire;

    //护照对应的旅客信息: 关系属性
    private Passenger passenger;
}

mapper.java

public interface PassengerMapper {

    // 通过旅客id,查新旅客和护照信息
    Passenger findPassengerById(@Param("id") Integer id);
}

xml映射文件



<mapper namespace="com.ff.crud.mapper.PassengerMapper">
    <resultMap id="passenger_passport" type="Passenger">
        <id column="id" property="id">id>
        <result column="name" property="name">result>
        <result column="sex" property="sex">result>
        <result column="birth" property="birth">result>
        
        <association property="passport" javaType="Passport">
            <id column="passId" property="id">id>
            <result column="nationality" property="nationality">result>
            <result column="expire" property="expire">result>
        association>
    resultMap>

    
    <select id="findPassengerById" resultMap="passenger_passport">
        select t_passengers.id,t_passengers.name,t_passengers.sex,t_passengers.birthday,
               t_passports.id passId,t_passports.nationality,t_passports.expire
        from t_passengers inner join t_passports
        on t_passengers.id = t_passports.passengers_id
        where t_passengers.id = #{id};
    select>
mapper>

对应关系

type=“Passport” 对应着返回的实体类
javaType=“Passenger” 对应着属性的实体类

Mybatis关系映射笔记_第1张图片

Test文件

@Test
public void findPassengerById(){
    Passenger passenger = passengerMapper.findPassengerById(1);
    System.out.println(passenger);
}

一对多关系映射

一个部门对应多个员工
一个员工对应一个部门(实际就是一对一)略

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {

    private Integer id;
    private String name;
    private String location;

    //部门下所有的员工信息:   关系属性
    private List<Employee> employee;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

    private Integer id;
    private String name;
    private Double salary;

    //员工所属部门的信息:    关系属性
    private Department department;
}

mapper.java

public interface DepartmentMapper {

    //查询部门同时得到所有的员工信息
    Department findDepartmentById(@Param("id") Integer id);
}

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="com.ff.crud.mapper.DepartmentMapper">
    <resultMap id="dept_emp" type="Department">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="location" property="location"></result>
        <!-- emp_id emp_name salary -->
        <collection property="employees" ofType="Employee">
            <id column="emp_id" property="id"></id>
            <result column="emp_name" property="name"></result>
            <result column="salary" property="salary"></result>
        </collection>
    </resultMap>

    <select id="findDepartmentById" resultMap="dept_emp">
        select dept.id,dept.name,dept.location,
               emp.id emp_id,emp.name emp_name,emp.salary
        from t_department dept inner join t_employee emp
        on dept.id = emp.dept_id
        where dept.id = #{id}
    </select>
</mapper>

对应关系

type=“Department” 对应着返回的实体类
ofType=“Employee” 对应着属性的集合的”泛型“

Mybatis关系映射笔记_第2张图片

Test文件

@Autowired
private DepartmentMapper departmentMapper;

@Test
public void findDepartmentById(){
    Department department = departmentMapper.findDepartmentById(1);
    System.out.println(department);
    List<Employee> employees = department.getEmployees();
    for (Employee employee : employees) {
        System.out.println(employee);
    }
}

多对多关系映射

一门课程对应多个学生
一个学生对应多个课程
实际就是一对多,过程略

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student2 {

    private Integer id;
    private String name;
    private char sex;

    List<Subject> subjects;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Subject {

    private Integer id;
    private String name;
    private Integer grade;

    List<Student2> students;
}

涉及到的表
Mybatis关系映射笔记_第3张图片

三表查询语句

select sub.id,sub.name,sub.grade,
       stu.id stuId,stu.name stuName,stu.sex
from t_subjects sub inner join t_stu_sub
on sub.id = t_stu_sub.subject_id
inner join t_students stu
on t_stu_sub.student_id = stu.id;

Mybatis关系映射笔记_第4张图片

总sql语句

use restful_crud;

create table t_user(
    id int primary key auto_increment ,
    username varchar(50) not null ,
    password varchar(50) not null ,
    gender tinyint not null ,
    regist_time datetime not null
) default charset=utf8;
########################################################################

create table student(
    id varchar(32) primary key ,
    name varchar(50),
    gender tinyint
) default charset=utf8;
########################################################################

create table t_passengers(
    id int primary key auto_increment,
    name varchar(50),
    sex varchar(1),
    birthday date
) default charset=utf8;

create table t_passports(
    id int primary key auto_increment,
    nationality varchar(50),
    expire date,
    passengers_id int unique ,
    foreign key (passengers_id) references t_passengers(id)
) default charset=utf8;

insert into t_passengers values(null,'fanfan_01','f','2018-11-11');
insert into t_passengers values(null,'fanfan_02','m','2019-12-10');

insert into t_passports values(null,'china','2030-11-11',1);
insert into t_passports values(null,'fanfan_01','2035-12-10',2);

select t_passengers.id,t_passengers.name,t_passengers.sex,t_passengers.birthday,
       t_passports.id passId,t_passports.nationality,t_passports.expire
from t_passengers inner join t_passports
on t_passengers.id = t_passports.passengers_id
where t_passengers.id = 1;

select t_passports.id,t_passports.nationality,t_passports.expire,
       t_passengers.id passenger_id,t_passengers.name,t_passengers.sex,t_passengers.birthday
from t_passports inner join t_passengers
on t_passports.passengers_id = t_passengers.id
where t_passports.id = 2;
########################################################################

create table t_department(
    id int primary key auto_increment,
    name varchar(50),
    location varchar(100)
) default charset=utf8;

create table t_employee(
    id int primary key auto_increment,
    name varchar(50),
    salary double,
    dept_id int,
    foreign key (dept_id) references t_department(id)
) default charset=utf8;

insert into t_department values(1,'教学部','北京'),(2,'研发部','上海');

insert into t_employee values(1,'fanfan01',10000.5,1),(2,'fanfan02',20000.5,1),
                             (3,'张三',9000.5,2),(4,'李四',8000.5,2);

select dept.id,dept.name,dept.location,
       emp.id emp_id,emp.name emp_name,emp.salary
from t_department dept inner join t_employee emp
on dept.id = emp.dept_id
where dept.id = 1;

select emp.id,emp.name,emp.salary,
       dept.id dept_id,dept.name dept_name,dept.location
from t_employee emp inner join t_department dept
on emp.dept_id = dept.id
where emp.id = 3;
########################################################################

create table t_students(
    id int primary key auto_increment,
    name varchar(50),
    sex varchar(1)
) default charset=utf8;

create table t_subjects(
    id int primary key auto_increment,
    name varchar(50),
    grade int
) default charset=utf8;

create table t_stu_sub(
    student_id int,
    subject_id int,
    foreign key (student_id) references t_students(id),
    foreign key (subject_id) references t_subjects(id),
    primary key (student_id,subject_id)
) default charset=utf8;

insert into t_students values (1,'fanfan','m'),(2,'张三','f');
insert into t_subjects values (1001,'javaSE',1),(1002,'JavaWeb',2);
insert into t_stu_sub values (1,1001),(1,1002),(2,1001),(2,1002);

select sub.id,sub.name,sub.grade,
       stu.id stuId,stu.name stuName,stu.sex
from t_subjects sub inner join t_stu_sub
on sub.id = t_stu_sub.subject_id
inner join t_students stu
on t_stu_sub.student_id = stu.id;

关系总结

建立关系属性后,对应的Mapper文件使用完成多表映射
持有对象关系属性,使用
持有集合关系属性,使用

你可能感兴趣的:(笔记)