Mybatis resultMap用法之查询返对象中的属性包含 List

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.x.x.XXXDao" >

    <resultMap id="ResultMap" type="com.x.x.XXXDTO">
        <result column="item" property="attribute" jdbcType="jdbcType"/>
        <result column="item" property="attribute" jdbcType="jdbcType"/>
        
        <!-- collection 标签需放在最后 -->
        <collection property="items" resultMap="ItemsMap"/>
    </resultMap>

    <resultMap id="ItemsMap" type="com.x.x.XXXDTOxxDto">
        <result column="item" property="item" jdbcType="jdbcType"/>
        <result column="item" property="item" jdbcType="jdbcType"/>
        <result column="item" property="item" jdbcType="jdbcType"/>
    </resultMap>

</mapper>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用MyBatisresultMapList时,需要确保以下几点。首先,在Mapper接口,方法的回类型应该是List类型。例如: ```java List<YourModel> getYourData(); ``` 然后,在Mapper的XML配置文件,需要使用resultMap来定义回类型。首先,在resultMap的定义添加id属性,以便在select语句引用。然后,使用result标签来定义回类型。在result标签,使用collection标签来指定回类型为List。例如: ```xml <resultMap id="yourResultMap" type="YourModel"> <id property="id" column="id" /> <result property="name" column="name" /> ... <collection property="yourListProperty" ofType="YourListElementType"> <id property="id" column="list_id" /> <result property="value" column="list_value" /> </collection> </resultMap> ``` 其,YourModel是你的模型类,yourResultMap是你为resultMap定义的id,yourListProperty是你模型类List类型属性,YourListElementType是你List元素的类型。 最后,在select语句,使用resultMap属性来引用定义好的resultMap。例如: ```xml <select id="getYourData" resultMap="yourResultMap"> SELECT * FROM your_table </select> ``` 通过以上的配置,你就可以在MyBatis使用resultMapList类型的结果了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mybatis回结果类型为集合类型(ListMap)](https://blog.csdn.net/qq_37776015/article/details/89932034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Mybatis List集合](https://blog.csdn.net/qq_55024883/article/details/127104514)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值