Mybatis CRUD中万能Map的用法及优势

当实体类或者数据库中的表,字段或者参数过多,我们应当考虑使用HashMap!

//万能Map
int addUser2(Map<String,Object> map);
<!--对象中的属性,可以直接取出来  传递map中的key-->
<insert >
insert into user (id,name,pwd) values (#{userid},#{username},#{userpwd})
</insert>
@Test
public void addUser2Test(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("userid",5);
map.put("username","lucy");
map.put("userpwd","123123");
mapper.addUser2(map);
sqlSession.commit();
sqlSession.close();
}
Map传递参数,直接在sql中取出key即可!【parameterType="map"】
对象传递参数,直接在sql中取对象的属性即可!【parameterType="Object"】
只有一个基本类型参数的情况下,可以直接在sql中取到!