在使用mybatis时,经常会用到关联查询,下面来总结一下关联查询(一对一)的使用(3)
一、首先说一下一对一的关联查询,一对一关联查询建议使用标签来完成
1.第三种使用标签的方法,直截了当,看代码:
1.1 新建实体类
public class CustDetailFME {
private CustDetail custDetail;
private CustFamily custFamily;
public CustDetail getCustDetail() {
return custDetail;
}
public void setCustDetail(CustDetail custDetail) {
this.custDetail = custDetail;
}
public CustFamily getCustFamily() {
return custFamily;
}
public void setCustFamily(CustFamily custFamily) {
this.custFamily = custFamily;
}
}
1.2 主表对应的sql映射接口
CustDetailFME custDetailFMESelectByCustId(@Param("custId") String custId);
1.3 主表的sql文件
<resultMap type="com.hbcfc.yusys.cmis.customer.mapper.domain.CustDetailFME" id="CustDetailFMEMap" >
<association property="custDetail" javaType="com.hbcfc.yusys.cmis.customer.mapper.domain.CustDetail" >
<id property="custId" column="cust_id"/>
<result property="indivMobile" column="indiv_mobile"/>
<result property="regMobile" column="reg_mobile"/>
<result property="localResid" column="local_resid"/>
</association>
<association property="custDetail" javaType="com.hbcfc.yusys.cmis.customer.mapper.domain.CustFamily" columnPrefix="cd_">
<id property="custId" column="cust_id"/>
<result property="custNat" column="cust_nat"/>
<result property="familyId" column="family_id"/>
</association>
</resultMap>
<select id="custDetailFMESelectByCustId" resultMap="CustDetailFMEMap">
select a.cust_id,a.indiv_mobile,a.reg_mobile,a.localResid,b.cust_id cd_cust_id,b.cust_nat cd_cust_nat,b.family_id cd_family_id from cmis_uat1.CUST_DETAIL a , cmis_uat1.CUST_FAMILY b
where a.cust_id = b.cust_id and cust_id=#{custId}
</select>
(具体使用方法说明:
返回实体类中对象是一对一的;与第二种方法的区别在于返回的对象中可以是两个实体
)