首页 > 其他分享 >springboot集成neo4j

springboot集成neo4j

时间:2024-03-06 11:00:34浏览次数:19  
标签:集成 springboot Person org person import neo4j public

1创建一个springboot项目引入neo4j的依赖

<!-- neo4j依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>

2、配置文件设置连接neo4j的地址

# neo4j配置
spring.data.neo4j.uri= bolt://192.168.0.100:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=testpasswd

3、创建person的实体类和接口PersonRepository

package com.example.demo.entity;

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")
public class Person implements Serializable {

    //neo4j自动生成的id
    @Id
    @GeneratedValue
    private Long id;

    //节点的属性
    @Property
    private String 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.example.demo.dao;

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

import java.util.List;

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

    @Query("Match (p:person) return p")
    List<Person> findParentList();
}

4、创建RelatlionShip的关系类和接PersonRelatlionShipRepository

package com.example.demo.entity;

import org.neo4j.ogm.annotation.*;

import java.io.Serializable;

//关系实体  必须有起始节点和结束节点
@RelationshipEntity(type="personRelationShip")
public class RelatlionShip  implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    //起始节点
    @StartNode
    private Person startPerson;

    //结束节点
    @EndNode
    private Person endPerson;

    //属性
    @Property
    private String relation;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Person getStartPerson() {
        return startPerson;
    }

    public void setStartPerson(Person startPerson) {
        this.startPerson = startPerson;
    }

    public Person getEndPerson() {
        return endPerson;
    }

    public void setEndPerson(Person endPerson) {
        this.endPerson = endPerson;
    }

    public String getRelation() {
        return relation;
    }

    public void setRelation(String relation) {
        this.relation = relation;
    }
}

 

package com.example.demo.dao;


