Mybatis进行 collection Select查询获得主表数据时某一列为null

博客主要讲述了Mybatis使用collection进行聚合查询时遇到的问题,数据库表中有数据,但主表的id列查询结果为null。通过在主表的resultMap中用result和id标签指定每列,最终成功获取到id列的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis进行 collection Select查询获得主表数据时某一列为null

问题:在使用collection查询聚合查询数据时,数据库中的表是有数据的,但查出来某一列本该显示具体数据,可却是null

主表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.giao.store.mapper.admin.AdminGoodsSpecificationMapper">

    <resultMap id="getSpecAndOptionList" type="com.giao.store.entity.GoodsSpecification">
        <collection property="goodsSpecificationOptionList"
                    column="id"
                    javaType="ArrayList"
                    ofType="com.giao.store.entity.GoodsSpecificationOption"
                    select="com.giao.store.mapper.admin.AdminGoodsSpecificationOptionMapper.getGoodsSpecificationOptionListByIdDB"/>
    </resultMap>

    <select id="getGoodsSpecificationAndOptionListDB"
            resultMap="getSpecAndOptionList">
         SELECT id,name,status,create_time
        FROM g_goods_specification
    </select>
</mapper>

子表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.giao.store.mapper.admin.AdminGoodsSpecificationOptionMapper">
    <select id="getGoodsSpecificationOptionListByIdDB"
            resultType="com.giao.store.entity.GoodsSpecificationOption">
        SELECT id,spec_id,value,create_time
        FROM g_goods_specification_option
        WHERE  spec_id=#{id}
    </select>
</mapper>

控制台打印结果:
在这里插入图片描述
主表子表的每一列的都可以查出来,但只有主表的id列查不出来

解决:
在主表的resultMap中通过result标签和id标签指定好每一列

在这里插入图片描述
控制台打印结果:
在这里插入图片描述

此时可以获取到id这一列的数据了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值