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("小姐姐", "暗恋", "小哥哥");
    }
相关推荐
Refrain_zc10 小时前
Android 应用内的APK 安装(可复制)
java
杨运交10 小时前
[020][缓存模块]基于 BeanCreator 的缓存管理器创建器模式设计与实践
java·spring·缓存
risc12345610 小时前
DocumentsWriterDeleteQueue 的核心设计思想
java·全文检索·lucene
风味蘑菇干10 小时前
Stream基础题目
java·算法
2501_9327502610 小时前
Java反射机制基础入门
java·开发语言
5008411 小时前
HCCL 集合通信编程:多卡协同的正确姿势
java·flutter·性能优化·electron·wpf
asdfg125896311 小时前
Java中的Comparator 和JS中的回调函数好相似
java·开发语言
会编程的土豆11 小时前
消息队列(MQ)入门笔记
java·笔记·spring
专注VB编程开发20年11 小时前
python运行提速方案全解
java·linux·服务器
涤生大数据11 小时前
大数据面试高频题:row_number() 数据倾斜到底怎么解决?
java·大数据·面试