前言
许多人对Myabtis持久层框架的一对多、多对一映射很模糊,本篇幅将通讲一下它们的映射关系。对谁就以谁为中心,例如:一对多中,对多,那么就是部门对员工,多对一,对一个,就是多个员工对一个部门。
数据库设计
其中d_id为外键,连接部门表dept_id。
多对一
示例:多个员工有且仅有一个部门,一个部门则是对应多个员工。
站在员工角度而言,其就是一个典型的多对一关系
- 员工Bean(Employee)
package com.mybatis.bean;
/**
* @author Superb
* @date 2020/9/11 - 15:11
* @E_mail super
*/
public class Employee {
private Integer empId;
private String empName;
//员工部门(每个员工有且仅有一个部门,典型的多对一)
private Department dept;
//set()get()及toString方法已省略
}
- 部门Bean(Department)
package com.mybatis.bean;
/**
* @author Superb
* @date 2020/9/11 - 15:13
* @E_mail super
*/
public class Department {
private Integer deptId;
private String deptName;
//set()get()及toString方法已省略
}
- 员工接口Mapper(EmployeeMapper)
查询单个员工并连接部门表Department查询其所在部门信息
通过@Results进行字段映射,column属性代表数据库字段,property属性代表JavaBean字段,特别的:如d_id映射dept(员工类部门属性),通过@One注解(一对一,多对一)进行关联查询,其中select属性指向DepartmentMapper中查询的方法名,方法参数为d_id的值,方法返回Department对象赋值给dept属性,从而达到查询单个员工并填充其部门信息的目的,即为多对一关系映射。
package com.mybatis.mapper;
import com.mybatis.bean.Employee;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @author Superb
* @date 2020/9/11 - 15:09
* @E_mail supe
*/
@Mapper
public interface EmployeeMapper {
/**
* 查询所有员工并附带每个员工所在部门
* @return
*/
@Results(id = "empMap",value = {
@Result(column = "emp_id",property = "empId",id = true),
@Result(column = "emp_name",property = "empName"),
@Result(column = "d_id",property = "dept",one = @One(select = "com.mybatis.mapper.DepartmentMapper.getDept"))
})
@Select("select * from tbl_emp")
List<Employee> getEmps();
/**
* 查询单个员工并且附带其所在部门部门
* @param id
* @return
*/
@ResultMap("empMap")//引入id为”empMap“的resultMap
@Select("select * from tbl_emp where emp_id = #{id}")
Employee getEmp(int id);
}
- 部门接口Mapper(DepartmentMapper)
普通ResultMap映射
package com.mybatis.mapper;
import com.mybatis.bean.Department;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
/**
* @author Superb
* @date 2020/9/11 - 15:17
* @E_mail super
*/
@Mapper
public interface DepartmentMapper {
/**
* 根据id查询部门,此id为员工表外键
*/
@Results({
@Result(id = true,column = "dept_id",property = "deptId"),
@Result(column = "dept_name",property = "deptName")
})
@Select("select * from tbl_dept where dept_id = #{id}")
Department getDept(int id);
}
- 测试实例
/**
* 测试
* 查询单个员工的信息并填充部门信息
*/
@Test
void test1() {
//查询一号员工卢本伟
Employee emp = employeeMapper.getEmp(1);
System.out.println(emp);
}
/**
* 测试
* 查询所有员工的信息并填充部门信息
*/
@Test
void test2() {
//查询所有员工
List<Employee> emps = employeeMapper.getEmps();
for (Employee emp : emps){
System.out.println(emp);
}
}
- 测试结果
查询一号员工并填充部门信息
Employee{empId=1, empName='卢本伟', dept=Department{deptId=5, deptName='吃干饭'}}
查询所有员工并填充部门信息
Employee{empId=1, empName='卢本伟', dept=Department{deptId=5, deptName='吃干饭'}}
Employee{empId=2, empName='大司马', dept=Department{deptId=2, deptName='销售部'}}
Employee{empId=3, empName='Uzi', dept=Department{deptId=1, deptName='人事部'}}
Employee{empId=4, empName='PDD', dept=Department{deptId=2, deptName='销售部'}}
Employee{empId=5, empName='茄子', dept=Department{deptId=3, deptName='后勤部'}}
Employee{empId=6, empName='药水哥', dept=Department{deptId=5, deptName='吃干饭'}}
Employee{empId=7, empName='旭旭宝宝', dept=Department{deptId=4, deptName='市场部'}}
Employee{empId=8, empName='小智', dept=Department{deptId=5, deptName='吃干饭'}}
Employee{empId=9, empName='化腾', dept=Department{deptId=4, deptName='市场部'}}
一对多
示例:一个部门对应多个员工,且每个员工隶属一个部门。
站在部门角度而言,其就是一个典型的一对多关系
- 员工Bean(Employee)
package com.mybatis.bean;
/**
* @author Superb
* @date 2020/9/11 - 15:11
* @E_mail superb
*/
public class Employee {
private Integer empId;
private String empName;
//set()get()及toString方法已省略
}
- 部门Bean(Department)
package com.mybatis.bean;
import java.util.List;
/**
* @author Superb
* @date 2020/9/11 - 15:13
* @E_mail superb
*/
public class Department {
private Integer deptId;
private String deptName;
private List<Employee> emps;
//set()get()及toString方法已省略
}
- 员工接口Mapper(EmployeeMapper)
package com.mybatis.mapper;
import com.mybatis.bean.Employee;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @author Superb
* @date 2020/9/11 - 15:09
* @E_mail super
*/
@Mapper
public interface EmployeeMapper {
/**
* 根据部门id查询所有此部门的员工集合,此id为部门表dept_id
*/
@Results({
@Result(column = "emp_id",property = "empId",id = true),
@Result(column = "emp_name",property = "empName")
})
@Select("select * from emp where d_id = #{id}")
List<Employee> getEmps(int id);
}
- 部门接口Mapper(DepartmentMapper)
普通ResultMap映射
查询单个部门并填充此部门中所有员工信息
通过@Results进行字段映射,column属性代表数据库字段,property属性代表JavaBean字段,特别的:如dept_id映射emps(当前部门所有员工的集合)属性,通过@Many注解进行关联查询,其中select属性指向EmployeeMapper中查询的方法名,方法参数为dept_id的值,方法返回的List值赋值给emps属性,从而达到查询单个部门并填充其所有部门员工的目的,即为一对多关系映射。
package com.mybatis.mapper;
import com.mybatis.bean.Department;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @author Superb
* @date 2020/9/11 - 15:17
* @E_mail superb12580@163.com
*/
@Mapper
public interface DepartmentMapper {
/**
* 查询所有部门信息并附带部门下所有员工
* @return
*/
@Results(id = "deptMap",value = {
@Result(id = true,column = "dept_id",property = "deptId"),
@Result(column = "dept_name",property = "deptName"),
@Result(column = "dept_id",property = "emps",many = @Many(select = "com.mybatis.mapper.EmployeeMapper.getEmps"))
})
@Select("select * from dept")
List<Department> getDepts();
/**
* 根据id查询单个部门信息并附带部门下所有员工
* @param id
* @return
*/
@ResultMap("deptMap")
@Select("select * from dept where dept_id = #{id}")
Department getDept(int id);
}
- 测试实例
/**
* 测试
* 查询单个部门并填充旗下所有员工信息
*/
@Test
void test1() {
//查询一号部门
Department dept = departmentMapper.getDept(1);
System.out.println(dept);
}
/**
* 测试
* 查询所有部门并填充旗下所有员工信息
*/
@Test
void test2() {
//查询所有部门
List<Department> depts = departmentMapper.getDepts();
for (Department dept : depts){
System.out.println(dept);
}
}
- 测试结果
查询一号员工并填充部门信息
Department{deptId=1, deptName='人事部', emps=[Employee{empId=3, empName='Uzi'}]}
查询所有员工并填充部门信息
Department{deptId=1, deptName='人事部', emps=[Employee{empId=3, empName='Uzi'}]}
Department{deptId=2, deptName='销售部', emps=[Employee{empId=2, empName='大司马'}, Employee{empId=4, empName='PDD'}]}
Department{deptId=3, deptName='后勤部', emps=[Employee{empId=5, empName='茄子'}]}
Department{deptId=4, deptName='市场部', emps=[Employee{empId=7, empName='旭旭宝宝'}, Employee{empId=9, empName='化腾'}]}
Department{deptId=5, deptName='吃干饭', emps=[Employee{empId=1, empName='卢本伟'}, Employee{empId=6, empName='药水哥'}, Employee{empId=8, empName='小智'}]}