数据对象含有普通属性,一对一对象,一对多对象(2种情况:单一主键和复合主键)
下面是resultMap的定义:
<resultMap id="GoodsResultMap" type="GoodsModelCustomize">
<id property="goods_cd" column="goods_cd" />
<result property="production_company_cd" column="production_company_cd" />
<result property="production_place_cd" column="production_place_cd" />
<result property="goods_nm_jp" column="goods_nm_jp" />
<result property="goods_nm_en" column="goods_nm_en" />
<result property="goods_nm_zh" column="goods_nm_zh" />
<result property="goods_type" column="goods_type" />
<result property="goods_package" column="goods_package" />
<result property="upd_dttm" column="upd_dttm" />
<association property="productioncompany" column="production_company_cd" javaType="trade.db.model.Productioncompany">
<id property="production_company_cd" column="production_company_cd"/>
<result property="company_nm_jp" column="company_nm_jp"/>
<result property="company_rnm_jp" column="company_rnm_jp"/>
</association>
<collection property="goodsnutrientList" column="goods_cd" ofType="trade.db.model.Goodsnutrient" select="getGoodsnutrient" />
<collection property="liquorgoodsccicList" column="goods_cd" ofType="trade.db.model.Liquorgoodsccic" select="getLiquorgoodsccic">
<id property="goods_cd" column="goods_cd" />
<id property="ccic_item_cd" column="ccic_item_cd"/>
<result property="ccic_item_val1" column="ccic_item_val1"/>
</collection>
<collection property="projectgoodsList" column="goods_cd" ofType="trade.db.model.Projectgoods" select="getProjectgoods">
<id property="project_goods_cd" column="project_goods_cd"/>
<result property="project_cd" column="project_cd"/>
<result property="goods_cd" column="goods_cd"/>
</collection>
</resultMap>
<resultMap id="GoodsnutrientResultMap" type="Goodsnutrient">
<id property="goods_cd" column="goods_cd" />
<id property="nutrient_item_cd" column="nutrient_item_cd" />
<result property="sort_no" column="sort_no" />
<result property="nutrient_value" column="nutrient_value" />
<result property="nutrient_nrv" column="nutrient_nrv" />
<result property="upd_dttm" column="upd_dttm" />
</resultMap>
普通属性省略说明
一对一属性productioncompany
一对多属性goodsnutrientList(复合主键,返回的复杂对象内有数据)
一对多属性liquorgoodsccicList(复合主键,返回的复杂对象内没有数据)
一对多属性projectgoodsList(单一主键,返回的复杂对象内有数据)
select相关配置 :
<select id="getGoodsnutrient" parameterType="String" resultMap="GoodsnutrientResultMap">
SELECT
m_goods_nutrient.*
FROM m_goods_nutrient
<where>
m_goods_nutrient.goods_cd = #{goods_cd}
AND m_goods_nutrient.del_flg = '0'
</where>
</select>
<select id="getLiquorgoodsccic" parameterType="String" resultType="trade.db.model.Liquorgoodsccic">
SELECT
m_liquorgoods_ccic.*
FROM m_liquorgoods_ccic
<where>
m_liquorgoods_ccic.goods_cd = #{goods_cd}
AND m_liquorgoods_ccic.del_flg = '0'
</where>
</select>
<select id="getProjectgoods" parameterType="String" resultType="trade.db.model.Projectgoods">
SELECT
t_project_goods.*
FROM t_project_goods
<where>
t_project_goods.goods_cd = #{goods_cd}
AND t_project_goods.del_flg = '0'
</where>
</select>
<select id="findByPrimaryKey" parameterType="String" resultMap="GoodsResultMap">
SELECT
m_goods.*,
m_production_company.*,
now() AS SELECT_TIME
FROM m_goods
LEFT JOIN
m_production_company
ON m_goods.production_company_cd = m_production_company.production_company_cd
AND m_production_company.del_flg = '0'
WHERE
m_goods.goods_cd = #{primary_key}
AND m_goods.del_flg = '0'
</select>
通过findByPrimaryKey方法获取普通属性和1对1对象属性
通过getGoodsnutrient方法获取1对多复合主键的属性,注意返回类型的配置为resultMap=“GoodsnutrientResultMap”(返回的对象的List属性内有数据)
通过getLiquorgoodsccic方法获取1对多复合主键的属性,此处的返回类型为 resultType=“trade.db.model.Liquorgoodsccic”(返回的对象的List属性内没有数据)
通过getProjectgoods方法获取1对多单一主键的属性,此处的返回类型为resultType=“trade.db.model.Projectgoods”(返回的对象的List属性内有数据)
Model代码:
public class GoodsModelCustomize extends Goods {
/**
* 製造会社
*/
private Productioncompany productioncompany;
public Productioncompany getProductioncompany() {
return productioncompany;
}
public void setProductioncompany(Productioncompany productioncompany) {
this.productioncompany = productioncompany;
}
/**
* 商品栄養成分
*/
private List<Goodsnutrient> goodsnutrientList;
public List<Goodsnutrient> getGoodsnutrientList() {
return goodsnutrientList;
}
public void setGoodsnutrientList(List<Goodsnutrient> goodsnutrientList) {
this.goodsnutrientList = goodsnutrientList;
}
/**
* 酒類商品CCIC
*/
private List<Liquorgoodsccic> liquorgoodsccicList;
public List<Liquorgoodsccic> getLiquorgoodsccicList() {
return liquorgoodsccicList;
}
public void setLiquorgoodsccicList(List<Liquorgoodsccic> liquorgoodsccicList) {
this.liquorgoodsccicList = liquorgoodsccicList;
}
/**
* 项目商品列表
*/
private List<Projectgoods> projectgoodsList;
public List<Projectgoods> getProjectgoodsList() {
return projectgoodsList;
}
public void setProjectgoodsList(List<Projectgoods> projectgoodsList) {
this.projectgoodsList = projectgoodsList;
}
}
普通属性继承与Goods代码省略,上述属性productioncompany,goodsnutrientList和projectgoodsList有数据,但是liquorgoodsccicList没有数据
因此,当返回对象内有1对多的List属性,同时此list为复合主键的话,推荐使用resultMap来对返回数据映射。