resultMap结果返回类型

当Java实体类的属性与数据库查询的字段名称不一致时,可以利用MyBatis的resultMap来定义映射规则,实现数据的正确封装。resultMap允许我们自定义映射关系,确保查询结果能正确地映射到对应的实体对象上。
摘要由CSDN通过智能技术生成

查询结果属性名和模型类中不一样时,其结果可用resultMap接收

property实体类属性;column是sql语句查询字段名

自动映射封装的配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用MyBatisresultMap返回List时,需要确保以下几点。首先,在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中使用resultMap返回List类型的结果了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mybatis返回结果类型为集合类型(List、Map)](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、付费专栏及课程。

余额充值