Mybatis-plus resultMap结果集映射

在这里插入图片描述

1实体类

package com.zs.testmybatisplus.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Data;

/**
 * @author miemie
 * @since 2019-11-27
 */
@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;
}

package com.zs.testmybatisplus.entity;

import java.util.List;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Data;

/**
 * @author miemie
 * @since 2019-11-27
 */
@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;
}

package com.zs.testmybatisplus.entity;

import java.util.List;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Data;

/**
 * @author miemie
 * @since 2019-11-27
 */
@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;
}

2mapper

package com.zs.testmybatisplus.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zs.testmybatisplus.entity.Child;

/**
 * @author miemie
 * @since 2019-11-27
 */
@Mapper
@Component
public interface ChildMapper extends BaseMapper<Child> {

    Child selectLinkById(@Param("id") Long id);

    @Select("select * from child where lao_han_id = #{id}")
    List<Child> selectByLaoHanId(@Param("id") Long id);

    @Select("select * from child where lao_ma_id = #{id}")
    List<Child> selectByLaoMaId(@Param("id") Long id);
}

package com.zs.testmybatisplus.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zs.testmybatisplus.entity.Man;


/**
 * @author miemie
 * @since 2019-11-27
 */
@Mapper
@Component
public interface ManMapper extends BaseMapper<Man> {

    Man selectLinkById(@Param("id") Long id);
}

package com.zs.testmybatisplus.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zs.testmybatisplus.entity.Woman;

/**
 * @author miemie
 * @since 2019-11-27
 */

@Mapper
@Component
public interface WomanMapper extends BaseMapper<Woman> {

    Woman selectLinkById(@Param("id") Long id);
}

3XML

<?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.zs.testmybatisplus.mapper.ChildMapper">
<!--    id: 当前命名空间中的一个唯一标识,用于标识一个结果映射
        type: 类的完全限定名, 或者一个类型别名-->
    <resultMap id="c_r" type="com.zs.testmybatisplus.entity.Child">
<!--        id 和 result 元素都将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段
            id 元素对应的属性会被标记为对象的标识符。 这样可以提高整体的性能,尤其是进行缓存和嵌套结果映射(也就是连接映射)的时候-->
        <!--     column: 数据库中的列名,或者是列的别名。
         property: 实体类的属性-->
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="laoHanId" column="lao_han_id"/>
        <result property="laoMaId" column="lao_ma_id"/>

        <!--嵌套 Select 查询:通过执行另外一个 SQL 映射语句来加载期望的复杂类型。-->
<!--        2: ====>  Preparing: SELECT * FROM man WHERE id=?-->
        <association property="laoHan" column="lao_han_id"
                     select="com.zs.testmybatisplus.mapper.ManMapper.selectById"/>
<!--        3: ====>  Preparing: SELECT id,name,lao_gong_id FROM woman WHERE id=?-->
        <association property="laoMa" column="lao_ma_id"
                     select="com.zs.testmybatisplus.mapper.WomanMapper.selectById"/>
    </resultMap>
<!--    1: ==>  Preparing: select * from child where id = ?-->
    <select id="selectLinkById" resultMap="c_r">
        select *
        from child
        where id = #{id}
    </select>
</mapper>
<?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.zs.testmybatisplus.mapper.WomanMapper">

    <resultMap id="w_r" type="com.zs.testmybatisplus.entity.Woman">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="laoGongId" column="lao_gong_id"/>

<!--        2: ====>  Preparing: SELECT * FROM man WHERE id=?
               ====> Parameters: 1(Long)-->
        <association property="laoGong" column="lao_gong_id"
                     select="com.zs.testmybatisplus.mapper.ManMapper.selectById"/>

<!--        3: ====>  Preparing: select * from child where lao_ma_id = ?
               ====> Parameters: 1(Long)-->
        <collection property="waWa" column="id"
                    select="com.zs.testmybatisplus.mapper.ChildMapper.selectByLaoMaId"/>
    </resultMap>
<!--1: ==>  Preparing: select * from woman where id = ?
       ==> Parameters: 1(Long)
-->
    <select id="selectLinkById" resultMap="w_r">
        select *
        from woman
        where id = #{id}
    </select>
</mapper>
<?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.zs.testmybatisplus.mapper.ManMapper">

    <resultMap id="m_b" type="com.zs.testmybatisplus.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="com.zs.testmybatisplus.entity.Man" extends="m_b">
<!--        2: ====>  Preparing: SELECT id,name,lao_gong_id FROM woman WHERE id=?
               ====> Parameters: 1(Long)-->
<!--        select	它会从 column 属性指定的列中检索数据,作为参数传递给目标 select 语句-->
        <association property="laoPo" column="lao_po_id"
                     select="com.zs.testmybatisplus.mapper.WomanMapper.selectById"/>
<!--        3: ====>  Preparing: select * from child where lao_han_id = ?
               ====> Parameters: 2(Long)-->
        <collection property="waWa" column="id"
                    select="com.zs.testmybatisplus.mapper.ChildMapper.selectByLaoHanId"/>
    </resultMap>
<!--1: ==>  Preparing: select * from man where id = ?
       ==> Parameters: 2(Long)-->
    <select id="selectLinkById" resultMap="m_r">
        select *
        from man
        where id = #{id}
    </select>
</mapper>

4测试类

package com.zs.testmybatisplus;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import javax.annotation.Resource;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


import com.zs.testmybatisplus.entity.Child;
import com.zs.testmybatisplus.entity.Man;
import com.zs.testmybatisplus.entity.Woman;
import com.zs.testmybatisplus.mapper.ChildMapper;
import com.zs.testmybatisplus.mapper.ManMapper;
import com.zs.testmybatisplus.mapper.WomanMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * @author miemie
 * @since 2019-11-27
 */
@Slf4j
@SpringBootTest
class ResultmapTest {

    @Resource
    private ChildMapper childMapper;
    @Resource
    private ManMapper manMapper;
    @Resource
    private WomanMapper womanMapper;

    @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();
    }

    @Test
    void t_m() {
        final Man man = manMapper.selectLinkById(2L);
        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());
    }

    @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());
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值