查询出演次数大于一次的演员和相关的电影
MATCH (person:Person)-[:ACTED_IN]->(m:Movie) WITH person, count(*) AS appearances, collect(m.title) AS movies WHERE appearances > 1 RETURN person.name, appearances, movies结果:
╒═════════════╤═════════════╤══════════════════════════════╕ │"person.name"│"appearances"│"movies" │ ╞═════════════╪═════════════╪══════════════════════════════╡ │"Tom Hanks" │2 │["Cloud Atlas","Forrest Gump"]│ └─────────────┴─────────────┴──────────────────────────────┘ Springboot 集成测试版本:2.0.3.RELEASE
引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency> 配置文件application.yml注意格式的正确
spring: data: neo4j: uri: bolt://localhost:7687 username: neo4j password: 123456 定义节点、关系对象(省略了Get/Set等方法)
/** * @author chenghao.li * @Description 电影节点 */ @NodeEntity(label = "Movie") public class MovieGraph implements Serializable { /** * 主键 */ @Id @GeneratedValue private Long id; /** * 电影标题 */ @Property(name = "title") private String title; /** * 发布年份 */ @Property(name = "released") private Integer released; } /** * @author chenghao.li * @Description 演员节点 */ @NodeEntity(label = "People") public class PeopleGraph implements Serializable { @Id @GeneratedValue private Long id; @Property(name = "name") private String name; @Property(name = "born") private Integer born; } /** * @author chenghao.li * @Description 出演关系 */ @RelationshipEntity(type = "ACTED_IN") public class ActedInRelationShip implements Serializable { @Id @GeneratedValue private Long id; @StartNode private PeopleGraph startNode; @EndNode private MovieGraph endNode; @Property(name = "roles") private String roles; } /** * @author chenghao.li * @Description 指导关系 */ @RelationshipEntity(type = "DIRECTED") public class DirectedRelationShip implements Serializable { @Id @GeneratedValue private Long id; @StartNode private PeopleGraph startNode; @EndNode private MovieGraph endNode; }涉及到的一些注解说明:
@NodeEntity(label = "People") 表示图中的一个节点
@Id 主键ID,使用Long类型
@GeneratedValue 自动生成主键ID的值
@Property(name = "name") 表示图中节点的一个属性
@RelationshipEntity(type = "ACTED_IN") 表示图中连线的关系
定义Repository以演员为例,继承Neo4jRepository 即可实现增删改查分页排序等操作;其他的都是如此
/** * @author chenghao.li */ @Repository public interface PeopleRepository extends Neo4jRepository<PeopleGraph, Long> { /** * 根据姓名查找 * * @param name 姓名 * @return PeopleGraph */ @Query("MATCH (p:People) where p.name={0} return p") PeopleGraph findByName(String name); } 单元测试 @SpringBootTest @RunWith(SpringRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class Neo4jApplicationTests { @Autowired private PeopleRepository peopleRepository; @Autowired private MovieRepository movieRepository; @Autowired private ActedInRelationShipRepository actedInRelationShipRepository; @Autowired private DirectedRelationShipRepository directedRelationShipRepository; /** * 添加演员节点 */ @Test public void t1() { PeopleGraph peopleGraph1 = new PeopleGraph(); peopleGraph1.setName("Keanu Reeves"); peopleGraph1.setBorn(1964); PeopleGraph peopleGraph2 = new PeopleGraph(); peopleGraph2.setName("Robert Zemeckis"); peopleGraph2.setBorn(1951); PeopleGraph peopleGraph3 = new PeopleGraph(); peopleGraph3.setName("Tom Hanks"); peopleGraph3.setBorn(1956); PeopleGraph peopleGraph4 = new PeopleGraph(); peopleGraph4.setName("Jerry"); peopleGraph4.setBorn(1986); List<PeopleGraph> peopleGraphList = new ArrayList<>(); peopleGraphList.add(peopleGraph1); peopleGraphList.add(peopleGraph2); peopleGraphList.add(peopleGraph3); peopleGraphList.add(peopleGraph4); Iterable<PeopleGraph> peopleGraphs = peopleRepository.saveAll(peopleGraphList); Assert.assertNotNull(peopleGraphs); } /** * 添加电影节点 */ @Test public void t2() { MovieGraph movieGraph1 = new MovieGraph(); movieGraph1.setTitle("The Matrix"); movieGraph1.setReleased(1997); MovieGraph movieGraph2 = new MovieGraph(); movieGraph2.setTitle("Cloud Atlas"); movieGraph2.setReleased(2012); MovieGraph movieGraph3 = new MovieGraph(); movieGraph3.setTitle("Forrest Gump"); movieGraph3.setReleased(1994); List<MovieGraph> movieGraphs = new ArrayList<>(); movieGraphs.add(movieGraph1); movieGraphs.add(movieGraph2); movieGraphs.add(movieGraph3); Iterable<MovieGraph> movieGraphIterable = movieRepository.saveAll(movieGraphs); Assert.assertNotNull(movieGraphIterable); } /** * 添加关系 */ @Test public void t3() { // Tom Hanks在电影Forrest Gump中出演Forrest角色 PeopleGraph tom_hanks = peopleRepository.findByName("Tom Hanks"); MovieGraph forrest_gump = movieRepository.findByTitle("Forrest Gump"); ActedInRelationShip tom_ActedIn_relationShip_forrest = new ActedInRelationShip(); tom_ActedIn_relationShip_forrest.setStartNode(tom_hanks); tom_ActedIn_relationShip_forrest.setEndNode(forrest_gump); tom_ActedIn_relationShip_forrest.setRoles("Forrest"); ActedInRelationShip save = actedInRelationShipRepository.save(tom_ActedIn_relationShip_forrest); Assert.assertNotNull(save); // Tom Hanks在电影Forrest Gump中出演Forrest角色 MovieGraph cloud = movieRepository.findByTitle("Cloud Atlas"); ActedInRelationShip tom_ActedIn_relationShip_Cloud = new ActedInRelationShip(); tom_ActedIn_relationShip_Cloud.setStartNode(tom_hanks); tom_ActedIn_relationShip_Cloud.setEndNode(cloud); tom_ActedIn_relationShip_Cloud.setRoles("Zachry"); ActedInRelationShip save1 = actedInRelationShipRepository.save(tom_ActedIn_relationShip_Cloud); Assert.assertNotNull(save1); // Robert Zemeckis指导电影Forrest Gump PeopleGraph robert_zemeckis = peopleRepository.findByName("Robert Zemeckis"); DirectedRelationShip directedRelationShip = new DirectedRelationShip(); directedRelationShip.setStartNode(robert_zemeckis); directedRelationShip.setEndNode(forrest_gump); DirectedRelationShip save2 = directedRelationShipRepository.save(directedRelationShip); Assert.assertNotNull(save2); // jerry 在电影 The Matrix 出演 J 角色 PeopleGraph jerry = peopleRepository.findByName("Jerry"); MovieGraph the_matrix = movieRepository.findByTitle("The Matrix"); ActedInRelationShip jerry_ActedIn_relationShip_matrix = new ActedInRelationShip(); jerry_ActedIn_relationShip_matrix.setStartNode(jerry); jerry_ActedIn_relationShip_matrix.setEndNode(the_matrix); jerry_ActedIn_relationShip_matrix.setRoles("J"); actedInRelationShipRepository.save(jerry_ActedIn_relationShip_matrix); } } 附报错信息