时间飞逝,今年满打满算已经是实习的第七天啦,以前MP用习惯了(公司好像都不用MP的!)导致xml的配置文件都不太会写了,今天就来复盘复盘公司用到的级联查询吧!
一、实体类
@Data
public class Department{
private Integer id;
private String sysName;
private String hasRank;
private String officeId;
private Office office;
//就不全写出来啦
}
@Data
public class Office{
private Integer id;
private String name;
}
二、xml文件
<resultMap id="getById" type="">
<id property="id" column="id"></id>
<result property="sysName" column="sys_name"></result>
<result property=" hasRank" column=" has_rank"></result>
<result property="officeId" column="office_id"></result>
<association property="office" javaType="Office">
//这里的id就是赋值给Department中的office.id
<result property="id" column="office.id"></result>
<result property="name" column="office.name"></result>
</association>
</resultMap>
//这里就是为图方便省得每次重复使用这段代码都要重新写
<sql id="levelProtectManagerJoins">
LEFT JOIN sys_office office ON b.officeId = office.id
</sql>
<select id="get" resultMap="getById">
SELECT b.id,
b.sys_name,
b.has_rank,
office.id AS "office.id",
office.name AS "office.name"
FROM tb_level_protect_manager b
<include refid="levelProtectManagerJoins"></include>
where b.id = #{id}
</select>
以上就是基于mybatis多表查询的复盘,欢迎大佬指正!!!!!!!!!