MyBatis返回map数据
(1)接口中编写方法
1 //单行 2 public Map<String, Object> getEmpReturnMap(Integer id); 3 4 //多行 5 @MapKey("id") 6 public Map<Integer, Emp> getEmpReturnMaps(String lastName);
(2)编写Mapper文件
MyBatis框架为Map起别名叫map
1 <!-- public Map<String, Object> getEmpReturnMap(Integer id); --> 2 <select id="getEmpReturnMap" resultType="map"> 3 select id,last_name lastName,gender geder,email from Emp where id like #{id } 4 </select> 5 6 <!-- public Map<String, Object> getEmpReturnMaps(String lastName); --> 7 <select id="getEmpReturnMaps" resultType="com.eu.bean.Emp"> 8 select id,last_name lastName,gender geder,email from Emp where last_name like #{lastName } 9 </select>
(3)编写测试
1 @Test 2 public void testMapperReMap() throws IOException { 3 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 4 //1.获取到sqlsession 不会自动提交数据 5 SqlSession openSession = sqlSessionFactory.openSession(); 6 EmpDao mapper = openSession.getMapper(EmpDao.class); 7 8 Map<Integer, Emp> empReturnMaps = mapper.getEmpReturnMaps("%e%"); 9 System.out.println(empReturnMaps); 10 11 //手动提交数据 12 openSession.commit(); 13 openSession.close(); 14 }