mybatis 關聯查詢 一對一 一對多

</pre><pre name="code" class="java">

當前對象的xml設置中

返回類型

    <resultMap id="releaseUnitMap" type="com.afocus.fa.release.model.ReleaseUnit">
        <result property="id" column="ID"></result>
        <result property="name" column="NAME"></result>
        <result property="platformId" column="PLATFORM_ID"></result>
        <result property="categoryId" column="CATEGORY_ID"></result>
        <result property="brandId" column="BRAND_ID"></result>
        <result property="goodsName" column="GOODS_NAME"></result>
        <result property="originalPrice" column="ORIGINAL_PRICE"></result>
        <result property="presentPrice" column="PRESENT_PRICE"></result>
        <result property="type" column="TYPE"></result>
        <result property="status" column="STATUS"></result>
        <result property="createTime" column="CREATE_TIME"></result>
        <result property="updateTime" column="UPDATE_TIME"></result>
        <result property="creatorId" column="CREATOR_ID"></result>
        <result property="creatorName" column="CREATOR_NAME"></result>
        <result property="categoryName" column="categoryName"></result>
        <result property="brandName" column="brandName"></result>
        <result property="platformName" column="platformName"></result>
        <!-- 其他定義字段 -->
        <result property="searchStartTime" column=""></result>
        <result property="searchEndTime" column=""></result>
        <!-- 投放地域數據 通用  一對一-->
        <association property="releaseArea"
            javaType="com.afocus.fa.release.model.ReleaseArea"
            resultMap="com.afocus.fa.release.dao.ReleaseAreaDao.globalReleaseArea"/>
        <!-- 天貓直通車投放方式數據 一對多 -->
        <collection property="tmztcReleaseWays"
            ofType="com.afocus.fa.release.model.ReleaseTmztcWay"
            resultMap="com.afocus.fa.release.dao.ReleaseTmztcWayDao.globalReleaseTmztcWay"/>
        <!-- 天貓直通車投放時間數據 -->
        <collection property="tmztcReleaseTimes"
            ofType="com.afocus.fa.release.model.ReleaseTmztcTime"
            resultMap="com.afocus.fa.release.dao.ReleaseTmztcTimeDao.globalReleaseTmztcTime"/>
        

    </resultMap>


查詢語句

<select id="loadTmztcInformation" resultMap="releaseUnitMap">        
        SELECT <include refid="field"/>,
            MC.NAME categoryName,MB.NAME brandName,MP.NAME platformName,
            <include refid="com.afocus.fa.release.dao.ReleaseTmztcWayDao.globalField"/>,
            <include refid="com.afocus.fa.release.dao.ReleaseTmztcTimeDao.globalField"/>,
            <include refid="com.afocus.fa.release.dao.ReleaseAreaDao.globalField"/>
        FROM FA_RELEASE_UNIT RU
        LEFT JOIN (SELECT * FROM FA_MANAGE_CATEGORY CA WHERE CA.STATUS != -1) MC
         ON RU.CATEGORY_ID = MC.ID
        LEFT JOIN (SELECT * FROM FA_MANAGE_BRAND BR WHERE BR.STATUS !=1) MB
         ON RU.BRAND_ID = MB.ID
        LEFT JOIN (SELECT * FROM FA_MANAGE_PLATFORM PL WHERE PL.STATUS!=-1) MP
        ON MP.ID =RU.PLATFORM_ID
        LEFT JOIN (SELECT * FROM FA_RELEASE_TMZTC_WAY WA WHERE WA.STATUS !=-1) RTW
         ON RTW.UNIT_ID=RU.ID
        LEFT JOIN (SELECT * FROM FA_RELEASE_TMZTC_TIME T WHERE T.STATUS !=-1) RTT
        ON RTT.UNIT_ID=RU.ID
        LEFT JOIN (SELECT * FROM FA_RELEASE_AREA A WHERE A.STATUS !=-1) RA
        ON RA.UNIT_ID=RU.ID
        <include refid="dynamicWhere" />
    </select>

當前對象的model中

    private ReleaseArea releaseArea;
    
    private List<ReleaseTmztcWay> tmztcReleaseWays;

    private List<ReleaseTmztcTime> tmztcReleaseTimes;   


投放地域的xml 注意對應樣色相同的相互對應

    <sql id="globalField">
        RA.ID RA_ID, RA.UNIT_ID RA_UNIT_ID, RA.PROVINCE, RA.TYPE RA_TYPE, RA.STATUS RA_STATUS,
        RA.CREATE_TIME RA_CREATE_TIME, RA.UPDATE_TIME RA_UPDATE_TIME, RA.CREATOR_ID RA_CREATOR_ID, RA.CREATOR_NAME RA_CREATOR_NAME
    </sql>

<resultMap id="globalReleaseArea" type="com.afocus.fa.release.model.ReleaseArea">
        <result property="id" column="RA_ID"></result>
        <result property="unitId" column="RA_UNIT_ID"></result>
        <result property="province" column="PROVINCE"></result>
        <result property="type" column="RA_TYPE"></result>
        <result property="status" column="RA_STATUS"></result>
        <result property="createTime" column="RA_CREATE_TIME"></result>
        <result property="updateTime" column="RA_UPDATE_TIME"></result>
        <result property="creatorId" column="RA_CREATOR_ID"></result>
        <result property="creatorName" column="RA_CREATOR_NAME"></result>
    </resultMap>
   

其他的xml也就和投放地域的設置相同





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章