neo4j 是一个高性能的 图数据库,适用于存储和查询 节点(node) 和 关系(relationship) 的数据。本教程将带你从零开始,在 spring boot 项目中整合 neo4j,并实现基本的 crud 操作。
1. neo4j 简介
neo4j 是一个 原生图数据库,采用 属性图模型,数据由 节点(node) 和 关系(relationship) 组成,每个节点和关系都可以有 属性(property)。
neo4j 的核心概念
| 概念 | 说明 |
|---|---|
| 节点(node) | 类似于关系数据库中的 记录,可以有标签(label)和属性(property)。 |
| 关系(relationship) | 连接两个节点,有方向(单向或双向),可以有类型(type)和属性(property)。 |
| 属性(property) | 键值对(key-value),可以存储在节点或关系上。 |
| 标签(label) | 类似于关系数据库中的 表,用于分类节点。 |
| 类型(type) | 类似于关系数据库中的 外键,用于定义关系的类型。 |
2. 环境准备
2.1 安装 neo4j
方式 1:本地安装(docker 方式)
docker run \
--name neo4j \
-p 7474:7474 -p 7687:7687 \
-e neo4j_auth=neo4j/123456 \
neo4j:5.12.0- 7474:neo4j web 管理界面端口
- 7687:neo4j 数据库端口(bolt 协议)
- neo4j_auth=neo4j/123456:默认用户名
neo4j,密码123456
访问 neo4j web 界面:http://localhost:7474
方式 2:云服务(neo4j aura)
- 官网:https://neo4j.com/cloud/aura/
- 免费试用,无需本地安装。
2.2 创建 spring boot 项目
使用 spring initializr 创建项目:
- 依赖:
- spring web
- spring data neo4j
https://i.imgur.com/xyz1234.png
3. spring boot 整合 neo4j
3.1 添加 neo4j 依赖
在 pom.xml 中添加:
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-neo4j</artifactid>
</dependency>3.2 配置 neo4j 连接
在 application.yml 或 application.properties 中配置:
spring:
data:
neo4j:
uri: bolt://localhost:7687 # neo4j bolt 协议地址
username: neo4j # 默认用户名
password: 123456 # 默认密码3.3 创建 neo4j 实体类
neo4j 的实体类使用 @node 注解标记,类似于 jpa 的 @entity。
示例:定义person节点
import org.springframework.data.neo4j.core.schema.id;
import org.springframework.data.neo4j.core.schema.node;
import org.springframework.data.neo4j.core.schema.property;
@node("person") // 定义节点标签为 "person"
public class person {
@id // 主键
private long id;
@property("name") // 属性名
private string name;
@property("age")
private integer age;
// 构造方法、getter、setter
public person() {}
public person(long id, string name, integer age) {
this.id = id;
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 integer getage() { return age; }
public void setage(integer age) { this.age = age; }
}3.4 创建 neo4j repository
类似于 jpa 的 jparepository,neo4j 提供 neo4jrepository 用于 crud 操作。
import org.springframework.data.neo4j.repository.neo4jrepository;
import org.springframework.stereotype.repository;
@repository
public interface personrepository extends neo4jrepository<person, long> {
// 可以自定义查询方法
list<person> findbyname(string name);
}3.5 创建 service 层
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import java.util.list;
@service
public class personservice {
@autowired
private personrepository personrepository;
// 保存 person
public person saveperson(person person) {
return personrepository.save(person);
}
// 查询所有 person
public list<person> findallpersons() {
return personrepository.findall();
}
// 按名字查询 person
public list<person> findbyname(string name) {
return personrepository.findbyname(name);
}
// 删除 person
public void deleteperson(long id) {
personrepository.deletebyid(id);
}
}3.6 创建 controller 层
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;
import java.util.list;
@restcontroller
@requestmapping("/api/persons")
public class personcontroller {
@autowired
private personservice personservice;
// 新增 person
@postmapping
public person addperson(@requestbody person person) {
return personservice.saveperson(person);
}
// 查询所有 person
@getmapping
public list<person> getallpersons() {
return personservice.findallpersons();
}
// 按名字查询 person
@getmapping("/name/{name}")
public list<person> getpersonsbyname(@pathvariable string name) {
return personservice.findbyname(name);
}
// 删除 person
@deletemapping("/{id}")
public void deleteperson(@pathvariable long id) {
personservice.deleteperson(id);
}
}4. 测试 neo4j 操作
4.1 启动 spring boot 项目
运行 springbootapplication 主类,访问:
- neo4j web 界面:http://localhost:7474
- api 接口:
post /api/persons(新增 person)get /api/persons(查询所有 person)get /api/persons/name/{name}(按名字查询)delete /api/persons/{id}(删除 person)
4.2 使用 postman 测试
(1) 新增 person
post http://localhost:8080/api/persons
{
"id": 1,
"name": "alice",
"age": 25
}(2) 查询所有 person
get http://localhost:8080/api/persons
(3) 按名字查询
get http://localhost:8080/api/persons/name/alice
(4) 删除 person
delete http://localhost:8080/api/persons/1
5. 进阶:定义关系(relationship)
neo4j 不仅可以存储节点,还可以定义 关系。例如,person 和 movie 之间可以有 acted_in 关系。
5.1 定义movie节点
@node("movie")
public class movie {
@id
private long id;
@property("title")
private string title;
// getter & setter
}5.2 定义关系
使用 @relationship 注解定义关系:
import org.springframework.data.neo4j.core.schema.relationship;
@node("person")
public class person {
@id
private long id;
@property("name")
private string name;
@property("age")
private integer age;
@relationship(type = "acted_in", direction = relationship.direction.outgoing)
private list<movie> movies;
// getter & setter
}6. 总结
| 步骤 | 说明 |
|---|---|
| 1. 安装 neo4j | 本地 docker 或云服务 |
| 2. 创建 spring boot 项目 | 添加 spring-boot-starter-data-neo4j |
| 3. 配置 neo4j 连接 | application.yml 配置 |
| 4. 定义 neo4j 实体 | 使用 @node 注解 |
| 5. 创建 repository | 继承 neo4jrepository |
| 6. 实现 crud 操作 | service + controller |
| 7. 测试 api | postman 或浏览器 |
到此这篇关于spring boot 整合 neo4j的文章就介绍到这了,更多相关spring boot 整合 neo4j内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论