崛起于SpringBoot2.X + 集成Neo4j图形数据库(70)

原创
01/02 17:38
阅读数 669

 《SpringBoot2.X心法总纲》 

码云地址:https://gitee.com/mdxl/blog.git

提前安装好neo4j,使用Springboot2.1.5版本

1、依赖添加

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>

2、配置文件

server.port=8084
#neo4j
spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=76521

3、配置类

import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.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;

@Configuration
@EnableNeo4jRepositories(basePackages = "com.springboot.neo4j.repository")
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();
    }
}

4、实体类

弄一个人物关系图谱,需要的实体类三个

@Getter
@NodeEntity("ParentNode")
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@ToString
public class ParentNode {
    private @Id @GeneratedValue Long id;
    private @Property(name = "name") String name;
    private @Relationship(type = "RelationEdge") Set<RelationNode> 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);
    }
}
@RelationshipEntity(type = "RelationEdge")
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@Getter
public class RelationNode {
    private @Id @GeneratedValue 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;
    
}
}
@Getter
@NodeEntity("SonNode")
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
public class SonNode {
    private @Id @GeneratedValue Long id;
    private @Property(name = "name") String name;
    private @Relationship(type = "RelationEdge", direction = "INCOMING") Set<RelationNode> sets = new HashSet<>();
    public SonNode(String name){
        this.name = name;
    }
}

5、Repository层

public interface ParentReporitory extends Neo4jRepository<ParentNode,Long> {
}

6、Controller层

@RestController
@Slf4j
public class Neo4JController {
    @Autowired
    ParentReporitory parentReporitory;

    @GetMapping(value = "/test1")
    public void test1(){
        SonNode sonNode1 = new SonNode("孩子柳依依");
        SonNode sonNode2 = new SonNode("孩子柳风");

        ParentNode parentNode = new ParentNode("青年:柳大叔");

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

        parentReporitory.save(parentNode);
    }

    @GetMapping(value = "/test2")
    public void test2(){
        Iterable<ParentNode> parentNodes = parentReporitory.findAll();
        Iterator<ParentNode> iterator = parentNodes.iterator();
        while (iterator.hasNext()){
            ParentNode parentNode = iterator.next();
            Set<RelationNode> relationNodeSet = parentNode.getSets();
            for (RelationNode relationNode : relationNodeSet){
                log.info("id:"+parentNode.getId()+"    name:"+parentNode.getName() +"     关系:"+relationNode.getName() +"子节点:"+relationNode.getSonNode().getName());
            }

        }
    }
}

7、测试

先执行test1接口,在执行test2接口,结果如图:

测试成功,然后我们使用D3.js可以实现前端页面的展示,这个我没做。

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部