@Data
@TableName(autoResultMap = true) //不配合 typeHandler 或 numericScale 使用无意义,演示而已
public class Child {
private Long id;
private String name;
private Long laoHanId;
private Long laoMaId;
@TableField(exist = false)
private Man laoHan;
@TableField(exist = false)
private Woman laoMa;
}
@Data
@TableName(resultMap = "m_b") // 对应xml里的 id
public class Man {
private Long id;
private String name;
private Long laoPoId;
@TableField(exist = false)
private Woman laoPo;
@TableField(exist = false)
private List<Child> waWa;
}
@Data
@TableName(autoResultMap = true) //不配合 typeHandler 或 numericScale 使用无意义,演示而已
public class Woman {
private Long id;
private String name;
private Long laoGongId;
@TableField(exist = false)
private Man laoGong;
@TableField(exist = false)
private List<Child> waWa;
}
create table man
(
id bigint(20) not null,
name varchar(30),
lao_po_id bigint(20) not null
);
create table woman
(
id bigint(20) not null,
name varchar(30),
lao_gong_id bigint(20) not null
);
create table child
(
id bigint(20) not null,
name varchar(30),
lao_han_id bigint(20) not null,
lao_ma_id bigint(20) not null
);
# 测试类
@Test
void t_c() {
final Child child = childMapper.selectLinkById(1L);
log.info("child: {}", child);
assertThat(child).isNotNull();
final Man laoHan = child.getLaoHan();
assertThat(laoHan).isNotNull();
assertThat(laoHan.getName()).isNotBlank();
final Woman laoMa = child.getLaoMa();
assertThat(laoMa).isNotNull();
assertThat(laoMa.getName()).isNotBlank();
}
# 持久层接口
public interface ChildMapper extends BaseMapper<Child> {
Child selectLinkById(Long id);
}
# 映射文件
<mapper namespace="org.example.demo02.mapper.ChildMapper">
<resultMap id="c_r" type="org.example.demo02.entity.Child">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="laoHanId" column="lao_han_id"/>
<result property="laoMaId" column="lao_ma_id"/>
<association property="laoHan" column="lao_han_id"
select="org.example.demo02.mapper.ManMapper.selectById"/>
<association property="laoMa" column="lao_ma_id"
select="org.example.demo02.mapper.WomanMapper.selectById"/>
</resultMap>
<select id="selectLinkById" resultMap="c_r">
select *
from child
where id = #{id}
</select>
</mapper>
# 由于孩子的实体类中包含父亲和母亲的id,所以在返回结果集中调用了各自的selectById方法
# 查看控制台
select * from child where id = ?
SELECT * FROM man WHERE id=?
SELECT id,name,lao_gong_id FROM woman WHERE id=?
# 查询结果
child: Child(id=1, name=小小明, laoHanId=1, laoMaId=1, laoHan=Man(id=1, name=程序猿小明, laoPoId=1, laoPo=null, waWa=null), laoMa=Woman(id=1, name=程序猿小明老婆, laoGongId=1, laoGong=null, waWa=null))
# 测试类
@Test
void t_m() {
final Man man = manMapper.selectLinkById(1L);
log.info("man: {}", man);
assertThat(man).isNotNull();
assertThat(man.getName()).isNotBlank();
final Woman laoPo = man.getLaoPo();
assertThat(laoPo).isNotNull();
assertThat(laoPo.getName()).isNotBlank();
final List<Child> waWa = man.getWaWa();
assertThat(waWa).isNotEmpty();
waWa.forEach(i -> assertThat(i.getName()).isNotBlank());
}
# 持久层接口
public interface ManMapper extends BaseMapper<Man> {
Man selectLinkById(Long id);
}
# 映射文件
<?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.example.demo02.mapper.ManMapper">
<resultMap id="m_b" type="org.example.demo02.entity.Man">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="laoPoId" column="lao_po_id"/>
</resultMap>
<resultMap id="m_r" type="org.example.demo02.entity.Man" extends="m_b"> # 这里继承了m_b
<association property="laoPo" column="lao_po_id"
select="org.example.demo02.mapper.WomanMapper.selectById"/>
<collection property="waWa" column="id"
select="org.example.demo02.mapper.ChildMapper.selectByLaoHanId"/>
</resultMap>
<select id="selectLinkById" resultMap="m_r">
select *
from man
where id = #{id}
</select>
</mapper>
# 由于父亲的实体类中包含了母亲对象和List的孩子集合,所以返回结果集中有selectById根据id查询母亲的信息,和selectByLaoHanId根据LaoHanId查询孩子信息
# 查看控制台
select * from man where id = ?
SELECT id,name,lao_gong_id FROM woman WHERE id=?
select * from child where lao_han_id = ?
# 查看返回结果
Man(id=1, name=程序猿小明, laoPoId=1, laoPo=Woman(id=1, name=程序猿小明老婆, laoGongId=1, laoGong=null, waWa=null), waWa=[Child(id=1, name=小小明, laoHanId=1, laoMaId=1, laoHan=null, laoMa=null), Child(id=5, name=大礼包, laoHanId=1, laoMaId=2, laoHan=null, laoMa=null)])
# 测试类
@Test
void t_w() {
final Woman woman = womanMapper.selectLinkById(1L);
log.info("woman: {}", woman);
assertThat(woman).isNotNull();
assertThat(woman.getName()).isNotBlank();
final Man laoGong = woman.getLaoGong();
assertThat(laoGong).isNotNull();
assertThat(laoGong.getName()).isNotBlank();
final List<Child> waWa = woman.getWaWa();
assertThat(waWa).isNotEmpty();
waWa.forEach(i -> assertThat(i.getName()).isNotBlank());
}
# 持久层接口
public interface WomanMapper extends BaseMapper<Woman> {
Woman selectLinkById(Long id);
}
# 映射文件
<mapper namespace="org.example.demo02.mapper.WomanMapper">
<resultMap id="w_r" type="org.example.demo02.entity.Woman">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="laoGongId" column="lao_gong_id"/>
<association property="laoGong" column="lao_gong_id"
select="org.example.demo02.mapper.ManMapper.selectById"/>
<collection property="waWa" column="id"
select="org.example.demo02.mapper.ChildMapper.selectByLaoMaId"/>
</resultMap>
<select id="selectLinkById" resultMap="w_r">
select *
from woman
where id = #{id}
</select>
</mapper>
# 由于母亲的实体类中包含父亲的实体对象和List孩子集合,所以返回结果集合中有selectById根据id查询父亲信息和selectByLaoMaId根据LaoMaId查询孩子信息
# 查看控制台
select * from woman where id = ?
SELECT * FROM man WHERE id=?
select * from child where lao_ma_id = ?
# 查看返回结果
Woman(id=1, name=程序猿小明老婆, laoGongId=1, laoGong=Man(id=1, name=程序猿小明, laoPoId=1, laoPo=null, waWa=null), waWa=[Child(id=1, name=小小明, laoHanId=1, laoMaId=1, laoHan=null, laoMa=null), Child(id=3, name=旺仔, laoHanId=2, laoMaId=1, laoHan=null, laoMa=null), Child(id=4, name=小馒头, laoHanId=2, laoMaId=1, laoHan=null, laoMa=null)])