使用jpa中的group by返回一个数组对象

160 阅读1分钟

前言

在jpa中,使用grouo by返回一个数组对象

对象接收

1、定义一个实体类

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OperatorStatistics {

    private Long id;

    private Long count;

}

hsql写法为:

@Query(value = "select new OperatorStatistics(m.operatorId, count(*)) from MachineRoom m where m.operatorId in ?1 group by m.operatorId")
List<OperatorStatistics> getMachineRoomCountByOperatorIds(@Param("operatorIds") List<Long> operatorIds);

否则group by 返回的是一个List<Object[]>数组

总结

可以jpa中的group by返回一个数组对象,根据自己需要返回值取