MyBatisPlus实现多对一关系并自动查询并注入一方的值

2022-10-19 1130点热度 0人点赞 0条评论

例如这样的场景:学生和班级是多对一的关系,我们在查询学生(Student)的时候,(t_student表有一个clazz_id会对应到t_clazz表的ID),希望自动带出这个学生所属的班级(Clazz)

多方(Student)的Mapper.xml:

注意这里使用了association来表明Student下面的有对应一的属性:clazz(这个学生所属班级)

<?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.test.mapper.StudentMapper">
    <resultMap id="BaseResultMap" type="com.test.Student">
        <[email protected]>
        <!--@Table t_lab-->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>

        <association property="clazz" javaType="com.test.Clazz">
            <id column="id" jdbcType="INTEGER" property="id" />
            <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
            <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
            <result column="name" jdbcType="VARCHAR" property="name" />
        </association>
    </resultMap>
    <sql id="Base_Column_List">
        <[email protected]>
        id, create_date, update_date, `name`
    </sql>
</mapper>

一方(Clazz)的mapper.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.test.mapper.ClazzMapper">
  <resultMap id="BaseResultMap" type="com.test.entity.Clazz">
    <[email protected]>
    <!--@Table t_department-->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <sql id="Base_Column_List">
    <[email protected]>
    id, create_date, update_date, `name`
  </sql>
</mapper>

来看下实体类 Student.java,里面有一个clazz属性

@TableName(value = "t_student", resultMap = "BaseResultMap")
public class Lab implements Serializable {

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField(value = "create_date")
    private Date createDate;

    @TableField(value = "update_date")
    private Date updateDate;

    @TableField(value = "`name`")
    private String name; // 学生姓名

    private Clazz clazz; // 学生所属班级
}

Clazz.java 就比较简单,常规属性

@TableName(value = "t_clazz", resultMap = "BaseResultMap")
public class Lab implements Serializable {

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField(value = "create_date")
    private Date createDate;

    @TableField(value = "update_date")
    private Date updateDate;

    @TableField(value = "`name`")
    private String name; // 班级名称

}

在接下来的关于Student查询中,MyBatisPlus会自动做连表查询,把clazz带入进去:

LambdaQueryWrapper<Student> labLambdaQueryWrapper = new LambdaQueryWrapper<>();
List<Student> students = studentMapper.selectList(labLambdaQueryWrapper);
students.forEach(student -> student.getClazz()); // 查询student下的clazz已经自动有值了

Student student1 = studentMapper.selectById(1);// 根据ID查student
student1.getClazz(); // 也是自动赋值了

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论