MybatisPlus结合springboot实现多表级联查询

MybatisPlus级联查询实现,主要借助resultMap定义级联查询,在实体类上注解属性指定resultMap的id,添加级联表映射类的对象属性,来完成的。这样在调用Mybatis公共服务接口、公共映射器接口,返回对象时,自动完成级联查询,体现了MybatisPlus编码高效的优势。

一对一级联查询

一个医生属性一个科室,医生实体类中拥有科室对象,对应表中有department_id 字段,可以级联查询。
第一步,创建实体类

@TableName(value="h_doctor")
public class Doctor implements Serializable {//实现序列化接口
    @TableId
   private Integer id;// id int primary key auto_increment,
   private String name ;// name varchar(32),
   private Integer departmentId;//department_id int,
   private String  info;// in_fo VARCHAR(200)
   private Department departmentObj;
   。。。
}

部门表
@TableName(“h_department”)

public class Department implements Serializable {
    @TableId
    private int id;
    private String departName;
    private String info;
    。。。
    }

第二步,创建医生Mapper映射器配置,在resultMap中实现关联

<?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="org.dainei.mapper.DoctorMapper">
    <resultMap id="doctorMap" type="org.dainei.pojo.Doctor">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="info" column="info"/>
<result property="departmentId" column="department_id"/>

        <association property="departmentObj" column="department_id"
          select="org.dainei.mapper.DepartmentMapper.findDepartmentById"/>
    </resultMap>

</mapper>

第三步,给Doctor类添加注解,配置结果集映射resultMap

@TableName(value="h_doctor",resultMap = "doctorMap")
public class Doctor implements Serializable {//实现序列化接口
    @TableId
   private Integer id;// id int primary key auto_increment,
...
}

代码测试,直接调用Mybatisplus公共映射器方法

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class DoctorMapperTest {
    @Resource
    private DoctorMapper doctorMapper;

    @Test
    public void findById(){
        Doctor doctor=doctorMapper.selectById(1);
        System.out.println(doctor.getDepartmentBean());
        System.out.println(doctor);
    }
    @Test
    public void testListAssociate(){
        List<Doctor> list = doctorMapper.selectList(new QueryWrapper<>());
        list.forEach(System.out::println);
    }
}

```csharp

一对多级联查询

一个部门有多个医生

@TableName(value="h_department" ,resultMap = "departmentMap")
public class Department {
    @TableId
    private int id;      //id int PRIMARY key,
    private String name; //name VARCHAR(64) UNIQUE not null,
    private String info; //info VARchar(250),
    private int  hosipitalId; //hosipital_id int not null
    @TableField(exist = false)
    private List<Doctor> doctorList;
。。。
}

医生类

public class Doctor implements Serializable {//实现序列化接口
    @TableId
   private Integer id;// id int primary key auto_increment,
   private String name ;// name varchar(32),
    @TableField("department_id") //注解对应表中与该属性对应的字段
   private Integer departmentId;//department_id int,
   private String  info;// in_fo VARCHAR(200)
   @TableField(exist = false)  //注解类的属性是否为数据库表对应字段
。。。
}

部门映射器配置

DepartmentMapper.xml
<resultMap id="departmentMap" type="org.dainei.pojo.Department">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="info" column="info"/>
    <result property="hosipitalId" column="hosipital_id"/>
    <collection  property="doctorList" column="id"
    select="org.dainei.mapper.DoctorMapper.findDoctorListByDeptId"
    />
</resultMap>

医生映射器接口添加

List<Doctor> findDoctorListByDeptId(int id);

医生映射器配置

<select id="findDoctorListByDeptId" parameterType="_int" resultType="doctor">
    select id,name,department_id,info from h_doctor where department_id=#{id};
</select>

代码测试,直接调用Mybatisplus公共映射器方法

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class DepartmentMapperTest {
    @Resource
    private DepartmentMapper departmentMapper;

    @Test
    public void selectOne(){
        Department department=departmentMapper.selectById(203);
        System.out.println(department);
    }
    @Test
    public void list(){
        List<Department> departments=departmentMapper.selectList(null);
        System.out.println(departments);
    }
}
``

## 小结:

完成级联后,在控制层、服务层调用Mybatisplus提供的CRUD操作、或条件查询器时,自动完成级联查询,体现了Java框架强大的面向对象特性。`

``

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值