欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 【spring-boot-starter-data-neo4j】创建结点和查找结点操作

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

2025/3/16 8:54:29 来源:https://blog.csdn.net/Anmory/article/details/146278715  浏览:    关键词:【spring-boot-starter-data-neo4j】创建结点和查找结点操作

配置连接neo4j

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

定义实体类

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

定义仓库类

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

定义服务类

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);@Autowiredprivate 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 如果未找到}
}

定义视图类

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 {@Autowiredprivate 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
在这里插入图片描述
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词