• Spring boot项目集成Neo4j


    第一步,创建Springboot工程 使用Eclipse 创建Maven项目,并修改pom.xml文件为:

    <?xml version="1.0" encoding="UTF-8"?>
    <project 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">
       <modelVersion>4.0.0</modelVersion>
    

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

      </span><span class="tag">&lt;dependency&gt;</span><span class="pln">
         </span><span class="tag">&lt;groupId&gt;</span><span class="pln">org.springframework.boot</span><span class="tag">&lt;/groupId&gt;</span><span class="pln">
         </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">spring-boot-starter-test</span><span class="tag">&lt;/artifactId&gt;</span><span class="pln">
         </span><span class="tag">&lt;scope&gt;</span><span class="pln">test</span><span class="tag">&lt;/scope&gt;</span><span class="pln">
      </span><span class="tag">&lt;/dependency&gt;</span><span class="pln">
    
      </span><span class="tag">&lt;dependency&gt;</span><span class="pln">
         </span><span class="tag">&lt;groupId&gt;</span><span class="pln">org.springframework.boot</span><span class="tag">&lt;/groupId&gt;</span><span class="pln">
         </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">spring-boot-starter-data-neo4j</span><span class="tag">&lt;/artifactId&gt;</span><span class="pln">
      </span><span class="tag">&lt;/dependency&gt;</span><span class="pln">
    

    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
    </project>

    第二步,修改配置文件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;

    </span><span class="lit">@StartNode</span><span class="pln">
    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">UserNode</span><span class="pln"> startNode</span><span class="pun">;</span><span class="pln">
    
    </span><span class="lit">@EndNode</span><span class="pln">
    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">UserNode</span><span class="pln"> endNode</span><span class="pun">;</span><span class="pln">
    
    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">Long</span><span class="pln"> getId</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> id</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    
    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setId</span><span class="pun">(</span><span class="typ">Long</span><span class="pln"> id</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">id </span><span class="pun">=</span><span class="pln"> id</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    
    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">UserNode</span><span class="pln"> getStartNode</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> startNode</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    
    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setStartNode</span><span class="pun">(</span><span class="typ">UserNode</span><span class="pln"> startNode</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">startNode </span><span class="pun">=</span><span class="pln"> startNode</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    
    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">UserNode</span><span class="pln"> getEndNode</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> endNode</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    
    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setEndNode</span><span class="pun">(</span><span class="typ">UserNode</span><span class="pln"> endNode</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">endNode </span><span class="pun">=</span><span class="pln"> endNode</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    

    }

    第四步,repository文件----UserRepository和UserRelationRepository,@Query里面编写Cypher查询

    
    @Component
    public interface UserRepository extends GraphRepository<UserNode> {
    
        [@Query](/user/Query)("MATCH (n:User) RETURN n ")
        List<UserNode> getUserNodeList();
    
        [@Query](/user/Query)("create (n:User{age:{age},name:{name}}) RETURN n ")
        List<UserNode> addUserNodeList(@Param("name") String name, @Param("age")int age);
    }
    
    @Component
    public interface UserRelationRepository extends GraphRepository<UserRelation> {
    
        [@Query](/user/Query)("match p=(n:User)<-[r:UserRelation]->(n1:User) where n.userId={firstUserId} and n1.userId={secondUserId} return p")
        List<UserRelation> findUserRelationByEachId(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);
    
        [@Query](/user/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);
    
    }

    第五步,修改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 {
    }
    原文地址:http://neo4j.com.cn/topic/5a935b4a2aa9336e06418eca
  • 相关阅读:
    Python try/except/finally
    EmailMessage类
    HTML DOM 初学笔记
    JavaScript 初学备忘录
    html style标签
    Django 导出csv文件 中文乱码问题
    Html 列表实现展开和收起
    Django CreateView 简单使用
    Django用户认证
    Nginx负载均衡配置实例详解
  • 原文地址:https://www.cnblogs.com/jpfss/p/11578248.html
Copyright © 2020-2023  润新知