關於mybatis中一個實體類對應多個list集合且,list集合中的泛型實體類中仍然存在多個list集合寫法


這個問題困擾了我一個多星期,本來簡簡單單的查詢可能花幾個小時就能輕松解決,就是因為mybatis的映射關系沒有掌握牢固,導致時間大把大把的浪費掉了,在網上查閱了很多信息,基本上都是一個實體類對應一個集合,一對多的關系,並沒有看到一個實體類存在多個list集合的情況,本人不才,看了mybatis的文檔后依然沒有完全理解,就抱着試一試的心態,進行了下一步。進入正題:我們都知道實體類中存在一個list集合,就如下:

      <resultMap id="InsureMap" type="com.entry.insurance.PolicyOfInsurance" autoMapping="true">
            <result column="policyHolderId" property="policyHolderIds"/>
            <result column="insuredPersonId" property="insuredPersonIds"/>
            <collection property="policyList" ofType="com.entry.insurance.PolicyHolder" autoMapping="true">
                <result column="spid" property="id"/>
            </collection>
    </resultMap>

然后在查詢代碼中引用即可

    <select id="selectInsure" resultMap="InsureMap">
        SELECT  
                sp.id spid
            FROM
                SHOP_POLICYOFINSURANCE
            where
            sp.id = ‘1’
    </select>

以上代碼有刪減,僅供參考,這里需要注意的是collection中的column=spid需要和下面查詢語句中的字段要對應上,以次類推,如果存在多個list集合,情況如下:

    <resultMap id="InsureMap" type="com.entry.insurance.PolicyOfInsurance" autoMapping="true">
            <result column="policyHolderId" property="policyHolderIds"/>
            <result column="insuredPersonId" property="insuredPersonIds"/>
            <collection property="policyList" ofType="com.entry.insurance.PolicyHolder" autoMapping="true">
                <result column="spid" property="id"/>
            </collection>
            <collection property="insuredList" ofType="com.entry.insurance.InsuredPerson" autoMapping="true">
                <result column="siid" property="id"/>
                <collection property="beneficiaryList" ofType="com.entry.insurance.Beneficiary" autoMapping="true">
                    <result column="sbid" property="id"/>
                </collection>
                <collection property="contactList" ofType="com.entry.insurance.EmergencyContact" autoMapping="true">
                    <result column="seid" property="id"/>
                </collection>
                <collection property="riskInfoList" ofType="com.entry.insurance.InsuranceType" autoMapping="true">
                    <result column="sipid" property="id"/>
                </collection>
            </collection>
        </resultMap>

這里說一下情況,我在collection中嵌套了collection,目的是list集合中的實體類也存在多個list,切記一點就行,collection中的column=‘id’要與查詢語句中的字段對應。在查詢過程中還需要進行表的自連接或者左(右)連接。上述代碼不全面,有需要進一步了解的,可以留言評論。
---------------------
作者:倚樓無言
來源:CSDN
原文:https://blog.csdn.net/Jyan8001/article/details/88789621
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM