云社区 博客 博客详情

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

xixi 发表于 2020-01-02 20:15:27 2020-01-02
0
0

【摘要】 【推荐】2019Java开发者跳槽指南.pdf(吐血整理)>>> 《SpringBoot2.X心法总纲》 提前安装好neo4j,使用Springboot2.1.5版本1、依赖添加org.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-data-neo4j2、配置文件server.port=80...

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

 《SpringBoot2.X心法总纲》 

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

1、依赖添加


   org.projectlombok
   lombok
   true


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

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 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 sets = new HashSet<>();
    public SonNode(String name){
        this.name = name;
    }
}

5、Repository层

public interface ParentReporitory extends Neo4jRepository {
}

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 parentNodes = parentReporitory.findAll();
        Iterator iterator = parentNodes.iterator();
        while (iterator.hasNext()){
            ParentNode parentNode = iterator.next();
            Set 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可以实现前端页面的展示,这个我没做。

 

文章来源: www.oschina.net,作者:木九天,版权归原作者所有,如需转载,请联系作者。

原文链接:https://my.oschina.net/mdxlcj/blog/3152755

登录后可下载附件,请登录或者注册

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:huaweicloud.bbs@huawei.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
评论文章 //点赞 收藏 0
点赞
分享文章到微博
分享文章到朋友圈

上一篇:C#/winform设置窗体自动缩放适应不同尺寸的电脑屏幕大小

下一篇:md5和password_hash谁跟牛X

评论 (0)


登录后可评论,请 登录注册

评论