访问Neo4j spring-data-neo4j入门 (二)@Query 复杂查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/russle/article/details/84436993

本文在访问Neo4j spring-data-neo4j入门 (一) 基础上成文,因此请先阅读访问Neo4j spring-data-neo4j入门 (一)

我们在上一篇提到使用@Query完成复杂查询,如同我们的业务一样,使用简单的比较大小、日期范围无法完成业务需要,特别是当我们需要在多个关系中进行查询时,返回的对象也需要包括关联的内容。例如返回Movie的所有参演者,就需要Person对象的name等信息。因此返回的对象也不再是简单的domain对象,而是复杂的组合对象。笔者这里以Person为例。实际中可以使用map等。

示例代码

我们只需要在自己的Repository实现类上完成@Query注解即可。

public interface PersonRepository extends Neo4jRepository<Person, Long> , CustomizedRepository<Person> {

	Person findByfirstName(@Param("firstName") String firstName);

	Collection<Person> findByfirstNameLike(@Param("firstName") String firstName);

	// returns a Page of Actors that have a ACTS_IN relationship to the movie node with the title equal to movieTitle parameter.
	@Query(value = "MATCH (movie:Movie {title:{0}})<-[:ACTED_IN]-(p:Person) RETURN p",
			countQuery= "MATCH (movie:Movie {title:{0}})<-[:ACTED_IN]-(p:Person) RETURN count(p)")
	Page<Person> getActorsThatActInAmoviesFromTitle(String movieTitle, Pageable pageable );
}

我们可以看到需要传入的参数主要是movieTitle, 然后他作为{0}会在MATCH (movie:Movie {title:{0}})<-[:ACTED_IN]-(p:Person) RETURN p中使用到。 因为我们使用Pageable 对象,因此需要提供计算count的cypher

运行效果

完成的代码在这里`,欢迎加星,fork,注意我的代码中有CustomizedRepositoryImpl这个是我为了做其他demo添加的, 不是使用@Query注解需要的类. 你可以把CustomizedRepository都删除了。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/russle/article/details/84436993