欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis 返回 (List < Map < List > >)格式数据

程序员文章站 2022-06-30 19:18:22
...

需求:从数据库返回一对多数据
问题:通过查询得知,mybatis是无法直接返回此种格式的
解决方案:使用GROUP_CONCAT以json格式拼接字符串,返回json字符串,然后使用fastjson解析成list。

 SELECT
    w.user_id userId,
    w.data_time dataTime,
    u.user_name userName,
    concat('[',GROUP_CONCAT(concat('{"relId\":"',w.rel_id,'","proId":"',w.pro_id,'","proName":"',p.pro_name,'","parentProName":"',p1.pro_name,'","workTime":"',w.work_time,'","workRemark":"',w.work_remark,'","status":"',w.status
    ,'","updateTime":"',w.update_time,'"}')),']') workTimeListStr
    FROM
    pro_staff_work_time w left join pro_project_info p on p.pro_id = w.pro_id
    left join sys_user u on w.user_id  = u.clerk_id
    left join pro_project_info p1 on p.parent_id = p1.pro_id
     group by w.user_id,w.data_time
List<ProStaffWorkTime> list = proStaffWorkTimeService.findList(proStaffWorkTime);
        for (ProStaffWorkTime p: list) {
        //先用字符串接收,再解析成list
            List<ProStaffWorkTime> proProjectInfos = JSONObject.parseArray(p.getWorkTimeListStr(), ProStaffWorkTime.class);
            p.setWorkTimeList(proProjectInfos);
        }
相关标签: java