SpringBoot2.x系列教程53--NoSQL之SpringBoot整合Neo4j

SpringBoot2.x系列教程53--NoSQL之SpringBoot整合Neo4j

作者:一一哥

一. Spring Boot中整合Neo4j

我们上一章节简单介绍了Neo4j及其安装配置过程,这节带大家学习在Spring Boot中整合Neo4j.

1. 创建web项目

我们按照之前的经验,创建一个web程序,并将之改造成Spring Boot项目,具体过程略。
SpringBoot2.x系列教程53--NoSQL之SpringBoot整合Neo4j_第1张图片

2. 添加依赖包

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

3. 创建配置文件

#neo4j
spring:
  data:
    neo4j:
      uri: bolt://localhost:7474
      username: neo4j
      password: neo4j

4. 创建Neo4j的配置类

注解@EnableNeo4jRepositories用来开启Neo4j的仓库功能;
注解@EnableTransactionManagement用来支持事务。

package com.yyg.boot.config;

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/10
 * @Description Description
 */
@Configuration
@EnableNeo4jRepositories(basePackages = "com.yyg.boot.repository")
@EnableTransactionManagement
public class Neo4jConfig {

    @Value("${spring.data.neo4j.uri}")
    private String url;

    @Value("${spring.data.neo4j.username}")
    private String username;

    @Value("${spring.data.neo4j.password}")
    private String password;

    @Bean(name = "session")
    public Session neo4jSession() {
        Driver driver = GraphDatabase.driver(url, AuthTokens.basic(username, password));
        return driver.session();
    }

}

5. 创建关系节点实体类

ParentNode类

package com.yyg.boot.entity;

import lombok.Data;
import lombok.ToString;
import org.neo4j.ogm.annotation.*;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/10
 * @Description Description
 */
@Data
@ToString
@NodeEntity("ParentNode")
public class ParentNode {

    @Id
    @GeneratedValue
    private Long id;

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

    private @Relationship(type = "RelationEdge")
    Set sets = new HashSet<>();

    public ParentNode(String name) {
        this.name = name;
    }

    public void addRelation(SonNode sonNode, String name) {
        RelationNode relationNode = new RelationNode(this, name, sonNode);
        sets.add(relationNode);
        sonNode.getSets().add(relationNode);
    }

}

SonNode类

package com.yyg.boot.entity;

import lombok.Data;
import lombok.ToString;
import org.neo4j.ogm.annotation.*;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/10
 * @Description Description
 */
@NodeEntity("SonNode")
@Data
@ToString
public class SonNode {

    @Id
    @GeneratedValue
    private Long id;

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

    private @Relationship(type = "RelationEdge", direction = "INCOMING")
    Set sets = new HashSet<>();

    public SonNode(String name) {
        this.name = name;
    }

}

RelationNode节点类

package com.yyg.boot.entity;

import lombok.Data;
import lombok.ToString;
import org.neo4j.ogm.annotation.*;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/10
 * @Description Description
 */
@RelationshipEntity(type = "RelationEdge")
@Data
@ToString
public class RelationNode {

    @Id
    @GeneratedValue
    private Long id;

    private @StartNode
    ParentNode parentNode;

    // 关系名
    private String name;

    private @EndNode
    SonNode sonNode;

    RelationNode(ParentNode parentNode, String name, SonNode sonNode) {
        this.parentNode = parentNode;
        this.name = name;
        this.sonNode = sonNode;
    }

}

6. 创建Repository仓库类

package com.yyg.boot.repository;

import com.yyg.boot.entity.ParentNode;
import org.springframework.data.neo4j.repository.Neo4jRepository;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/10
 * @Description Description
 */
public interface ParentRepository extends Neo4jRepository {
}

7. 编写Controller接口方法

package com.yyg.boot.web;

import com.yyg.boot.entity.ParentNode;
import com.yyg.boot.entity.RelationNode;
import com.yyg.boot.entity.SonNode;
import com.yyg.boot.repository.ParentRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.Set;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/10
 * @Description Description
 */
@RestController
@RequestMapping("/node")
@Slf4j
public class Neo4jController {

    @Autowired
    private ParentRepository parentRepository;

    @GetMapping(value = "/create")
    public void createNodeRelation() {
        SonNode sonNode1 = new SonNode("儿子小帅");
        SonNode sonNode2 = new SonNode("女儿小美");

        ParentNode parentNode = new ParentNode("爸爸:孙一一");

        parentNode.addRelation(sonNode1, "女儿");
        parentNode.addRelation(sonNode2, "儿子");

        parentRepository.save(parentNode);
    }

    @GetMapping(value = "/find")
    public void findNodes() {
        Iterable parentNodes = parentRepository.findAll();
        for (ParentNode parentNode : parentNodes) {
            Set relationNodeSet = parentNode.getSets();
            for (RelationNode relationNode : relationNodeSet) {
                log.info("id:" + parentNode.getId() + " name:" + parentNode.getName() + " 关系:" + relationNode.getName() + "子节点:" + relationNode.getSonNode().getName());
            }
        }
    }

}

8. 创建入口类

package com.yyg.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/9
 * @Description Description
 */
@SpringBootApplication
public class Neo4jApplication {

    public static void main(String[] args) {
        SpringApplication.run(Neo4jApplication.class, args);
    }

}

完整目录结构

9. 启动项目进行测试

注意:

要先执行/node/create接口,再执行/node/find接口,结果如图:
SpringBoot2.x系列教程53--NoSQL之SpringBoot整合Neo4j_第2张图片
 

你可能感兴趣的:(Neo4j,Spring,Boot,2)