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

mybatis 返回Map<String,List<Entity>> 格式数据

程序员文章站 2022-06-30 20:49:14
...

1、创建实体类

@Data
public class DisabilityManageVo1
{
    private String disabilityManageId;

    private List<DisabilityManageVo> list;
}

2、mapper

public List<DisabilityManageVo1> selectCompletionTime1(List<String> idList);

3、XML

<resultMap id="resultMap" type="com.hanclouds.system.domain.vo.DisabilityManageVo1">
        <result column="id" property="disabilityManageId"/>
        <collection property="list" ofType="com.hanclouds.system.domain.vo.DisabilityManageVo">
            <result property="completionTime" column="completionTime"/>
            <result property="nextServerTime" column="nextServerTime"/>
        </collection>
    </resultMap>

<select id="selectCompletionTime1" parameterType="list" resultMap="resultMap">
        select
            npt.completion_time as completionTime,
            dm.id as id
        from
            nursing_personnel_task npt
        inner join
            plan_month pm on npt.rele_monthly_id = pm.id
        inner join
            disability_manage dm on pm.dis_id = dm.id
        <where>
            npt.task_states = '2'
            and DATE(npt.completion_time) &lt; DATE(NOW())
            and
            <foreach collection="list" item="id" separator="or" open="(" close=")">
                dm.id = #{id}
            </foreach>
        </where>
        ORDER BY npt.completion_time desc
    </select>

4、转换成Map<String,List<Entity>> 格式

List<DisabilityManageVo1> disabilityManageVos = disabilityManageMapper.selectCompletionTime1(idList);
            Map<String, List<DisabilityManageVo>> collect = disabilityManageVos.stream().collect(Collectors.toMap(DisabilityManageVo1::getDisabilityManageId, DisabilityManageVo1::getList));

优化查询,原来是多次查询数据库,现在是查询一次数据库返回所有数据;

相关标签: java