写在前面

本文基于 Docker 搭建 Neo4J服务,提供web管理页面

详细搭建过程,可参考这里 Neo4J示例练习

更多学习,可官网自行学习,官网

一、项目搭建

Maven 工程

1.1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-demo-neo4j</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-demo-neo4j</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.2-jre</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>spring-boot-demo-neo4j</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.2、application.yml配置

spring:
  data:
    neo4j:
      uri: bolt://192.168.1.74:7687
      username: neo4j
      password: admin
      open-in-view: false

二、测试

package com.tonels.neo4j;

import cn.hutool.json.JSONUtil;
import com.tonels.neo4j.service.NeoService;
import com.tonels.neo4j.model.Lesson;
import com.tonels.neo4j.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class Neo4jTest extends SpringBootDemoNeo4jApplicationTests {
   
    @Autowired
    private NeoService neoService;

    /** * 测试保存 */
    @Test
    public void testSave() {
   
        neoService.initData();
    }

    /** * 测试删除 */
    @Test
    public void testDelete() {
   
        neoService.delete();
    }

    /** * 测试查询 鸣人 学了哪些课程 */
    @Test
    public void testFindLessonsByStudent() {
   
        // 深度为1,则课程的任教老师的属性为null
        // 深度为2,则会把课程的任教老师的属性赋值
        List<Lesson> lessons = neoService.findLessonsFromStudent("漩涡鸣人", 2);

        lessons.forEach(lesson -> log.info("【lesson】= {}", JSONUtil.toJsonStr(lesson)));
    }

    /** * 测试查询班级人数 */
    @Test
    public void testCountStudent() {
   
        Long all = neoService.studentCount(null);
        log.info("【全校人数】= {}", all);
        Long seven = neoService.studentCount("第七班");
        log.info("【第七班人数】= {}", seven);
    }

    /** * 测试根据课程查询同学关系 */
    @Test
    public void testFindClassmates() {
   
        Map<String, List<Student>> classmates = neoService.findClassmatesGroupByLesson();
        classmates.forEach((k, v) -> log.info("因为一起上了【{}】这门课,成为同学关系的有:{}", k, JSONUtil.toJsonStr(v.stream()
                .map(Student::getName)
                .collect(Collectors.toList()))));
    }

    /** * 查询所有师生关系,包括班主任/学生,任课老师/学生 */
    @Test
    public void testFindTeacherStudent() {
   
        Map<String, Set<Student>> teacherStudent = neoService.findTeacherStudent();
        teacherStudent.forEach((k, v) -> log.info("【{}】教的学生有 {}", k, JSONUtil.toJsonStr(v.stream()
                .map(Student::getName)
                .collect(Collectors.toList()))));
    }
}

篇幅有限,无法摘出所有部分 ,本文所有 代码,可在这里找到 Gitee地址