resultmap为list_Mybatis查询结果返回Map、List

1.当查询结果为单条记录时,可采用如下方式:

SELECT id, username, age FROM user WHERE id = #{id}

返回的Map key为字段名,value为字段值。

@Test

public void findUserResultMap(){

HashMap map = userMapper.findUserResultMap(1);

System.out.println(map.get("id"));

System.out.println(map.get("age"));

System.out.println(map.get("username"));

}

2.当查询结果为多条记录时,可采用如下方式:

返回List

select id, username, age from user

@Test

public void findAllResultListMap(){

List listMap = userMapper.findAllResultListMap();

for(int i=0; i

HashMap map = listMap.get(i);

System.out.print(map.get("id")+" ");

System.out.print(map.get("age")+" ");

System.out.print(map.get("username")+" ");

System.out.println();

}

}

注意:此时查出的是一个List集合,但是resultType写的是集合中元素的类型;

返回Map   注:User为实体类。

select * from user

@MapKey("id")

Map findAllResultUser();

现在的resultType是Map中value的类型,使用注解@MapKey来指定Map的key

返回的Map的key是User的id,value是User对象

  • 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返回结果类型为集合类型(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、付费专栏及课程。

余额充值