mybatis中传MAP
时间: 2023-11-07 14:15:15 浏览: 91
在MyBatis中,可以使用Map作为参数来传递键值对。为了在XML文件中使用Map参数,需要进行以下步骤:
1. 将参数类型设置为Map。
2. 在XML中可以直接使用Map的键来获取对应的值。
举个例子,假设有如下的XML配置和Mapper接口方法:
```xml
<insert id="addUser" parameterType="map">
insert into user (id, name, pwd) values (#{userid}, #{username}, #{userpwd});
</insert>
```
```java
// Map的使用
int addUser(Map<String, Object> map);
```
在测试代码中,可以按照以下方式传递Map参数:
```java
@Test
public void addUser2() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("userid", 8888);
map.put("username", "java爱好者");
map.put("userpwd", "java265.com");
int result = mapper.addUser(map);
if (result > 0) {
System.out.println("添加成功!");
}
sqlSession.commit(); // 增删改一定要提交事务
sqlSession.close();
}
```
这样就可以通过Map参数在MyBatis中传递键值对了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [SSM框架的学习与应用JavaEE(第二天)Mybatis的深入学习](https://download.csdn.net/download/m0_53659738/88241458)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Mybatis中如何传入map参数呢?](https://blog.csdn.net/qq_25073223/article/details/128093957)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文