springboot neo4j的配置代码

neo4j是一个图形数据库,有一个做关系图谱的需求里面需要使用到图形数据库。

w3c教程:https://www.w3cschool.cn/neo4j/

中文版的数据库可以通过image属性显示图片,官网的动画效果是通过3D.js实现的;

pom导入配置


            org.springframework.boot
            spring-boot-starter-data-neo4j
        
 
            org.neo4j
            neo4j-ogm-http-driver
            3.1.4
        

配置数据库:

#neo4j
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123
spring.data.neo4j.uri=http://192.168.100.106:7474
package com.koala.console.configuration;

import org.neo4j.ogm.session.SessionFactory;
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.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableNeo4jRepositories(basePackages = "com.koala.console.repository.neo4j")
@EnableTransactionManagement
public class Neo4jConfig {

    @Value("${blotUri}")
    private String uri;

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

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

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

    @Bean
    public SessionFactory sessionFactory() {
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder()
                .uri(databaseUrl)
                .credentials(userName, password)
                .build();
        return new SessionFactory(configuration, "com.koala.console.model.neo4j");
    }

    @Bean
    public Neo4jTransactionManager transactionManager() {
        return new Neo4jTransactionManager(sessionFactory());
    }


}

使用neo4j:

neo4j基本由两部分组成,节点和关系,关系用来指示两个节点的方向和关联属性:

节点bean:

package com.koala.console.model.neo4j;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import lombok.Data;
import org.neo4j.ogm.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Data
@NodeEntity
public class FaceNode {

    public FaceNode() {
    }

    public FaceNode(String name) {
        this.objId = name;
    }

    @Id
    @GeneratedValue
    private Long id;

    /**
     * objId
     */
    private String objId;


    @JSONField(serialize = false)
    @JsonIgnore
    @Transient
    private String startName;
    @JSONField(serialize = false)
    @JsonIgnore
    @Transient
    private String endName;

    @Transient
    private Integer pathNum;

    @Transient
    private String intimacy;

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

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

    @Transient
    private Integer[] symbolSize = {60,60};

    @JSONField(serialize = false)
    @Transient
    private MultipartFile faceImg;
    @JSONField(serialize = false)
    @Transient
    private String startTime;

    @JSONField(serialize = false)
    @Transient
    private String endTime;



    @Relationship(type = "relation")
    private List faceNodePros = Lists.newArrayList();

    private List  children = Lists.newArrayList();

    public void addEndNode(FaceNode endNode, String title) {
        FaceNodePro pro = new FaceNodePro(title, this, endNode);
        this.faceNodePros.add(pro);
    }

    public void addStartNode(FaceNode startNode, String title) {
        FaceNodePro pro = new FaceNodePro(title, startNode, this);
        this.faceNodePros.add(pro);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getObjId() {
        return objId;
    }

    public void setObjId(String objId) {
        this.objId = objId;
    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public List getFaceNodePros() {
        return faceNodePros;
    }

    public void setFaceNodePros(List faceNodePros) {
        this.faceNodePros = faceNodePros;
    }

    public String getStartName() {
        return startName;
    }

    public void setStartName(String startName) {
        this.startName = startName;
    }

    public String getEndName() {
        return endName;
    }

    public void setEndName(String endName) {
        this.endName = endName;
    }

    public MultipartFile getFaceImg() {
        return faceImg;
    }

    public void setFaceImg(MultipartFile faceImg) {
        this.faceImg = faceImg;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getScenes() {
        return scenes;
    }

    public void setScenes(String scenes) {
        this.scenes = scenes;
    }
}

关系bean:

package com.koala.console.model.neo4j;

import com.fasterxml.jackson.annotation.JsonBackReference;
import org.neo4j.ogm.annotation.*;

@RelationshipEntity(type = "relation")
public class FaceNodePro {

    public FaceNodePro() {
    }

    public FaceNodePro(String intimacy, FaceNode startNode, FaceNode endNode) {
        this.intimacy = intimacy;
        this.startNode = startNode;
        this.endNode = endNode;
    }

    @Id
    @GeneratedValue
    private Long nodeProId;

    /**
     * 亲密度
     */
    @Property
    private String intimacy;

    @JsonBackReference
    @StartNode
    private FaceNode startNode;

    @EndNode
    private FaceNode endNode;

    public Long getNodeProId() {
        return nodeProId;
    }

    public void setNodeProId(Long nodeProId) {
        this.nodeProId = nodeProId;
    }

    public String getIntimacy() {
        return intimacy;
    }

    public void setIntimacy(String intimacy) {
        this.intimacy = intimacy;
    }

    public FaceNode getStartNode() {
        return startNode;
    }

    public void setStartNode(FaceNode startNode) {
        this.startNode = startNode;
    }

    public FaceNode getEndNode() {
        return endNode;
    }

    public void setEndNode(FaceNode endNode) {
        this.endNode = endNode;
    }
}

Repository实现:

@Repository
public interface FaceNodeRepository extends Neo4jRepository {

    FaceNode findByObjId(@Param("objId") String objId);

    /**
     * 查询节点Node指定层级的图
     *
     * @param
     * @return
     */
    @Query(value = "MATCH n=(:FaceNode{objId:{objId}})-[*..6]->() RETURN n")
    List findByHierarchical(@Param("objId") String objId);

}

剩下的使用都是很简单的事情。

我收集了一些会使用到的语句:

查询节点Node所有的关系
match(Node{name:'5c26219bd3e2dca5322110bb'})-[:PLAYED_IN]->(yf)return Node,yf
查询节点Node的2层关系图
MATCH n=(:Node{name:"5c262177d3e2dca5322110b3"})-[*..1]-() return n
两个陌生人之间的所有最短认识路径
MATCH n = allshortestPaths((小讯:朋友圈{姓名:"小讯"})-[*..6]-(小菲:朋友圈{姓名:"小菲"})) return n
查询节点Node指向的所有节点
MATCH (:Node { name: '5c262137d3e2dca5322110a7' })-->(movie)RETURN movie;
查询标签Node所有节点
match(n)--(m:Node)return n;
批量创建Node节点的朋友圈
MATCH (Node:Node {name:"4j2ap"})FOREACH (name in ["Johan","Rajesh","Anna","Julia","Andrew"] |CREATE (Node)-[:FRIEND]->(:Person {name:name}))
查询两个节点有向关系条数
match (:FaceNode{name:"gong"})-[r:PLAYED_IN]->(:FaceNode{name:"eza2e"})return count(r)

参考:

转载自:https://www.cxymm.net/article/sinat_21184471/87092034
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/sinat_21184471/article/details/87092034

到此这篇关于springboot neo4j的文章就介绍到这了,更多相关springboot neo4j内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的