最近敲代码遇到一个问题,需要用到双层map+list。刚开始没有想到mybatis能够转换此类参数,一般也就单层map加list,大多数需求也就能满足了。
我的需求是,按照时间统计不同班组的成绩,班组有层级关系,这里使用union all解决。
<select id="queryTrendData" resultType="DTO" parameterType="java.util.Map" >
<foreach collection="grouplist.keys" item="key" separator="UNION ALL">
select count(*) as count,ROUND(AVG(er.score),1) as score,er.train_time,#{key} as groupName
FROM
t_b_exercise_record er
left join sys_user u on er.user_id = u.id
where 1 =1
<if test="startTime != null and startTime.trim() != ''">
AND er.train_time >= #{startTime}
</if>
<if test="endTime != null and endTime.trim() != ''">
AND er.train_time <= #{endTime}
</if>
<if test="sceneId != null">
AND er.scene_id = #{sceneId}
</if>
and u.baid in
<foreach collection="grouplist[key]" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
group by DATE_FORMAT(er.train_time,'%Y%m%d') ASC
</foreach>
1、先遍历内层map的key,然后遍历key对应的list
注解: groupList是内层map的key
select #{key} 固定分组,用来在程序中分组使用。
再次感叹mybatis的强大!!!