mybatis返回Map

返回多条数据的时候,用map作为返回值,key保存一些标志性字符串,value保存具体的实体类或者是hashMap都很好用。

mapper.java

//Map:键是这条记录的主键,值是记录封装后的javaBean
//@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
@MapKey("id")
public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);

/**
* 获取部门人数,最高工资、最低工资等信息
*/
@MapKey("departmentName")
Map<String, Map<String,Object>> getDepartmentInfo(List<Department> departmentList);

对应的xml

<select id="getEmpByLastNameLikeReturnMap" resultType="employee">
         select * from employee where last_name like #{lastName}
 select>


 <select id="getDepartmentInfo" resultType="java.util.Map">
        select department_name,count(*),max,min
        from employee
        where department_name in (
        <foreach collection="departmentList" item="department" separator=",">
            #{department}
        foreach>
        )
        group by department_name;
select>

你可能感兴趣的:(mybatis,mybatis,java)