最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Boot?整合?Neo4j的過程詳解

 更新時間:2025年10月31日 10:20:30   作者:上官簫羽  
Neo4j是一個高性能的?圖數(shù)據(jù)庫??,適用于存儲和查詢節(jié)點(Node)??和??關系(Relationship)??的數(shù)據(jù),本文介紹在springboot項目中整合neo4j,并實現(xiàn)基本的crud操作,感興趣的朋友跟隨小編一起看看吧

        Neo4j 是一個高性能的 ??圖數(shù)據(jù)庫??,適用于存儲和查詢 ??節(jié)點(Node)?? 和 ??關系(Relationship)?? 的數(shù)據(jù)。本教程將帶你從零開始,在 ??Spring Boot?? 項目中整合 ??Neo4j??,并實現(xiàn)基本的 ??CRUD?? 操作。

??1. Neo4j 簡介??

Neo4j 是一個 ??原生圖數(shù)據(jù)庫??,采用 ??屬性圖模型??,數(shù)據(jù)由 ??節(jié)點(Node)?? 和 ??關系(Relationship)?? 組成,每個節(jié)點和關系都可以有 ??屬性(Property)??。

??Neo4j 的核心概念??

概念說明
??節(jié)點(Node)??類似于關系數(shù)據(jù)庫中的 ??記錄??,可以有標簽(Label)和屬性(Property)。
??關系(Relationship)??連接兩個節(jié)點,有方向(單向或雙向),可以有類型(Type)和屬性(Property)。
??屬性(Property)??鍵值對(Key-Value),可以存儲在節(jié)點或關系上。
??標簽(Label)??類似于關系數(shù)據(jù)庫中的 ??表??,用于分類節(jié)點。
??類型(Type)??類似于關系數(shù)據(jù)庫中的 ??外鍵??,用于定義關系的類型。

2. 環(huá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??:默認用戶名 neo4j,密碼 123456

訪問 ??Neo4j Web 界面??:http://localhost:7474

??方式 2:云服務(Neo4j Aura)??

  • 官網(wǎng):https://neo4j.com/cloud/aura/
  • 免費試用,無需本地安裝。

??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             # 默認用戶名
      password: 123456            # 默認密碼

3.3 創(chuàng)建 Neo4j 實體類??

Neo4j 的實體類使用 @Node 注解標記,類似于 JPA 的 @Entity。

??示例:定義Person節(jié)點?

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é)點標簽為 "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 創(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 項目??

運行 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 不僅可以存儲節(jié)點,還可以定義 ??關系??。例如,Person 和 Movie 之間可以有 ACTED_IN 關系。

??5.1 定義Movie節(jié)點?

@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. 創(chuàng)建 Spring Boot 項目??添加 spring-boot-starter-data-neo4j
??3. 配置 Neo4j 連接??application.yml 配置
??4. 定義 Neo4j 實體??使用 @Node 注解
??5. 創(chuàng)建 Repository??繼承 Neo4jRepository
??6. 實現(xiàn) CRUD 操作??Service + Controller
??7. 測試 API??Postman 或瀏覽器

到此這篇關于Spring Boot 整合 Neo4j的文章就介紹到這了,更多相關Spring Boot 整合 Neo4j內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java源碼解析HashMap的resize函數(shù)

    Java源碼解析HashMap的resize函數(shù)

    今天小編就為大家分享一篇關于Java源碼解析HashMap的resize函數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 微信支付H5調(diào)用支付詳解(java版)

    微信支付H5調(diào)用支付詳解(java版)

    本篇文章主要介紹了微信支付H5調(diào)用支付詳解,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2016-12-12
  • 使用kotlin集成springboot開發(fā)的超詳細教程

    使用kotlin集成springboot開發(fā)的超詳細教程

    目前大多數(shù)都在使用java集成 springboot進行開發(fā),本文演示僅僅將 java換成 kotlin,其他不變的情況下進行開發(fā),需要的朋友可以參考下
    2021-09-09
  • 淺談spring中的default-lazy-init參數(shù)和lazy-init

    淺談spring中的default-lazy-init參數(shù)和lazy-init

    下面小編就為大家?guī)硪黄獪\談spring中的default-lazy-init參數(shù)和lazy-init。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • java實現(xiàn)單機版五子棋

    java實現(xiàn)單機版五子棋

    這篇文章主要為大家詳細介紹了java實現(xiàn)單機版五子棋源碼,以及五子棋游戲需要的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Springboot項目打war包docker包找不到resource下靜態(tài)資源的解決方案

    Springboot項目打war包docker包找不到resource下靜態(tài)資源的解決方案

    今天小編就為大家分享一篇關于Springboot項目打war包docker包找不到resource下靜態(tài)資源的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Java中條件運算符的嵌套使用技巧總結

    Java中條件運算符的嵌套使用技巧總結

    在Java中,我們經(jīng)常需要使用條件運算符來進行多個條件的判斷和選擇,條件運算符可以簡化代碼,提高代碼的可讀性和執(zhí)行效率,本文將介紹條件運算符的嵌套使用技巧,幫助讀者更好地掌握條件運算符的應用,需要的朋友可以參考下
    2023-11-11
  • Spring?boot?整合Logback過程示例解析

    Spring?boot?整合Logback過程示例解析

    這篇文章主要為大家介紹了Spring?boot?整合Logback的過程及示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • mybatis-plus返回map自動轉(zhuǎn)駝峰配置操作

    mybatis-plus返回map自動轉(zhuǎn)駝峰配置操作

    這篇文章主要介紹了mybatis-plus返回map自動轉(zhuǎn)駝峰配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • IntelliJ IDEA基于SpringBoot如何搭建SSM開發(fā)環(huán)境的步驟詳解

    IntelliJ IDEA基于SpringBoot如何搭建SSM開發(fā)環(huán)境的步驟詳解

    這篇文章主要介紹了IntelliJ IDEA基于SpringBoot如何搭建SSM開發(fā)環(huán)境,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10

最新評論

周口市| 慈利县| 日土县| 博白县| 航空| 昭苏县| 崇文区| 临颍县| 高州市| 桓仁| 鄂州市| 永登县| 汽车| 疏附县| 彰武县| 广东省| 灵台县| 南澳县| 泽库县| 濉溪县| 石门县| 拜泉县| 河南省| 安顺市| 拉萨市| 嵊州市| 兴文县| 井陉县| 桓仁| 慈利县| 舟曲县| 平遥县| 梅河口市| 青田县| 明水县| 克拉玛依市| 徐汇区| 拜城县| 仁布县| 昌图县| 东安县|