《Spring Data JPA》jpa表关联查询,聚合查询,JPQL语句
单表查询
需要保证类中有此构造方法
聚合查询
/**
* 以JPQL面向对象的方式 连接查询
* @return
*/
@Query(value="SELECT new com.zzy.demo.utils.dto.PersonCount(p.name as name, COUNT(p.id) as totalNum) FROM Person p GROUP BY p.name" )
public List<PersonCount> personStatsOopSql();
关联查询
/**
* 根据用户查找代理角色
* @param userId
* @return List<ProxyRole>
*/
@Query(value="select new ProxyRole(p.id, p.userId, p.organId, o.name as organName,p.roleId,p.roleName,p.limitDate) from ProxyRole as p , Organ as o WHERE p.organId=o.id and p.userId = :userId")
List<ProxyRole> findByUserId(@Param("userId")String userId);