【spring-boot-starter-data-neo4j】创建结点和查找结点操作

配置连接neo4j

复制代码
# application.properties
spring.neo4j.uri=bolt://localhost:7687
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=你的密码

定义实体类

java 复制代码
package com.anmory.platform.GraphService.Dao;

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.Property;

@Node(labels = "药材名称")
public class Herb {
    @Id
    @GeneratedValue
    private Long id;

    @Property("name")
    private String name;

    public Herb() {}

    public Herb(String name) {
        this.name = name;
    }

    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;
    }
}

定义仓库类

java 复制代码
package com.anmory.platform.GraphService.Repository;

import com.anmory.platform.GraphService.Dao.Herb;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.stereotype.Repository;

/**
 * @author Anmory/李梦杰
 * @description TODO
 * @date 2025-03-15 下午12:58
 */

@Repository
public interface HerbRepo extends Neo4jRepository<Herb, Long> {
    // 使用自定义查询
    @Query("MATCH (herb:药材名称 {name: $name}) RETURN herb")
    Herb findByName(String name);
}

定义服务类

java 复制代码
package com.anmory.platform.GraphService.Service;

import com.anmory.platform.GraphService.Dao.Herb;
import com.anmory.platform.GraphService.Repository.HerbRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HerbService {
    private static final Logger logger = LoggerFactory.getLogger(HerbService.class);

    @Autowired
    private HerbRepo herbRepo;

    public Herb createHerb(String name) {
        logger.info("Creating herb with name: {}", name);
        Herb herb = new Herb(name);
        return herbRepo.save(herb);
    }

    public Herb findHerbByName(String name) {
        logger.info("Finding herb by name: {}", name);
        return herbRepo.findByName(name); // 返回 null 如果未找到
    }
}

定义视图类

java 复制代码
package com.anmory.platform.GraphService.Controller;

import com.anmory.platform.GraphService.Dao.Herb;
import com.anmory.platform.GraphService.Service.HerbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class HerbController {
    @Autowired
    private HerbService herbService;

    @PostMapping("/createherb")
    public Herb createHerb(@RequestParam String name) {
        System.out.println("name = " + name);
        return herbService.createHerb(name);
    }

    @GetMapping("/findherb")
    public Herb getHerbByName(@RequestParam String name) {
        System.out.println("name = " + name);
        return herbService.findHerbByName(name);
    }
}

测试创建和查找

在postman中输入以下url来创建和测试
http://localhost:8080/createherb?name=我的药材1
http://localhost:8080/findherb?name=我的药材1

相关推荐
是赵敢敢啊20 小时前
自定义注解@SyncNeo4j实现增删改导入时自动同步数据节点和关系到Neo4j
neo4j
心念枕惊9 天前
Conda 环境一键搬家:用 conda-pack 打包带走,连网都不用
conda·neo4j
码农学院18 天前
Neo4j知识图谱赋能跨境电商GEO:LLM实体识别与AI搜索引擎结构化数据输出实战
人工智能·知识图谱·neo4j
TE-茶叶蛋18 天前
RAG智能电商客服什么时候上Neo4j?
neo4j
羊羊小栈18 天前
基于GraphRAG的税务财务智能问答系统(Neo4j_大语言模型)
人工智能·算法·语言模型·自然语言处理·毕业设计·neo4j·大作业
hhzz20 天前
(九)在MCU上跑AI推理:ESP-DL vs TFLite Micro,选哪个?怎么优化?
人工智能·单片机·neo4j·esp·ai推理
༄沐࿆风࿆࿆20 天前
neo4j Desktop下载学习笔记
笔记·学习·neo4j
我是唐青枫20 天前
Java Neo4j 实战指南:从图模型、Cypher 到 Spring Boot 关系查询
java·spring boot·neo4j
AI-好学者25 天前
RDF对比与Neo4j性能优化
人工智能·知识图谱·neo4j·knowledge graph
冷小鱼1 个月前
Neo4j 深度解析:从原生图存储到 GraphRAG 的知识图谱革命
人工智能·知识图谱·neo4j