- 开发环境
- jdk 1.8
- mybatis版本3.2.5
Dao的mapper
定义了一个查询 selectSpecialtyFormMap, 查询的结果用resultMap的specialFormResultGetmap接收.
<select id="selectSpecialtyFormMap" resultMap="specialFormResultGetmap" parameterType="java.util.Map">
SELECT
t1.DepartmentName ,t3.AccountID,t3.RealName,t3.Sexuality,t5.text
from
t_hrm_departmentinfo t1 LEFT JOIN
t_hrm_accountfordepartment t2 on t1.DepartmentID=t2.DepartmentID LEFT JOIN
t_hrm_staffinfo t3 on t2.accountid=t3.AccountID LEFT JOIN
t_hrm_staffinfo_patch t4 on t3.patch_id=t4.patch_id LEFT JOIN
t_dictionary t5 on t5.`code`=t4.specialty
WHERE t5.type_code="0090" and t3.status=0
<if test="code !=null and code !=''">
and t4.specialty=#{code,jdbcType=VARCHAR}
</if>
ORDER by t1.CODE asc
</select>
specialFormResultGetmap定义如下 . 注意重点是type要用java.util.Map.
即查询到的结果用map来接收.
map的键为result标签的property的内容, map的值为column列中查询出来的值
<resultMap id="specialFormResultGetmap" type="java.util.Map">
<result property="departmentName" column="DepartmentName"/>
<result property="accountId" column="AccountID"/>
<result property="realName" column="RealName"/>
<result property="sexuality" column="Sexuality"/>
<result property="specialty" column="text"/>
</resultMap>
Dao层的接口方法
方法的返回值用list来接收, 查询到的每一行的结果为list的一个元素.
list泛型为Map集合,map集合的泛型均为Obeject和Object . 方法的参数,根据业务需求定义,此处的参数用map.
List<Map<Object,Object>> selectSpecialtyFormMap(HashMap<String, Object> map);
Service层
public List<Map<Object, Object>> selectSpecialtyFormMap(HashMap<String, Object> ma) {
return staffInfoDao.selectSpecialtyFormMap(ma);
}
Controller层
在Controller层List<Map<Object, Object>> maps = staffInfoService.selectSpecialtyFormMap(ma);
这一行的代码,调用service层代码. 得到了查询的list.
map的键为属性的名称,map的值为查询到的值
@RequestMapping(value = "mapTest",method=RequestMethod.GET,produces={"application/json;charset=UTF-8"})
public @ResponseBody JSONObject mapTest() {
JSONObject json = new JSONObject();
ResultDTO result = new ResultDTO();
HashMap<String, Object> ma = new HashMap<>();
ma.put("code",3);
List<Map<Object, Object>> maps = staffInfoService.selectSpecialtyFormMap(ma);
json.put("list", maps);
return json;
}
通过debug查看, maps的结果,可以看到, 在list集合的maps中, 存储了四个map集合.
在每一个map集合中,有5个键,分别是在resultmap中定义的property的值. value为查询出来的结果.
查询到的json数据如下