mybatis返回map<string,<map<string,object>>>
时间: 2023-12-08 11:05:00 浏览: 100
MyBatis查询返回Map示例代码
根据引用\[2\]中的信息,mybatis可以通过查询返回Map<String, Object>来实现返回Map<String, Map<String, Object>>的功能。具体的实现方式如下:
在Dao接口文件中,定义一个返回类型为Map<String, Map<String, Object>>的方法,如下所示:
```java
Map<String, Map<String, Object>> selectAll();
```
在mapper.xml文件中,使用resultMap来映射查询结果,并将查询结果封装为Map<String, Map<String, Object>>,如下所示:
```xml
<resultMap id="NestedResultMap" type="java.util.HashMap">
<id column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="user_code" jdbcType="INTEGER" property="userCode" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="user_gender" jdbcType="INTEGER" property="userGender" />
<result column="mobile_phone" jdbcType="VARCHAR" property="mobilePhone" />
<result column="birthday" jdbcType="DATE" property="birthday" />
</resultMap>
<select id="selectAll" resultMap="NestedResultMap">
select * from t_user_info
</select>
```
这样,当调用selectAll方法时,mybatis会执行相应的SQL语句,并将查询结果封装为Map<String, Map<String, Object>>返回给调用方。每个外层Map的key为用户ID,对应的value为一个内层Map,内层Map的key为字段名,value为字段值。
#### 引用[.reference_title]
- *1* *3* [mybatis 查询返回Map<String,Object> 类型](https://blog.csdn.net/yin767833376/article/details/123738647)[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^v91^control_2,239^v12^insert_chatgpt"}} ] [.reference_item]
- *2* [mybatis 查询返回List<String>、Map<String,Object>、List<Map<String,Object>>](https://blog.csdn.net/li1325169021/article/details/114003291)[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^v91^control_2,239^v12^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文