MyBatis 中@MapKey注解的使用

当我们需要将多个数据 存放在一个HashMap中的时候 我们就可以使用@MapKey注解。

@MapKey("no")
  HashMapqueryStudentsByHashMap();
// 查询多个学生的并存放在HashMap中
   public static void queryStudentsByHashMap() throws IOException {

	      Reader reader = Resources.getResourceAsReader("config.xml");
	
	      SqlSessionFactory sqlSessionFactory = new  SqlSessionFactoryBuilder().build(reader);
	      
	      SqlSession session = sqlSessionFactory.openSession();
	     /* 1.实现接口
	      * 2.然后接口文件的名称 与 xml中的namespace形成映射 自动绑定配置
	      */
	      StudentMapper  studentmapper = session.getMapper(StudentMapper.class);
	      
	     
	  HashMap map=   studentmapper.queryStudentsByHashMap();
	  //map 遍历
	    for (Map.Entry entry : map.entrySet()) {
          System.out.println(entry.getKey() + ": " + entry.getValue());
      }
	      session.close();
}

结果:

MyBatis 中@MapKey注解的使用_第1张图片

你可能感兴趣的:(MyBatis)