前面文章 java springboot整合MyBatis做数据库查询操作写了springboot整合MyBatis的方法 并演示了基础查询的语法 根据id查
那么 我们这次来演示联合查询
我们staff 表 内容如下
每条数据 对应的都有一个departmentid 这是 department部门表的外键id
department表内容如下
如果你连主外键都还未掌握的话 建议先去用 MySql实现了 联合查询 再来用开发语言实现
然后 我们将 staff 代码修改如下
package com.example.textm.domain;public class staff {private int id;private String name;private int age;private int status;private int departmentid;private String departmentName;@Overridepublic String toString(){return "staff{"+"id"+id+"namne"+name+"age"+age+"status"+status+"departmentid"+departmentid+(departmentName != null?"departmentName"+departmentName:"")+"}";}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 getAge() {return age;}public void setAge(int age) {this.age = age;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}public int getDepartmentid() {return departmentid;}public void setDepartmentid(int departmentid) {this.departmentid = departmentid;}public void setDepartmentName(String departmentName) {this.departmentName = departmentName;}public String getDepartmentName() {return departmentName;}
}
新加了一个departmentName是用来存 部门名称的 我们用MyBatis查询 直接挂我们需要的字段就好了 没必要整个对象都带进来
然后 我们在staffDao添加一个函数
Select("select s.*, d.name as departmentName from staff s inner join department d on s.departmentid=d.id;")
List<staff> getAllStaffWithDepartment();
我们 sql中已经写明 将 department代理为 d 然后 将d中的 name转为departmentName
这就是我说的 思路 如果你还有个age 继续往后加就好了
@Select("SELECT s.*, d.name AS departmentName, d.age AS departmentAge FROM staff s INNER JOIN department d ON s.departmentid=d.id;")
List<staff> getAllStaffWithDepartment();
然后 我们通过测试类 来调用getAllStaffWithDepartment
编写代码如下
System.out.println(staffDao.getAllStaffWithDepartment());
运行结果如下
我们的人员也都被带出了 部门信息
然后 在 domain目录下创建一个 department
这就是 department 表对应的属性类
参考代码如下
package com.example.textm.domain;import java.util.List;public class department {private int id;private String name;private List<staff> staffList;@Overridepublic String toString(){return "department{"+"id="+id+"name="+name+(staffList != null?staffList:"暂无员工")+"}";}public void setId(int id) {this.id = id;}public int getId() {return id;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setStaffList(List<staff> staffList) {this.staffList = staffList;}public List<staff> getStaffList() {return staffList;}
}
对加了一个 staffList 用来存 部门下的员工 类型当然就是一个staff员工类的list集合
然后 在dao下创建一个接口 叫 departmentDom
用来写 department这边的sql
然后 我们编写代码
package com.example.textm.dao;import com.example.textm.domain.department;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface departmentDom {@Select("SELECT d.id as deptId, d.name as deptName FROM department d")@Results(id = "departmentMap", value = {@Result(property = "id", column = "deptId"),@Result(property = "name", column = "deptName"),@Result(property = "staffList", column = "deptId", javaType = List.class,many = @Many(select = "com.example.textm.dao.staffDao.getStaffByDepartmentId"))})List<department> getAllDepartmentsWithStaff();
}
我们方法中的 sql是根据部门表 department 去查 顺便带出员工表外键对应的信息 我指定 id和name用自己的 staffList 通过staffDao接口的 getStaffByDepartmentId函数去获取
所以 我们staffDao接口还要再加一个函数 通过 部门id 查询 staff员工表 然后反应 staff员工类的list集合给他的函数
@Select("SELECT * FROM staff WHERE departmentid = #{departmentId}")
List<staff> getStaffByDepartmentId(int departmentId);
然后 在测试类中调用 departmentDom 的getAllDepartmentsWithStaff
首先 我们要写
@Autowired
private departmentDom departmentDom;
装配一下我们刚刚写的 departmentDom
然后调用
System.out.println(departmentDom.getAllDepartmentsWithStaff())
运行结果如下
我们部门下 也都带出了对应的员工信息