栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot整合neo4j

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot整合neo4j

Spring Boot整合neo4j

spring-data-neo4j
版本:springboot 2.3.5 spring-data-neo4j:5.3.5

https://docs.spring.io/spring-data/neo4j/docs/5.3.5/reference/html/#getting-started

添加neo4j依赖

 
	org.springframework.boot 
	spring-boot-starter-data-neo4j 

添加配置
(注意:不同版本依赖配置可能不一样),可通过neo4j自动配置类查看

# neo4j配置 
spring.data.neo4j.uri= bolt://localhost:7687 
spring.data.neo4j.username=neo4j 
spring.data.neo4j.password=12345

创建实体
@NodeEntity:标明是一个节点实体 @RelationshipEntity:标明是一个关系实体 @Id:实体主键
@Property:实体属性 @GeneratedValue:实体属性值自增 @StartNode:开始节点(可以理解为父节
点) @EndNode:结束节点(可以理解为子节点)

import lombok.Data;

import java.io.Serializable;

@Data
@Builder
@NodeEntity("person")
public class Person implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    @Property("name")
    private String name;
}

@Data
@NoArgsConstructor
@RelationshipEntity(type = "徒弟")
public class PersonRelation implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    @StartNode
    private Person parent;
    @EndNode
    private Person child;
    @Property
    private String relation;

    public PersonRelation(Person parent, Person child, String relation) {
        this.parent = parent;
        this.child = child;
        this.relation = relation;
    }
}

创建接口继承Neo4jRepository

@Repository
public interface PersonRelationRepository extends Neo4jRepository {
}

@Repository
public interface PersonRepository extends Neo4jRepository {
    
    @Query("Match (p:person) -[*]->(s:person) where id(p)={0} return s")
    List findChildList(Long pId);

    @Query("Match (p:person {name:{0}}) -[*]->(s:person) return s")
    List findChildList(String name);

    
    @Query("Match (p:person) -[*]->(s:person {name:{0}}) return p")
    List findParentList(String name);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号