查询返回值是map类型的一条数据
1 首先在接口中写方法
public interface EmployeeMapper {
//返回一条记录的map;key就是列名,值就是对应的值
public Map<String, Object> getEmpByIdReturnMap(Integer id);
}
2 在映射文件xml配置方法
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id); -->
<select id="getEmpByIdReturnMap" resultType="map">
select * from tbl_employee where id=#{id}
</select>
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<String, Object> 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<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean
//@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
@MapKey("email")
public Map<String, Employee> getEmpByLastNameLikeReturnMap(String email);
}
2 在映射文件xml配置方法
<!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName); -->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where email like #{email}
</select>
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<String, Employee> 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();
}
}