Goods实体属性:
private List<GoodsImg> goodsImgList;
private String id;
private String name;
GoodsImg实体属性:
private String id;
private String fid;
private String imgpath;
private String basepath;
第一种:(collection分页有问题,一般用于条件查询)
<resultMap id="resultGoodsMap" type="com.ceshi.model.Goods">
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<collection property="goodsImgList" ofType="com.ceshi.model.GoodsImg">
<id column="i_id" property="id" jdbcType="VARCHAR" />
<result column="i_fid" property="fid" jdbcType="VARCHAR" />
<result column="i_imgpath" property="imgpath" jdbcType="LONGVARCHAR" />
<result column="i_basepath" property="basepath" jdbcType="LONGVARCHAR" />
</collection>
</resultMap>
<!--查询商品信息-->
<select id="selectGoods" resultMap="resultGoodsMap">
SELECT g.id AS id, g.name AS name, i.id AS i_id, i.fid AS i_fid, i.imgpath AS i_imgpath, i.basepath AS i_basepath
FROM crm_goods g
LEFT JOIN crm_goods_img i ON g.id = i.fid
WHERE g.id = #{id} <!--一般用于查询具体商品信息-->
</select>
第二种:(效率低,但是可以解决collection分页问题)
<resultMap id="resultGoodsMap" type="com.ceshi.model.Goods">
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<collection property="goodsImgList" ofType="com.ceshi.model.GoodsImg" select="selectGoodsImg" column="{fid = id}"></collection>
</resultMap>
<resultMap id="resultGoodsImgMap" type="com.ceshi.model.GoodsImg">
<id column="i_id" property="id" jdbcType="VARCHAR" />
<result column="i_fid" property="fid" jdbcType="VARCHAR" />
<result column="i_imgpath" property="imgpath" jdbcType="LONGVARCHAR" />
<result column="i_basepath" property="basepath" jdbcType="LONGVARCHAR" />
</resultMap>
<!--查询商品信息-->
<select id="selectGoods" resultMap="resultGoodsMap">
SELECT g.id AS id, g.name AS name
FROM crm_goods g
</select>
<!--根据商品id查询商品图片信息-->
<select id="selectGoodsImg" resultMap="resultGoodsImgMap">
SELECT i.id AS i_id, i.fid AS i_fid, i.imgpath AS i_imgpath, i.basepath AS i_basepath
FROM crm_goods_img i
WHERE i.fid = #{fid}
</select>