12.整合neo4j

Posted fly-book

tags:

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

neo4j

官网下载:
https://neo4j.com/download-center/#community
bin下运行neo4j.bat console

在浏览器地址栏里输入http://localhost:7474
默认会跳转到 http://localhost:7474/browser
刚开始时,会弹出登录页面,默认的初始密码是neo4j,登录进去后会让你设置新的密码,设完后进入neo4j管理界面

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
spring.data.neo4j.uri=http://localhost:7474
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123123
@MapperScan("com.fly.Mapper")
@EnableNeo4jRepositories(basePackages = "com.fly.UserDao")
public class SpringDemoApp
package com.fly.pojo;

import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;

@NodeEntity(label = "User")
public class UserNode 
    @GraphId
    private Long nodeId;
    @Property
    private String userId;
    @Property
    private String name;
    @Property
    private String age;

    public Long getNodeId() 
        return nodeId;
    

    public void setNodeId(Long nodeId) 
        this.nodeId = nodeId;
    

    public String getUserId() 
        return userId;
    

    public void setUserId(String userId) 
        this.userId = userId;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getAge() 
        return age;
    

    public void setAge(String age) 
        this.age = age;
    
package com.fly.pojo;

import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;

@RelationshipEntity(type = "UserRelation")
public class UserRelation 
    @GraphId
    private Long id;
    @StartNode
    private UserNode startNode;
    @EndNode
    private UserNode endNode;

    public Long getId() 
        return id;
    

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

    public UserNode getStartNode() 
        return startNode;
    

    public void setStartNode(UserNode startNode) 
        this.startNode = startNode;
    

    public UserNode getEndNode() 
        return endNode;
    

    public void setEndNode(UserNode endNode) 
        this.endNode = endNode;
    
package com.fly.UserDao;

import com.fly.pojo.UserRelation;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface UserRelationRepository extends GraphRepository<UserRelation> 
    @Query("match p=(n:User)<-[r:UserRelation]->(n1:User) where n.userId=firstUserId and n1.userId=secondUserId return p")
    List<UserRelation> findUserRelationsByEachId(@Param("firstUserId")String firstUserId,@Param("secondUserId")String secondUserId);

    @Query("match (fu:User),(su:User) where fu.userId=firstUserId and su.userId=secondUserId create p=(fu)-[r:UserRelation]->(su) return p")
    List<UserRelation> addUserRelation(@Param("firstUserId")String firstUserId,@Param("secondUserId")String secondUserId);

package com.fly.UserDao;


import com.fly.pojo.UserNode;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface UserRepository extends GraphRepository<UserNode> 
    @Query("MATCH (n:User) RETURN n")
    List<UserNode> getUserNodeList();

    @Query("create (n:Userage:age,name:name) RETURN n")
    List<UserNode> addUserNodeList(@Param("age")String age,@Param("name")String name);
package com.fly.service;

import com.fly.UserDao.UserRepository;
import com.fly.pojo.UserNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService 
    @Autowired
    private UserRepository userRepository;

    public void addUserNode(UserNode userNode)
        userRepository.addUserNodeList(userNode.getAge(),userNode.getName());
    
import com.app.SpringDemoApp;
import com.fly.pojo.UserNode;
import com.fly.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@SpringBootTest(classes = SpringDemoApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceTest 
    @Autowired
    private UserService userService;
    @Test
    public void test1()
        UserNode userNode = new UserNode();
        userNode.setNodeId(1L);
        userNode.setUserId("001");
        userNode.setName("张三");
        userNode.setAge("25");
        userService.addUserNode(userNode);
    

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

springboot整合neo4j

springboot整合neo4j

springboot整合图像数据库Neo4j

springBoot+SpringData 整合入门

12Feign整合断路器Hystrix

Spring原理篇(12)--架构整合流程;该篇章讲解Spring在整合架构的时候的思路;