欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot neo4j的配置代码

程序员文章站 2022-06-24 18:30:11
neo4j是一个图形数据库,有一个做关系图谱的需求里面需要使用到图形数据库。w3c教程:https://www.w3cschool.cn/neo4j/中文版的数据库可以通过image属性显示图片,官网...

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

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

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

pom导入配置

<dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-data-neo4j</artifactid>
        </dependency>
 <dependency>
            <groupid>org.neo4j</groupid>
            <artifactid>neo4j-ogm-http-driver</artifactid>
            <version>3.1.4</version>
        </dependency>

配置数据库:

#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<facenodepro> facenodepros = lists.newarraylist();

    private list<facenode>  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<facenodepro> getfacenodepros() {
        return facenodepros;
    }

    public void setfacenodepros(list<facenodepro> 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, long> {

    facenode findbyobjid(@param("objid") string objid);

    /**
     * 查询节点node指定层级的图
     *
     * @param
     * @return
     */
    @query(value = "match n=(:facenode{objid:{objid}})-[*..6]->() return n")
    list<facenode> 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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: springboot neo4j