用一个VO类做测试,要从三个表中查到数据
@Data
public class SkuInfoAndImagesVO {
private String skuName;
private SkuCommentEntity comment;
private List<SkuImagesEntity> images;
}
为VO类写一个结果映射
java类就用association, javaType指定类型
集合用collection,ofType指定集合内数据的类型
<resultMap id="test" type="com.zhangxueliang.njithome.product.vo.SkuInfoAndImagesVO">
<result property="skuName" column="sku_name"/>
<association property="comment" javaType="com.zhangxueliang.njithome.product.entity.SkuCommentEntity" >
<result property="memberNickName" column="member_nick_name"></result>
</association>
<collection property="images" ofType="com.zhangxueliang.njithome.product.entity.SkuImagesEntity">
<result property="id" column="id"/>
<result property="skuId" column="sku_id"/>
<result property="imgUrl" column="img_url"/>
<result property="imgSort" column="img_sort"/>
<result property="defaultImg" column="default_img"/>
</collection>
</resultMap>
SQL语句
查询的时候不要有冗余字段
<select id="getone" resultMap="test">
select s.sku_name,c.member_nick_name, i.img_url,i.id,i.sku_id,i.img_sort,i.default_img from pms_sku_info s , pms_sku_images i,pms_sku_comment c where s.sku_id = i.sku_id =c.sku_id and s.sku_id = #{id}
</select>