springboot整合neo4j

Posted 阿拉的梦想

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合neo4j相关的知识,希望对你有一定的参考价值。

安装neo4j

从官网下载neo4j-community-3.5.33-unix.tar.gz
3.5版本支持jdk1.8,新版本需要jdk11

用ip地址访问

修改neo4j.conf文件找到里面

#dbms.connectors.default advertised address=localhost 改为
dbms.connectors.default advertised address=0.0.0.0
#dbms.connector.bolt.listen address=:7687
dbms.connector.bolt.listen address=0.0.0.0:7687
#dlbms.connectorhttp.listen address=:7474改为
dbms.connector.http.listen address=0.0.0.0:7474

将注解都打开,换成0.0.0.0,重启,就可以通过ip连接了

springboot整合neo4j

本文使用的
springboot:1.5.5
java:1.8

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>

参数配置:

spring.data.neo4j.uri=http://redhare.demo.com:7474
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456

配置类

package com.demo.devops.itca.neo4j;

import org.neo4j.ogm.session.SessionFactory;
import org.neo4j.ogm.session.transaction.DefaultTransactionManager;
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
/*
 * 声明neo4j repository存放地址
 */

@EnableNeo4jRepositories("com.demo.devops.itca.neo4j")
public class Neo4jConfig 

    @Value("$spring.data.neo4j.uri")
    private String uri;
    @Value("$spring.data.neo4j.username")
    private String username;
    @Value("$spring.data.neo4j.password")
    private String password;

    @Bean
    public org.neo4j.ogm.config.Configuration getConfiguration() 
        org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
        config.driverConfiguration()
                .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
                .setURI(uri).setCredentials(username, password);
        return config;
    

    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory() 
        /**
         * 如果不指定节点映射的java bean路径,保存时会报如下警告,导致无法将节点插入Neo4j中
         * ... is not an instance of a persistable class
         */
        return new SessionFactory(getConfiguration(), "com.demo.devops.itca.neo4j");
    

    /**
     * 解决MyBatis 事务与Neo4j 事务冲突
     *
     * @return
     */
    @Bean
    public DefaultTransactionManager getDefaultTransactionManager() 
        return new DefaultTransactionManager(getSessionFactory().openSession());
    

模型类

节点类
AppNode.java

package com.demo.devops.itca.neo4j;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * @author CCL
 * @createTime 2022年05月31日 18:51:00
 */
@NodeEntity(label = "AppNode")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AppNode 
    @Id
    @GeneratedValue
    private Long id;

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

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

    private Integer identity;


关系类
AppRelationShip.java

package com.demo.devops.itca.neo4j;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@RelationshipEntity(type = "appRelationShip")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AppRelationShip 

    @Id
    @GeneratedValue
    private Long id;

    @StartNode
    private AppNode parent;

    @EndNode
    private AppNode child;

持久化类

节点持久化类

package com.demo.devops.itca.neo4j;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface AppRepository extends Neo4jRepository<AppNode, Long> 
    //@Query("MATCH (n fullName:fullName) RETURN n")
    @Query("MATCH (m:AppNode) where m.fullName=fullName RETURN m")
    AppNode findByFullName(@Param("fullName") String fullName);


关系持久化类

package com.demo.devops.itca.neo4j;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface AppRelationShipRepository extends Neo4jRepository<AppRelationShip, Long> 
    @Query("MATCH (fromNode) WHERE id(fromNode) = fromId MATCH (toNode) WHERE id(toNode) = toId MATCH (fromNode)-[r]->(toNode) DELETE r")
    void deleteByNodeId(@Param(value = "fromId") long fromId, @Param(value = "toId") long toId);

以上是关于springboot整合neo4j的主要内容,如果未能解决你的问题,请参考以下文章

springboot整合neo4j

springboot整合图像数据库Neo4j

SpringBoot整合Redis初实践

springBoot+SpringData 整合入门

12.整合neo4j

SpringBoot2基于SpringBoot实现SSMP整合