mysql 返回map类型数据_使用Map接收返回数据库的数据
查询返回值是map类型的一条数据
1 首先在接口中写方法
public interface EmployeeMapper {
//返回一条记录的map;key就是列名,值就是对应的值
public Map getEmpByIdReturnMap(Integer id);
}
2 在映射文件xml配置方法
select * from tbl_employee where id=#{id}
3在junit方法里进行测试
@Test
public void test04() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Map map = mapper.getEmpByIdReturnMap(1);
System.out.println(map);
String email=(String) map.get("email");
System.out.println(email);
}finally{
openSession.close();
}
}
查询返回值是map类型的多条数据
1 首先在接口中写方法
public interface EmployeeMapper {
//多条记录封装一个map:Map:键是这条记录的主键,值是记录封装后的javaBean
//@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
@MapKey("email")
public Map getEmpByLastNameLikeReturnMap(String email);
}
2 在映射文件xml配置方法
select * from tbl_employee where email like #{email}
3在junit方法里进行测试
@Test
public void test04() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Map map = mapper.getEmpByLastNameLikeReturnMap("%e%");
System.out.println(map);
System.out.println("第一条记录为:"+map.get("jerry6@atguigu.com"));
System.out.println("第一条记录为:"+map.get("jerry6@atguigu.com").getEmail());
}finally{
openSession.close();
}
}
查询返回值是map类型的一条数据 1 首先在接口中写方法 public interface EmployeeMapper { //返回一条记录的map;key就是列名,值就是对应的值 public Map getEmpByIdReturnMap(Integer id); } 2 在映射文件xml配置方法 select * from tbl_employee where id=#{id} 3在junit方法里进行测试 @Test public void test04() throws IOException{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //1、获取到的SqlSession不会自动提交数据 SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Map map = mapper.getEmpByIdReturnMap(1); System.out.println(map); String email=(String) map.get("email"); System.out.println(email); }finally{ openSession.close(); } } 查询返回值是map类型的多条数据 1 首先在接口中写方法 public interface EmployeeMapper { //多条记录封装一个map:Map:键是这条记录的主键,值是记录封装后的javaBean //@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key @MapKey("email") public Map getEmpByLastNameLikeReturnMap(String email); } 2 在映射文件xml配置方法 select * from tbl_employee where email like #{email} 3在junit方法里进行测试 @Test public void test04() throws IOException{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //1、获取到的SqlSession不会自动提交数据 SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Map map = mapper.getEmpByLastNameLikeReturnMap("%e%"); System.out.println(map); System.out.println("第一条记录为:"+map.get("jerry6@atguigu.com")); System.out.println("第一条记录为:"+map.get("jerry6@atguigu.com").getEmail()); }finally{ openSession.close(); } }