定义一个接口,接口中的方法为getxxx,xxx为需要返回的字段
public interface GroupCountVO {
Long getGroupId();
Integer getGroupCount();
}
Repository的中查询自定义sql
@Query(value = "select group_id as groupId,count(*) as groupCount from group_table where group_id in (?1) group by group_id",nativeQuery = true)
List<GroupCountVO> findCountByGroupId(List ids);//ids为给in传参数,in的参数必须是list
将list结果转成map
List<GroupCountVO> vo = xxxxRepository.findCountByGroupId(ids);
Map<Object,Object> map = vo.stream().collect(Collectors.toMap(GroupCountVO::getGroupId,GroupCountVO::getGroupCount));