springboot整合neo4j--采用Neo4jClient和Neo4jTemplate方式

1.背景

看了spring-boot-starter-data-neo4j的源码之后发现,该starter内已经实现了Neo4jClientNeo4jTemplate ,我们只需要使用Autowire就能直接使用它操作neo4j。

Neo4jClient方式与我的另一篇springboot整合neo4j-使用原生cypher Java API博客方式一样,Neo4jTemplate则与SpringBoot 整合 Neo4j博客实现方式类似,但比这篇博客要简单。

2.实现

2.1引入maven

springboot版本为2.6,低版本的可能不支持。

java 复制代码
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>

该包里面已经包含了neo4j-java-driver,故不需要额外引入。

完整maven文件与我的另一篇博客内容一致springboot整合neo4j-使用原生cypher Java API

2.2配置

因为该方式是springboot starter方式,所以配置文件的配置路径是固定的。

java 复制代码
spring.data.neo4j.uri=bolt://127.0.0.1:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456

2.3测试

java 复制代码
package com.win.chaos;

import com.win.chaos.model.neo4j.Neo4jGraph;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import javax.annotation.Resource;

@SpringBootTest
public class Neo4jTest {

    @Resource
    private Neo4jClient neo4jClient;
    @Resource
    private Neo4jTemplate neo4jTemplate;

    @Test
    public void test() {
        String cypher = "MATCH p=()-[r:`持股`]->() RETURN p LIMIT 25";
        Driver driver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "123456"));
        Session session = driver.session();
        //Transaction ts = session.beginTransaction();
        //Result result = ts.run(cypher);
        Result result = session.run(cypher);
        Neo4jGraph graph = Neo4jGraph.parse(result);
        int size = graph.getNodes().size();
    }

    @Test
    public void testNeo4jClient() {
        String cypher = "MATCH p=()-[r:`持股`]->() RETURN p LIMIT 25";
        String addQL = "CREATE (o:people {name:\"里斯\",id:32435})"
        QueryRunner runner = neo4jClient.getQueryRunner();
        Result result = runner.run(cypher);
        Neo4jGraph graph = Neo4jGraph.parse(result);
        int size = graph.getNodes().size();

		runner.run(addQL);
    }

    @Test
    public void testNeo4jTemplate() {
        String cypher = "MATCH p=()-[r:`持股`]->() RETURN p LIMIT 25";
        QueryRunner runner = neo4jTemplate.findAll();//需要传入参数
        Result result = runner.run(cypher);
        Neo4jGraph graph = Neo4jGraph.parse(result);
        int size = graph.getNodes().size();
    }
}

上述代码中的解析查询结果的代码Neo4jGraph.parse与我的另一篇博客内容一致springboot整合neo4j-使用原生cypher Java API

完整代码:本博客完整代码

相关推荐
程序员爱钓鱼16 小时前
用Python开发“跳一跳”小游戏——从零到可玩
后端·python·面试
程序员爱钓鱼16 小时前
Python 源码打包成.whl文件的完整指南
后端·python·面试
IT_陈寒16 小时前
Vite 3.0 实战:5个优化技巧让你的开发效率提升50%
前端·人工智能·后端
.生产的驴16 小时前
DockerCompoe 部署注册中心Nacos 一键部署 单机+Mysql8
java·linux·运维·spring boot·缓存·docker·doc
、BeYourself16 小时前
Spring AI ChatClient 响应处理
后端·ai·springai
222you17 小时前
前后端分离项目在云服务器上的部署(Spring Boot + Vue)
运维·服务器·spring boot
计算机毕设指导617 小时前
基于微信小程序图像识别的智能垃圾分类系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·分类·maven
若丶相见17 小时前
Java对比Python 3.10+ 全栈语法与底层进阶百科全书
后端
奕成则成17 小时前
Django使用
后端·python·django
superman超哥17 小时前
Rust impl 块的组织方式:模块化设计的艺术
开发语言·后端·rust·模块化设计·rust impl块·impl块