假设,我们的实体类或者数据库中的表、字段或者参数过多,我们应当考虑使用Map!
例1:update Map类型修改语句 仅修改密码
-
编写接口
//万能的Map int updateUser2(Map
map); -
编写对应的Mapper里的SQL语句
update mybatis.user set pwd = #{password} where id = #{userId}; -
测试
@Test //增改删查必须提交事务 Map仅修改密码 public void updateUser2() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Mapmap = new HashMap (); map.put("userId",1); map.put("password",222222); int res = mapper.updateUser2(map); if (res > 0) { System.out.println("修改成功"); } sqlSession.commit(); sqlSession.close(); }
例2:select Map类型查询语句 通过用户名和密码验证登录
-
编写接口
//根据NAME和PASSWORD查询用户 Map类型 User getUserByNameAndPassword(Map
map); -
编写对应的Mapper里的SQL语句
-
测试
@Test public void getUserByNameAndPassword() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Mapmap = new HashMap (); map.put("userName","张三"); map.put("passWord","123457"); User user = mapper.getUserByNameAndPassword(map); if (user != null) { //System.out.println(user); System.out.println("登陆成功!"); } else { System.out.println("用户名或密码错误!"); } sqlSession.close(); } 多个参数用Map,或者注解!
模糊查询(LIKE):
-
模糊查询怎么写?
-
Java代码执行的时候,传递通配符%value%;
List
userList = mapper.getUserLikeByName("%胡%"); -
在SQL拼接中使用通配符;
-



