Neo4j的springboot的简单实现

71 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第31天,点击查看活动详情

引言

今天带来通过springboot来操作neo4j的教程。

pom.xml的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.demo.DemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

上面是pom.xml中的全部依赖,我们分别使用一些Maven的插件,springboot的一些依赖和Neo4j的相关依赖。

使用Java代码完成节点的查找

接下来我们来使用Java来完成节点的查找

首先我定义一个实体类

package com.example.demo.realClass;


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

@NodeEntity(label = "Customer")
public class GoogleProfile {
    @Id
    @GeneratedValue
    private Long identity;

    @Property(name = "name")
    private String name;

    @Property(name = "dob")
    private String dob;

    @Property(name = "id")
    private String id;

    public Long getIdentity() {
        return identity;
    }

    public void setIdentity(Long identity) {
        this.identity = identity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    public GoogleProfile(String id,String name,String dob)
    {
        this.id = id;
        this.name = name;
        this.dob = dob;
    }
}

googleRepository的实现

package com.example.demo.trusteeship;

import com.example.demo.realClass.GoogleProfile;
import org.springframework.data.neo4j.repository.Neo4jRepository;

public interface GoogleRepository extends Neo4jRepository<GoogleProfile , Long> {
}

注意:这里我们继承自Neo4jRepository<>,我们ctrl+鼠标左边反编译打开这个类,发现其内部有很多方法,这都是其对neo4j的一些实现的一些封装。我们可以在日后对其仔细认真的研读学习。

分别定义identity,name,id,dob等几个属性,我们identity属性使用了两个注解,分别是@ID和@GeneratedValue,因为neo4j内部会自动的为我们为其生成独特的身份证(其真正的id)所以这里我们交给neo4j来实现id的赋予。

然后我在之前的数据库中示范的demo节点中使用了如下图的结构:

image.png

所以我们实体类中也要有name,id,dob这几个属性。 最后我们在类的上一行,使用了这样的注解

@NodeEntity(label = "Customer")

声明这是个节点,label(标签)为Customer.

package com.example.demo;

import com.example.demo.realClass.GoogleProfile;
import com.example.demo.trusteeship.GoogleRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Optional;

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    GoogleRepository googleRepository;
    @Test
    void contextLoads() {
        Optional<GoogleProfile> optional = googleRepository.findById(21L);
        System.out.println(optional.get().getName());
    }
}

解释一下上面的代码,我们首先使用@Autowired注解初始化了googleRepository,在实现中调用了其内部中的.findById()方法来查找其所要查找的节点的ID。

image.png

最后通过.get获取对象,最后通过我们之前实体类中书写的.getName()的方法获取其name的value。

运行一下:

结果如下:

image.png 成功获取到其value。

插入一个节点

package com.example.demo;
import com.example.demo.realClass.GoogleProfile;
import com.example.demo.trusteeship.GoogleRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Optional;

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    GoogleRepository googleRepository;
    @Test
    void contextLoads() {
        GoogleProfile googleProfile = new GoogleProfile("10002","hqc","2022-10-28");
        Optional<GoogleProfile> optional = googleRepository.findById(21L);
        System.out.println(optional.get().getName());
        googleRepository.save(googleProfile);
        optional.orElse(null);
    }
}

我们简单修改一下上面的代码,得到上述代码。

我们初始化了实体类GoogleProfile。

获得了实例化的对象googleProfile

我们调用构造方法为其对应的属性赋值。

然后我们通过.save()方法完成其节点的存储。

执行方法

我们节点多了一个。

image.png

成功完成插入的操作。