1.部门表
CREATE TABLE `department` (
`id` int NOT NULL,
`dept_name` varchar(255) DEFAULT NULL,
`dept_location` varchar(255) DEFAULT NULL,
`tenant_id` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
2.组织表
CREATE TABLE `groups` (
`id` int NOT NULL,
`group_name` varchar(255) DEFAULT NULL,
`dept_id` int NOT NULL,
`tenant_id` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
3.员工表
CREATE TABLE `employee` (
`id` int NOT NULL,
`employee_name` varchar(255) DEFAULT NULL,
`group_id` int NOT NULL,
`tenant_id` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
二、实体类
1.部门
@Data
@TableName("department")
public class Department {
private Integer id;
private String deptName;
private String deptLocation;
private List groupList;
}
2.组织
@Data
@TableName("groups")
public class Groups {
private Integer id;
private String groupName;
private List employeeList;
}
3.员工
@Data
@TableName("employee")
public class Employee {
private Integer id;
private String employeeName;
}
三、查询方法
//根据部门ID查询
@Mapper
public interface DepartmentMapper extends BaseMapper {
List testMybatis(@Param("id") Integer id);
}
四、mapper.xml,sql有两种写法,查询结果是一样的 第一种:
第二种:
SELECT
a.id,
a.dept_name,
a.dept_location,
b.id as gid,
b.group_name,
c.id as eid,
c.employee_name
FROM department a
LEFT JOIN `groups` b ON a.id = b.dept_id
LEFT JOIN `employee` c ON b.id = c.group_id
WHERe a.id = #{id}