Mybatis如何通過xml傳入集合到實體中

collection用法:

<resultMap id="classifyMap" type="com.xxx.xx.x.x.x.ClassifyList">
        <id property="id" column="id"/>
        <result property="classifyTitle" column="classify_title"/>
        <result property="classifyDesc" column="classify_desc"/>
        <collection property="seriesList" ofType="com.xxx.xx.x.x.x.ArticleEntity">
            <id property="id" column="id"/>
            <result property="title" column="title"/>
            <result property="desc" column="desc"/>
            <result property="imageUrl" column="image_url"/>
        </collection>
 </resultMap>

如果是非自定義collection:注意ofType中的值

<resultMap id="getArticleEsResultMap" type="com.xxx.xx.x.ArticleInfoEntity">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <collection property="tagIdList" ofType="int" javaType="list">
            <result column="tagId" />
        </collection>
    </resultMap>

columnPrefix用法:作為列名前綴

<resultMap id="homework" type="com.xxx.xx.x.HomeworkEntity" autoMapping="true">
        <id property="id" column="id"/>
        <collection property="homeworkItemEntityList" ofType="com.xxx.xx.x.HomeworkItemEntity" columnPrefix="item_" autoMapping="true"/>
</resultMap>

子查詢用法:內(nèi)嵌子查詢(獲取單詞釋義)

 <resultMap id="getWordListResultMap" type="com.qzh.entity.GetCatalogWordListEn">
        <result property="wordId" column="id"/>
        <result property="word" column="word"/>
        <collection property="meanings" column="id"
                    ofType="com.zhl.res.pojo.study.english.dict.MeaningsEn" select="getMeanings"/>
</resultMap>


<resultMap id="meanings" type="com.qzh.dict.MeaningsEn">
        <result property="pos" column="pos"/>
       <collection property="meaning" column="meaning" ofType="string" javaType="list">
            <result column="meaning" />
        </collection>
</resultMap>

 <select id="getCatalogWordList" resultMap="getWordListResultMap">
        select  id ,word from book_word a
        where a.`status` = 0
        and a.catalog_id in
        <foreach collection="list" open="(" separator="," close=")" item="item">
          #{item}
        </foreach>
        order by a.sort
</select>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容