Neo4j 入门级使用
一、集成步骤
(一)创建 Spring Boot 项目
-
使用 Spring Initializr 创建项目时,选择 Maven 或 Gradle 作为项目构建工具,选择合适的 Spring Boot 版本,并添加 “Spring Data Neo4j” 依赖。
(二)添加依赖
-
若使用 Maven,在
pom.xml
文件中添加以下代码:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency>
(三)配置 Neo4j 连接信息
-
在
application.properties
或application.yml
文件中配置 Neo4j 数据库的连接信息。如使用application.properties
文件,可按以下格式配置:
spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456
二、实体类定义
(一)节点实体类
-
使用
@Node
注解定义节点实体类。例如:
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 @GeneratedValueprivate Long id;private String name;private int age;// 空构造方法、带参数构造方法、getter 和 setter 方法public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}// getter 和 setter 方法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
注解定义关系实体类。例如:
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 @GeneratedValueprivate Long id;private String title;private String genre;// 空构造方法、带参数构造方法、getter 和 setter 方法public Movie() {}public Movie(String title, String genre) {this.title = title;this.genre = genre;}// getter 和 setter 方法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;}// 定义从 Movie 到 Person 的关系(演员参演)@Relationship(type = "ACTED_IN", direction = Relationship.Direction.INCOMING)private Person actor;public Person getActor() {return actor;}public void setActor(Person actor) {this.actor = actor;}
}
三、仓库接口
-
创建一个仓库接口来操作节点实体类。例如:
import org.springframework.data.neo4j.repository.Neo4jRepository;public interface PersonRepository extends Neo4jRepository<Person, Long> {// 自定义查询方法示例:根据姓名查询Person findByName(String name);
}
四、服务层
-
创建一个服务类来调用仓库接口的方法。例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class PersonService {@Autowiredprivate PersonRepository personRepository;// 保存 Personpublic Person savePerson(Person person) {return personRepository.save(person);}// 根据姓名查询 Personpublic Person findPersonByName(String name) {return personRepository.findByName(name);}// 删除所有 Personpublic void deleteAllPersons() {personRepository.deleteAll();}
}
五、控制器
-
创建一个控制器类来处理 HTTP 请求,并调用服务层的方法。例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/persons")
public class PersonController {@Autowiredprivate PersonService personService;// 添加 Person@PostMappingpublic Person addPerson(@RequestBody Person person) {return personService.savePerson(person);}// 根据姓名查询 Person@GetMapping("/{name}")public Person getPersonByName(@PathVariable String name) {return personService.findPersonByName(name);}// 删除所有 Person@DeleteMappingpublic void deleteAllPersons() {personService.deleteAllPersons();}
}
六、运行项目
-
启动 Spring Boot 应用程序后,可以通过 RESTful API 来操作 Neo4j 数据库。
七、Neo4j 的详细使用
(一)基本操作
1. 创建节点
-
在 Neo4j Browser 中,可以使用 Cypher 查询语言创建节点。例如:
CREATE (p:Person {name: "John Doe", age: 30})
CREATE (m:Movie {title: "The Matrix", genre: "Science Fiction"})
2. 查询节点
-
查询所有 Person 节点:
MATCH (p:Person) RETURN p
查询特定条件的节点:
MATCH (p:Person {name: "John Doe"}) RETURN p
3. 更新节点
-
更新节点的属性:
MATCH (p:Person {name: "John Doe"}) SET p.age = 31 RETURN p
4. 删除节点
-
删除节点:
MATCH (p:Person {name: "John Doe"})
DETACH DELETE p
(二)关系操作
1. 创建关系
-
创建两个节点之间的关系:
MATCH (p:Person {name: "John Doe"}), (m:Movie {title: "The Matrix"})
CREATE (p)-[r:ACTED_IN]->(m)
RETURN r
2. 查询关系
-
查询特定的关系:
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = "John Doe" AND m.title = "The Matrix"
RETURN r
3. 更新关系
-
更新关系的属性:
MATCH (p:Person {name: "John Doe"})-[r:ACTED_IN]->(m:Movie {title: "The Matrix"})
SET r.role = "Lead Actor"
RETURN r
4. 删除关系
-
删除关系:
MATCH (p:Person {name: "John Doe"})-[r:ACTED_IN]->(m:Movie {title: "The Matrix"})
DELETE r
(三)其他操作
1. 图查询
-
查询路径:
MATCH path = (p:Person)-[*]-(m:Movie)
RETURN path
查询最短路径:
MATCH (start:Person {name: "John Doe"}), (end:Movie {title: "The Matrix"})
CALL algo.shortestPath.stream(start, end, "ACTED_IN")
YIELD nodeIds
RETURN nodeIds
2. 图分析
-
计算节点的度数中心性:
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WITH p, COUNT(r) AS degree
RETURN p, degree
ORDER BY degree DESC
计算节点的 PageRank:
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 数据库来存储和查询图数据。