您当前的位置: 首页 >  mybatis

java持续实践

暂无认证

  • 4浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

mybatis在resultMap的type使用Map来接收值,避免建立DTO实体类

java持续实践 发布时间:2018-09-28 11:21:22 ,浏览量:4

  • 开发环境
  • jdk 1.8
  • mybatis版本3.2.5
Dao的mapper

定义了一个查询 selectSpecialtyFormMap, 查询的结果用resultMap的specialFormResultGetmap接收.

    
        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
        
            and t4.specialty=#{code,jdbcType=VARCHAR}
        
        ORDER by t1.CODE asc

specialFormResultGetmap定义如下 . 注意重点是type要用java.util.Map. 即查询到的结果用map来接收. map的键为result标签的property的内容, map的值为column列中查询出来的值


        
        
        
        
        

Dao层的接口方法

方法的返回值用list来接收, 查询到的每一行的结果为list的一个元素. list泛型为Map集合,map集合的泛型均为Obeject和Object . 方法的参数,根据业务需求定义,此处的参数用map.

List selectSpecialtyFormMap(HashMap map);
Service层
 public List selectSpecialtyFormMap(HashMap ma) {
        return staffInfoDao.selectSpecialtyFormMap(ma);
    }
Controller层

在Controller层 List 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 ma = new HashMap();
        ma.put("code",3);
        List maps = staffInfoService.selectSpecialtyFormMap(ma);
        json.put("list", maps);
        return json;
	}

通过debug查看, maps的结果,可以看到, 在list集合的maps中, 存储了四个map集合. 在每一个map集合中,有5个键,分别是在resultmap中定义的property的值. value为查询出来的结果.

查询到的json数据如下

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0399s