7.SpringBoot整合Neo4j

1.引入依赖

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

**说明:**这里引入neo4j的版本跟spring框架的版本有关系。需要注意不同的版本在neo4j的配置上可能存在差异,下面的代码书写上也存在差异。

这里使用的是:

2.配置文件添加配置

复制代码
spring:
  data:
    neo4j:
      uri: bolt://localhost:7687
      username: neo4j
      password: 123456

3.通过java实体创建节点代码

java 复制代码
package com.xkj.org.dao;

import com.xkj.org.entity.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {


}
java 复制代码
package com.xkj.org.entity;

import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;

import java.io.Serializable;

@NodeEntity(label = "person")
@Data
public class Person implements Serializable{

    @Id
    @GeneratedValue
    private Long id;

    @Property
    private String name;

}
java 复制代码
package com.xkj.org;

import com.xkj.org.dao.PersonRepository;
import com.xkj.org.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class Neo4jApplication {

    @Autowired
    private PersonRepository repository;

    @Test
    public void test() {
        List<Person> all = repository.findAll();
        System.out.println(all);
    }


}

4.通过java实体创建关系代码

java 复制代码
package com.xkj.org.dao;

import com.xkj.org.entity.PersonRelationShip;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Component;

@Component
public interface PersonRelationShipRepository extends Neo4jRepository<PersonRelationShip, Long>{



}
java 复制代码
package com.xkj.org.entity;

import lombok.Data;
import org.neo4j.ogm.annotation.*;

import java.io.Serializable;

@Data
@RelationshipEntity(type = "personRelationShip")
public class PersonRelationShip implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @StartNode
    private Person parent;

    @EndNode
    private Person child;

    @Property
    private String relation;

}
java 复制代码
@Test
    public void test2() {
        Person p1 = personRepository.findById(70L).orElse(null);
        Person p2 = personRepository.findById(66L).orElse(null);

        PersonRelationShip personRelationShip = new PersonRelationShip();
        personRelationShip.setParent(p1);
        personRelationShip.setChild(p2);
        personRelationShip.setRelation("夫妻");
        personRelationShipRepository.save(personRelationShip);
    }

5.直接通过cql语句创建关系代码

java 复制代码
package com.xkj.org.dao;

import com.xkj.org.entity.Person;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long>{

    @Query("match (n:person{name:{0}}), (m:person{name:{2}})" +
            "create (n)-[:人际关系{relation:{1}}]->(m)")
    void createRelation(String from, String relation, String to);

}
java 复制代码
@Test
    public void test3() {
        personRepository.createRelation("小姐姐", "暗恋", "小哥哥");
    }
相关推荐
特立独行的猫A5 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
一起_java6 小时前
技术文章-基于Dify-Agent的智能问答系统设计与实现
java·spring boot·后端·agent·dify
Flynn Liang7 小时前
I/O 密集型高并发实战:Java 服务 10 维优化,从线程打满到 10 万 QPS
java·spring boot·性能优化·高并发·系统设计·虚拟线程·io密集型
吃我一窝jio8 小时前
Python与Go线程模型深度对比
java·开发语言·python
这个昵称不容易8 小时前
SpringBoot 购物推荐网站的设计与实现:120 个 Java 文件、147 个接口、13 张表,附真实运行截图
java·springboot·毕设
文丑颜不良啊8 小时前
LeetCode3041~3060题目解析
java·leetcode
文丑颜不良啊10 小时前
LeetCode3101~3120题目解析
java·leetcode
Resean022310 小时前
SpringMVC 6+源码分析(十一)视图渲染与异步请求(收尾篇)
java·spring·springmvc
靠沿10 小时前
MySQL数据库进阶——存储过程和触发器
java·数据库·mysql
码少女11 小时前
MySQL--库与表的增删改查基础
java·数据库·mysql