美文网首页
Mybatis如何通过xml传入集合到实体中

Mybatis如何通过xml传入集合到实体中

作者: QZH_2019 | 来源:发表于2020-03-06 21:19 被阅读0次

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>

子查询用法:内嵌子查询(获取单词释义)

 <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>

相关文章

网友评论

      本文标题:Mybatis如何通过xml传入集合到实体中

      本文链接:https://www.haomeiwen.com/subject/iwkorhtx.html