万能Map

作为parameter:将map的key和value映射到parameter
  List<User> getUserListByLimit(Map<String,Object> map);
  <select id="getUserListByLimit" parameterType="map" resultType="user">
    select * from user where id limit #{startIndex},#{pageSize}
  </select>
  @Test
  public void getUserListByLimit() {
      UserMapper mapper = session.getMapper(UserMapper.class);
      HashMap<String, Object> map = new HashMap<>();
      map.put("startIndex", 0);
      map.put("pageSize", 2);
      for (User user : mapper.getUserListByLimit(map)) {
         logger.info(user);
      }
      session.close();
  }

作为result:查询结果为一个对象
    HashMap<String, Object> getStudentById(int id);
    <select id="getStudentById" resultType="map" parameterType="int">
    select * from student where id = #{id}
    </select>

    @Test
    public void getStudentById(){
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        HashMap<String, Object> map = mapper.getStudentById(1);
        for (String s : map.keySet()) {
            System.out.println(s+" : "+map.get(s));
        }
    }
 

  

posted @ 2020-12-01 17:18  nbg  阅读(48)  评论(0编辑  收藏  举报