Spring Boot配置neo4j(简单版)

图数据库的开发已经变得越来越流行,与springBoot结合也是很正常,在很早前就已经发布了相关Lib,但是当前网上的Spring Boot整合neo4j都极其复杂,也导致了本人在开发过程中耗费很长时间,顾写下这个文章帮助大众理顺neo4j整合。只需要简单六步,顺序按照编写先后顺序。

首先看整体Demo项目架构:
Spring Boot配置neo4j(简单版)_第1张图片
(一)首先导包pom.xml
xml version="1.0" encoding="UTF-8"?>
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   4.0.0

   com.example
   demo
   0.0.1-SNAPSHOT
   jar

   demo
   Demo project for Spring Boot

   
      org.springframework.boot
      spring-boot-starter-parent
      1.5.9.RELEASE
       
   

   
      UTF-8
      UTF-8
      1.8
   

   
      
         org.springframework.boot
         spring-boot-starter-web
      

      
         org.springframework.boot
         spring-boot-starter-test
         test
      

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

   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   


(二)编写配置文件application.properties
server.port=8080
spring.data.neo4j.uri=http://{neo4jweb端访问地址}:7474 
spring.data.neo4j.username=neo4j账号
spring.data.neo4j.password=neo4j密码
(三)model文件---节点User和关系UserRelation,这两个文件与neo4j里面的节点和关系的属性一一对应,此处是我的示例,具体与各自的neo4j内容对于,label必须与neo4j库里名字一致

@NodeEntity(label = "User")
public class UserNode {
    @GraphId
    private Long nodeId;

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

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

    @Property(name = "age")
    private int 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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
@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;
    }
}
(四)repository文件----UserRepository和UserRelationRepository,@Query里面编写Cypher查询

@Component
public interface UserRepository extends GraphRepository {

    @Query("MATCH (n:User) RETURN n ")
    List getUserNodeList();

    @Query("create (n:User{age:{age},name:{name}}) RETURN n ")
    List addUserNodeList(@Param("name") String name, @Param("age")int age);
}
@Component
public interface UserRelationRepository extends GraphRepository {

    @Query("match p=(n:User)<-[r:UserRelation]->(n1:User) where n.userId={firstUserId} and n1.userId={secondUserId} return p")
    List findUserRelationByEachId(@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 addUserRelation(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);

}
(五)service文件

@Service
public class Neo4jService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserRelationRepository userRelationRepository;

    /**
     * Always use constructor based dependency injection in your beans.
     * Always use assertions for mandatory dependencies.
     */

    public int addUser(UserNode userNode){
        userRepository.addUserNodeList(userNode.getName(),userNode.getAge());
        return 1;
    }
}
(六)neo4j文件注入config----Neo4jConfig,注意basePackges的路径。
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.demo.repository")
public class Neo4jConfig {
}



你可能感兴趣的:(neo4j)