import com.example.demo.entity.RelatlionShip;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRelatlionShipRepository extends Neo4jRepository<RelatlionShip,Long> {

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

5、创建测试类DemoApplicationTests

package com.example.demo;

import com.example.demo.dao.PersonRelatlionShipRepository;
import com.example.demo.dao.PersonRepository;
import com.example.demo.entity.Person;
import com.example.demo.entity.RelatlionShip;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import sun.print.PeekGraphics;

import java.util.List;
import java.util.Optional;

@SpringBootTest
class DemoApplicationTests {

@Autowired
PersonRepository personRepository;

@Autowired
PersonRelatlionShipRepository personRelatlionShipRepository;

@Test
void testFindById() {

//用id查询
Optional<Person> person = personRepository.findById(54L);
System.out.println(person.get().getName());
//orElse函数的作用是如果表达式的结果为空值(null),则返回一个指定的默认值作为替代
person.orElse(null);
}

@Test
void testFindPersonList() {
List<Person> list = personRepository.findParentList();
for(Person person:list){
System.out.println(person.getName());
}
}

@Test
void testDeleteById() {
//用id删除
personRepository.deleteById(2L);
}

@Test
void testCreateEntity() {
//增加节点
Person newPerson = new Person();
newPerson.setName("大侠");
personRepository.save(newPerson);
}

@Test
void testCreateRelation() {
//构建关系
Person father = new Person();
father.setName("爸爸");
personRepository.save(father);

Person child = new Person();
child.setName("child");
personRepository.save(child);

//创建关系
RelatlionShip relatlionShip = new RelatlionShip();
relatlionShip.setStartPerson(father);
relatlionShip.setEndPerson(child);
relatlionShip.setRelation("父子");
personRelatlionShipRepository.save(relatlionShip);

}

@Test
void testCreateRelationCql() {
personRelatlionShipRepository.createRelation("大侠","斩杀","李四");
}

@Test
void testDeleteRelationById() {
//用id删除关系
personRelatlionShipRepository.deleteById(18L);
}

}

6、运行测试方法

<1>清空数据

MATCH (n)

DETACH DELETE n

 

<2>造数据演示数据

CREATE (p1:person{name:"张三"})

CREATE (p2:person{name:"李四"})

CREATE (p3:person{name:"王五"})

查看数据

MATCH (p:person)

return p

 

 

<3>演示根据id查找person的方法

运行结果:

<4>运行自己写的方法,返回所有person对象

运行结果

<5>创建一个person  大侠

运行后在neo4j里查询

<5>调用方法创建对象之间的联系

运行后查看结果:

<5>调用自己写的语句方法,创建关系

 

运行结果:

 

标签:集成,springboot,Person,org,person,import,neo4j,public
From: https://www.cnblogs.com/yclh/p/18056051

相关文章

  • 下一代积木式智能组装编排,集成开发效率10倍提升
    理论+实战揭秘下一代组装式融合集成平台架构和核心技术,让开发者快速了解低代码智能集成开发趋势和下一代技术。本期直播主题《下一代积木式智能组装编排,集成开发效率10倍提升》,华为云DTSE技术布道师马兵东,结合当前iPaaS最新趋势,理论+实战揭秘下一代组装式融合集成平台架构和核......
  • springboot 应用程序 pom节点版本冲突问题解决思路
    springboot应用程序pom节点版本冲突问题解决思路一、首先 mavenhelper 查看是否有冲突 conflicts 二、allDenpendencies  查询如poi 查询冲突 ps: <scope>compile</scope>  compile:这是默认的依赖项范围。指定为compile的依赖项将在编译、测试和......
  • 01_尚硅谷_SpringBoot课件
    框架高级课程系列之SpringBoot-2.3.6尚硅谷JavaEE教研组版本:V3.3一.SpringBoot概述与入门(掌握) 1.1SpringBoot概述1.1.1什么是SpringBootSpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework同属于spring的产品:其最主要作用就是帮助开发人员快速的......
  • springboot - 配置文件 @ConfigurationProperties
    1.简单属性@Configuration@ConfigurationProperties(prefix="mail")publicclassConfigProperties{privateStringhostName;privateintport;privateStringfrom;//standardgettersandsetters}注意:如果我们不在POJO中使用@Configurati......
  • SpringBoot中try/catch异常并回滚事务(自动回滚/手动回滚/部分回滚)
    https://www.cnblogs.com/cfas/p/16423510.html https://www.cnblogs.com/konglxblog/p/16229175.htmlSpringBoot异常处理回滚事务详解(自动回滚、手动回滚、部分回滚)(事务失效) 参考:https://blog.csdn.net/zzhongcy/article/details/102893309概念事务定义事务,就是一......
  • SpringBoot3整合Druid数据源的解决方案
    druid-spring-boot-3-starter目前最新版本是1.2.20,虽然适配了SpringBoot3,但缺少自动装配的配置文件,会导致加载时报加载驱动异常。<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>1.2.20</version......
  • SpringBoot常用注解
    SpringBoot常用注解  @SpringBootApplication=@SpringBootConfiguration+@ComponentScan+@EnableAutoConfiguration@Configuration注解能够将一个类定义为SpringBoot应用程序中的配置类,等同于spring的XML配置文件,从而使该类中的Bean对象能够被SpringIoC容器进行自动管......
  • spring面试高频问题---springboot自动配置
    springboot自动配置1.springboot自动配置原理自动配置主要依赖于@SpringBootApplication注解,其中还包含了三个注解@SpringBootConfiguration:该注解与@Configuration注解作用相同,用来声明当前也是个配置类。@ComponentScan:组件扫描,默认扫描当前引导类所在包及其子包。@Ena......
  • 【HMS Core】集成推送服务,打包后应用名乱码
    ​【问题描述】使用flutter进行移动端开发,导入了agconnect-services.json之后,成功集成了华为推送,但是应用打包如果应用名为中文,debug包生成的应用名会是乱码。​​ 【问题分析】1、经推测可能是agc插件与开发者引入的另外的sdk相冲突导致的,但即使去掉其他的sdk也会导致该问......
  • springboot集成报文验证组件validation
    1.引入validation的依赖jar<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>3.2.3</version></dependency>2.请求报文增加字段的校验规则packa......