【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

相关推荐
麦麦大数据2 天前
F042 A星算法课程推荐(A*算法) | 课程知识图谱|课程推荐vue+flask+neo4j B/S架构前后端分离|课程知识图谱构造
vue.js·算法·知识图谱·neo4j·a星算法·路径推荐·课程推荐
rengang662 天前
132-Spring AI Alibaba Vector Neo4j 示例
人工智能·spring·neo4j·rag·spring ai·ai应用编程
小宋10214 天前
Neo4j-图数据库入门图文保姆攻略
数据库·neo4j
消失在人海中5 天前
图形数据库Neo4J简介
数据库·oracle·neo4j
麦麦大数据5 天前
F035 vue+neo4j中医南药药膳知识图谱可视化系统 | vue+flask
vue.js·知识图谱·neo4j·中医·中药·药膳·南药
liliangcsdn5 天前
如何结合langchain、neo4j实现关联检索问答
开发语言·python·langchain·neo4j
麦麦大数据5 天前
F037 vue+neo4j 编程语言知识图谱可视化分析系统vue+flask+neo4j
vue.js·flask·知识图谱·neo4j·可视化·编程语言知识图谱
chushiyunen6 天前
neo4j图数据库笔记
数据库·笔记·neo4j
麦麦大数据7 天前
F034 vue+neo4j 体育知识图谱系统|体育文献知识图谱vue+flask知识图谱管理+d3.js可视化
javascript·vue.js·知识图谱·neo4j·文献·体育·知识图谱管理
我叫张土豆7 天前
Neo4j 版本选型与 Java 技术栈深度解析:Spring Data Neo4j vs Java Driver,如何抉择?
java·人工智能·spring·neo4j