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这一列的数据了