MyBatisMap妙用和模糊查询
万能Map
假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用map
//万能map
int addUser2(Map<String, Object> map);
<insert id="addUser2" parameterType="map">
insert into mybatis.user (id, name, pwd) values (#{id}, #{username}, #{password});
</insert>
@Test
public void addUser2() {
try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "6");
map.put("username", "crs");
map.put("password", "123456");
userMapper.addUser2(map);
sqlSession.commit();
}
}
Map传递参数,直接在SQL中取Key即可![parameterType="map"]
对象传递参数,直接在sql中取对象的属性即可[parameterType="object"]
只有一个基本类型参数的情况下,可以直接在SQL中取到!
多个参数使用map,或者注解!
模糊查询
1.Java代码执行的时候,传递通配符%%
List<User> userList = userMapper.getUserLike("%cr%");
2.在sql拼接中使用通配符
select * from mybatis.user where name like "%"#{value}"%";
浙公网安备 33010602011771号