mybatis嵌套resultMap的映射错误问题记录

昨天遇到的一个mybatis嵌套映射的问题,今天终于在mybatis的issue中找到类似的问题,并成功解决了它。
解决的issue:嵌套<collection><association>返回意外结果: https://github.com/mybatis/mybatis-3/issues/1835

定义的映射方式

<resultMap id="ExtResultMap" type="com.xxx.SamplePageDTO">
    <association property="sample" columnPrefix="sp_" resultMap="BaseResultMap"/>
    <collection property="productList" columnPrefix="pl_"
                resultMap="com.xxx.SampleProductMapper.ExtResultMap"/>
</resultMap>

<!-- com.xxx.SampleProductMapper.ExtResultMap -->
<resultMap id="ExtResultMap" type="com.xxx.SampleProductDTO">
    <association property="product" columnPrefix="p_"
                 resultMap="com.xxx.ProductMapper.productInfoMap"/>
    <collection property="applyImage" columnPrefix="spi_"
                resultMap="com.xxx.SampleProductImageMapper.BaseResultMap"/>
</resultMap>

定义的实体类

public class SamplePageDTO {
    private SamplePage sample;
    private List<SampleProductDTO> productList;
}
public class SampleProductDTO implements Serializable {

    private static final long serialVersionUID = 6392533762543411354L;
    
    private ProductInfo product;
    private List<SampleProductImage> applyImage;
}

预期结果

{
    "sample":Object{...},
    "productList":[
        {
            "product":Object1{...},
            "applyImage":Array[2]{1,2}
        },
        {
            "product":Object2{...},
            "applyImage":Array[2]{3,4}
        }
    ]
}

实际结果

[
    {
        "sample":Object{...},
        "productList":[
            {
                "product":Object1{...},
                "applyImage":Array[1]{1}
            }
        ]
    },
    {
        "sample":Object{...},
        "productList":[
            {
                "product":Object1{...},
                "applyImage":Array[1]{2}
            }
        ]
    },
    {
        "sample":Object{...},
        "productList":[
            {
                "product":Object2{...},
                "applyImage":Array[1]{3}
            }
        ]
    },
    {
        "sample":Object{...},
        "productList":[
            {
                "product":Object2{...},
                "applyImage":Array[1]{4}
            }
        ]
    }
]

这个结果还导致了一个mybatis的映射异常,因为预期获得一个DTO的对象,实际则是获得了4个DTO对象。当我把预期结果改为一个列表时,得到的就是上面那样的映射结果,很显然与我们预期的不同,mybatis并没有为我自动归档成一个对象,而是按照SQL执行结果的行数为每一个DTO都分配了一个子对象。

解决方式

根据issue中提供的解决方式,在需要归档的地方加上一个<id>标签,使得mybatis可以为我们分组归档。

那么上述定义的ResultMap将修改成下面的方式

<resultMap id="ExtResultMap" type="com.xxx.SamplePageDTO">
	<!-- 添加一个只有按列分组的属性,而不添加实体属性(因为没有定义对应的实体属性) --->
	<id column="sp_id"/>
    <association property="sample" columnPrefix="sp_" resultMap="BaseResultMap"/>
    <collection property="productList" columnPrefix="pl_"
                resultMap="com.xxx.SampleProductMapper.ExtResultMap"/>
</resultMap>

<!-- com.xxx.SampleProductMapper.ExtResultMap -->
<resultMap id="ExtResultMap" type="com.xxx.SampleProductDTO">
	<!-- 添加一个只有按列分组的属性,而不添加实体属性(因为没有定义对应的实体属性) --->
	<id column="p_id"/>
    <association property="product" columnPrefix="p_"
                 resultMap="com.xxx.ProductMapper.productInfoMap"/>
    <collection property="applyImage" columnPrefix="spi_"
                resultMap="com.xxx.SampleProductImageMapper.BaseResultMap"/>
</resultMap>

经过实验,我成功得到了预期的结果,that’s good!

将上述问题记录希望可以帮到你!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis嵌套映射(Nested Mapping)是MyBatis框架中用于处理复杂数据结构和关联查询的一种高级映射技术。在传统的一对一、一对多和多对多的关系映射中,当数据模型包含嵌套的对象或者集合时,MyBatis嵌套映射就显得尤为重要。 嵌套映射允许你在SQL查询结果集中直接获取嵌套的对象或集合,而不需要显式地进行多次对象操作。它通常通过`<select>`标签中的`resultType`或`resultMap`元素来配置,同时可能涉及到`association`、`collection`、`discriminator`等元素来指定关联关系的处理方式。 例如,如果你有一个`User`对象,它有一个`Address`对象作为嵌套,你可以这样配置: ```xml <select id="selectUserWithAddress" resultType="com.example.User"> SELECT * FROM user LEFT JOIN address ON user.id = address.user_id </select> <resultMap id="userResultMap" type="com.example.User"> <id property="id" column="user_id"/> <result property="name" column="user_name"/> <!-- 使用association来映射嵌套的Address --> <association property="address" javaType="com.example.Address" select="selectAddressById"> <id property="id" column="address_id"/> <result property="street" column="street"/> </association> </resultMap> <select id="selectAddressById" parameterType="int" resultType="com.example.Address"> SELECT * FROM address WHERE id = #{id} </select> ``` 在这个例子中,`selectUserWithAddress`方法会返回一个包含`User`对象的结果集,其中的`address`属性会被自动解析为从`selectAddressById`查询得到的`Address`对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值