mybatis map取值
时间: 2023-05-25 18:04:34 浏览: 417
MyBatis中使用map取值的方式有两种:
1. 使用ognl表达式进行取值
可以通过将map的key作为属性名称,在ognl表达式中进行调用。例如:
```
<select id="selectByMap" resultType="User">
select * from user where username = #{map.username} and password = #{map.password}
</select>
```
在调用时需要将map作为参数传递进去:
```
Map<String, Object> map = new HashMap<>();
map.put("username", "admin");
map.put("password", "123456");
userMapper.selectByMap(map);
```
2. 使用注解中的@Param注解进行取值
可以通过在map的调用中使用@Param注解来取值。例如:
```
<select id="selectByMap" resultType="User">
select * from user where username = #{username} and password = #{password}
</select>
```
在调用时需要将map作为参数传递进去,并使用@Param注解将map的key与方法中的参数进行绑定:
```
Map<String, Object> map = new HashMap<>();
map.put("username", "admin");
map.put("password", "123456");
userMapper.selectByMap(@Param("map") Map<String, Object> map);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)