开发中mybatis利用map添加到数据库

接口中:

1 int addUser2(Map<String,Object> map);
View Code

mapper.xml中:

1 <insert id="addUser2" parameterType="map">
2         insert into mybatis.user (id, name, pwd) values (#{userId}, #{userName}, #{userPwd});
3     </insert>
View Code

测试方法中:

 1     @Test
 2     public void addUser2(){
 3         SqlSession sqlSession = MyBatisUtils.getSqlSession();
 4         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
 5 
 6         Map<String, Object> map = new HashMap<String, Object>();
 7         map.put("userId",6);
 8         map.put("userName","map的使用者");
 9         map.put("userPwd","123123");
10 
11         mapper.addUser2(map);
12         sqlSession.commit();
13         sqlSession.close();
14     }
View Code

 

posted @ 2021-08-19 21:35  现在开始JAVA  阅读(208)  评论(0)    收藏  举报