Spring?Boot?整合?Neo4j的過程詳解
Neo4j 是一個高性能的 ??圖數(shù)據(jù)庫??,適用于存儲和查詢 ??節(jié)點(diǎn)(Node)?? 和 ??關(guān)系(Relationship)?? 的數(shù)據(jù)。本教程將帶你從零開始,在 ??Spring Boot?? 項目中整合 ??Neo4j??,并實(shí)現(xiàn)基本的 ??CRUD?? 操作。
??1. Neo4j 簡介??
Neo4j 是一個 ??原生圖數(shù)據(jù)庫??,采用 ??屬性圖模型??,數(shù)據(jù)由 ??節(jié)點(diǎn)(Node)?? 和 ??關(guān)系(Relationship)?? 組成,每個節(jié)點(diǎn)和關(guān)系都可以有 ??屬性(Property)??。
??Neo4j 的核心概念??
| 概念 | 說明 |
|---|---|
| ??節(jié)點(diǎn)(Node)?? | 類似于關(guān)系數(shù)據(jù)庫中的 ??記錄??,可以有標(biāo)簽(Label)和屬性(Property)。 |
| ??關(guān)系(Relationship)?? | 連接兩個節(jié)點(diǎn),有方向(單向或雙向),可以有類型(Type)和屬性(Property)。 |
| ??屬性(Property)?? | 鍵值對(Key-Value),可以存儲在節(jié)點(diǎn)或關(guān)系上。 |
| ??標(biāo)簽(Label)?? | 類似于關(guān)系數(shù)據(jù)庫中的 ??表??,用于分類節(jié)點(diǎn)。 |
| ??類型(Type)?? | 類似于關(guān)系數(shù)據(jù)庫中的 ??外鍵??,用于定義關(guān)系的類型。 |
2. 環(huán)境準(zhǔn)備??
??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 數(shù)據(jù)庫端口(Bolt 協(xié)議)
- ??NEO4J_AUTH=neo4j/123456??:默認(rèn)用戶名
neo4j,密碼123456
訪問 ??Neo4j Web 界面??:http://localhost:7474
??方式 2:云服務(wù)(Neo4j Aura)??
- 官網(wǎng):https://neo4j.com/cloud/aura/
- 免費(fèi)試用,無需本地安裝。
??2.2 創(chuàng)建 Spring Boot 項目??
使用 ??Spring Initializr?? 創(chuàng)建項目:
- ??依賴??:
- ??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 協(xié)議地址
username: neo4j # 默認(rèn)用戶名
password: 123456 # 默認(rèn)密碼3.3 創(chuàng)建 Neo4j 實(shí)體類??
Neo4j 的實(shí)體類使用 @Node 注解標(biāo)記,類似于 JPA 的 @Entity。
??示例:定義Person節(jié)點(diǎn)?
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") // 定義節(jié)點(diǎn)標(biāo)簽為 "Person"
public class Person {
@Id // 主鍵
private Long id;
@Property("name") // 屬性名
private String name;
@Property("age")
private Integer age;
// 構(gòu)造方法、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 創(chuàng)建 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 創(chuàng)建 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 創(chuàng)建 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 項目??
運(yùn)行 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. 進(jìn)階:定義關(guān)系(Relationship)??
Neo4j 不僅可以存儲節(jié)點(diǎn),還可以定義 ??關(guān)系??。例如,Person 和 Movie 之間可以有 ACTED_IN 關(guān)系。
??5.1 定義Movie節(jié)點(diǎn)?
@Node("Movie")
public class Movie {
@Id
private Long id;
@Property("title")
private String title;
// Getter & Setter
}5.2 定義關(guān)系??
使用 @Relationship 注解定義關(guān)系:
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. 總結(jié)??
| 步驟 | 說明 |
|---|---|
| ??1. 安裝 Neo4j?? | 本地 Docker 或云服務(wù) |
| ??2. 創(chuàng)建 Spring Boot 項目?? | 添加 spring-boot-starter-data-neo4j |
| ??3. 配置 Neo4j 連接?? | application.yml 配置 |
| ??4. 定義 Neo4j 實(shí)體?? | 使用 @Node 注解 |
| ??5. 創(chuàng)建 Repository?? | 繼承 Neo4jRepository |
| ??6. 實(shí)現(xiàn) CRUD 操作?? | Service + Controller |
| ??7. 測試 API?? | Postman 或瀏覽器 |
到此這篇關(guān)于Spring Boot 整合 Neo4j的文章就介紹到這了,更多相關(guān)Spring Boot 整合 Neo4j內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java SpringBoot自定義注解,及自定義解析器實(shí)現(xiàn)對象自動注入操作
這篇文章主要介紹了java SpringBoot自定義注解,及自定義解析器實(shí)現(xiàn)對象自動注入操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
基于Java實(shí)現(xiàn)回調(diào)監(jiān)聽工具類
這篇文章主要為大家詳細(xì)介紹了如何基于Java實(shí)現(xiàn)一個回調(diào)監(jiān)聽工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
MybatisPlus實(shí)現(xiàn)對象嵌套關(guān)聯(lián)查詢一對多List集合查詢
這篇文章主要介紹了MybatisPlus實(shí)現(xiàn)對象嵌套關(guān)聯(lián)查詢一對多List集合查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
SpringBoot實(shí)現(xiàn)數(shù)據(jù)預(yù)熱的方式小結(jié)
這里用到的數(shù)據(jù)預(yù)熱,就是在項目啟動時將一些數(shù)據(jù)量較大的數(shù)據(jù)加載到緩存中(筆者這里用的Redis),那么在項目啟動有哪些方式可以實(shí)現(xiàn)數(shù)據(jù)預(yù)熱呢,本文就來給大家講講幾種實(shí)現(xiàn)數(shù)據(jù)預(yù)熱的方式,需要的朋友可以參考下2023-09-09
劍指Offer之Java算法習(xí)題精講數(shù)組與字符串
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化2022-03-03
SpringBoot整合Sa-Token實(shí)現(xiàn)RBAC權(quán)限模型的過程解析
這篇文章主要介紹了SpringBoot整合Sa-Token實(shí)現(xiàn)RBAC權(quán)限模型的過程解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-05-05

