《Spring Data JPA》jpa表关联查询,聚合查询,JPQL语句, 模糊查询,like

单表查询 

《Spring Data JPA》jpa表关联查询,聚合查询,JPQL语句, 模糊查询,like_第1张图片

需要保证类中有此构造方法 

 聚合查询

	 /**
	  * 以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 personStatsOopSql();

关联查询 

    /**
     * 根据用户查找代理角色
     * @param userId
     * @return List
     */
    @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 findByUserId(@Param("userId")String userId);

分页查询

@Query(value = "select new org.fage.vo.UserOutputVO(u.name, u.email, d.name as departmentName, count(r.id) as roleNum) from User u "
			+ "left join u.department d left join u.roles r group by u.id")
	Page findUserOutputVOAllPage(Pageable pageable);
@Test
	public void testFindUserOutputVOAllPage(){
		Pageable pageable = new PageRequest(0,5);
		Page page = userRepository.findUserOutputVOAllPage(pageable);
		List list = page.getContent();
		for(UserOutputVO vo : list)
			logger.info(vo.toString());
	}

模糊查询


	@Query("from Game where name like '%?1%' ")
	public void findByName(String name);

 

你可能感兴趣的:(java,spring)