一、集成步骤
(一)创建 Spring Boot 项目
- 使用 Spring Initializr 创建项目时,选择 Maven 或 Gradle 作为项目构建工具,选择合适的 Spring Boot 版本,并添加 “Spring Data Neo4j” 依赖。
(二)添加依赖
- 若使用 Maven,在
pom.xml 文件中添加以下代码:
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency>
|
(三)配置 Neo4j 连接信息
- 在
application.properties 或 application.yml 文件中配置 Neo4j 数据库的连接信息。如使用 application.properties 文件,可按以下格式配置:
1 2 3
| spring.data.neo4j.uri=bolt://localhost:7687 spring.data.neo4j.username=neo4j spring.data.neo4j.password=123456
|
二、实体类定义
(一)节点实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Node; @Node public class Person { @Id @GeneratedValue private Long id; private String name; private int age; public Person() {} public Person(String name, int age) { this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
|
(二)关系实体类
- 使用
@Relationship 注解定义关系实体类。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Node; import org.springframework.data.neo4j.core.schema.Relationship; @Node public class Movie { @Id @GeneratedValue private Long id; private String title; private String genre; public Movie() {} public Movie(String title, String genre) { this.title = title; this.genre = genre; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } @Relationship(type = "ACTED_IN", direction = Relationship.Direction.INCOMING) private Person actor; public Person getActor() { return actor; } public void setActor(Person actor) { this.actor = actor; } }
|
三、仓库接口
1 2 3 4 5 6
| import org.springframework.data.neo4j.repository.Neo4jRepository; public interface PersonRepository extends Neo4jRepository<Person, Long> { Person findByName(String name); }
|
四、服务层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PersonService { @Autowired private PersonRepository personRepository; public Person savePerson(Person person) { return personRepository.save(person); } public Person findPersonByName(String name) { return personRepository.findByName(name); } public void deleteAllPersons() { personRepository.deleteAll(); } }
|
五、控制器
- 创建一个控制器类来处理 HTTP 请求,并调用服务层的方法。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/persons") public class PersonController { @Autowired private PersonService personService; @PostMapping public Person addPerson(@RequestBody Person person) { return personService.savePerson(person); } @GetMapping("/{name}") public Person getPersonByName(@PathVariable String name) { return personService.findPersonByName(name); } @DeleteMapping public void deleteAllPersons() { personService.deleteAllPersons(); } }
|
六、运行项目
- 启动 Spring Boot 应用程序后,可以通过 RESTful API 来操作 Neo4j 数据库。
七、Neo4j 的详细使用
(一)基本操作
1. 创建节点
- 在 Neo4j Browser 中,可以使用 Cypher 查询语言创建节点。例如:
1 2
| CREATE (p:Person {name: "John Doe", age: 30}) CREATE (m:Movie {title: "The Matrix", genre: "Science Fiction"})
|
2. 查询节点
1
| MATCH (p:Person) RETURN p
|
查询特定条件的节点:
1
| MATCH (p:Person {name: "John Doe"}) RETURN p
|
3. 更新节点
1 2 3
| MATCH (p:Person {name: "John Doe"}) SET p.age = 31 RETURN p
|
4. 删除节点
1 2
| MATCH (p:Person {name: "John Doe"}) DETACH DELETE p
|
(二)关系操作
1. 创建关系
1 2 3
| MATCH (p:Person {name: "John Doe"}), (m:Movie {title: "The Matrix"}) CREATE (p)-[r:ACTED_IN]->(m) RETURN r
|
2. 查询关系
1 2 3
| MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) WHERE p.name = "John Doe" AND m.title = "The Matrix" RETURN r
|
3. 更新关系
1 2 3
| MATCH (p:Person {name: "John Doe"})-[r:ACTED_IN]->(m:Movie {title: "The Matrix"}) SET r.role = "Lead Actor" RETURN r
|
4. 删除关系
1 2
| MATCH (p:Person {name: "John Doe"})-[r:ACTED_IN]->(m:Movie {title: "The Matrix"}) DELETE r
|
(三)其他操作
1. 图查询
1 2
| MATCH path = (p:Person)-[*]-(m:Movie) RETURN path
|
查询最短路径:
1 2 3 4
| MATCH (start:Person {name: "John Doe"}), (end:Movie {title: "The Matrix"}) CALL algo.shortestPath.stream(start, end, "ACTED_IN") YIELD nodeIds RETURN nodeIds
|
2. 图分析
1 2 3 4
| MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) WITH p, COUNT(r) AS degree RETURN p, degree ORDER BY degree DESC
|
计算节点的 PageRank:
1 2 3 4
| CALL gds.pageRank.stream('person-movie-graph') YIELD nodeId, score RETURN gds.util.asNode(nodeId).name AS name, score ORDER BY score DESC
|
以上是 Spring Boot 集成 Neo4j 以及 Neo4j 的详细使用方法,通过这些步骤和操作,你可以方便地在 Spring Boot 项目中使用 Neo4j 数据库来存储和查询图数据。