当前位置: 首页 > news >正文

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.propertiesapplication.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 数据库来存储和查询图数据。

http://www.xdnf.cn/news/403201.html

相关文章:

  • 【Git】GitHub上传图片遇到的问题
  • 告别卡顿,图片查看界的“速度与激情”
  • CentOS部署Collabora Online
  • 【Java学习】Lambda表达式
  • 【Linux】进程状态、优先级、切换和调度
  • 三层交换机,单臂路由(用DHCP自动配置ip+互通+ACL
  • Elasticsearch架构原理
  • 数据库原理期末考试速成--最后附带两套题
  • 项目全栈实战-基于智能体、工作流、API模块化Docker集成的创业分析平台
  • 【漫话机器学习系列】254.假设空间(Hypothesis Space)
  • 批量重命名bat
  • 当 AI 邂逅丝路:揭秘「丝路智旅」,用 RAG 重塑中阿文化旅游体验
  • Axure 纵向滚动隐藏滚动条 Axure 滑动开关(属性开关)on-off
  • 数据结构-树(2)
  • DVWA在线靶场-xss部分
  • 【内网渗透】——S4u2扩展协议提权以及KDC欺骗提权
  • Nginx stream模块是连接级别的负载均衡
  • [计算机科学#14]:数据结构
  • 现代化水库运行管理矩阵平台如何建设?
  • DNS域名解析服务器的部署
  • 2025 年福建省职业院校技能大赛网络建设与运维赛项Linux赛题解析
  • 基于STM32、HAL库的CH342K USB转UART收发器 驱动程序设计
  • Spring Boot 注解详细解析:解锁高效开发的密钥
  • 中科院无人机导航物流配送的智能变革!LogisticsVLN:基于无人机视觉语言导航的低空终端配送系统
  • C++类与对象(二):六个默认构造函数(一)
  • 基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
  • 《Python星球日记》 第64天:NLP 概述与文本预处理
  • 深度学习与机器学习模型全景解析:适用场景与最优实践指南
  • 【高并发架构设计】-1:高并发通用设计思想
  • LayerNorm vs RMSNorm 技术对比