程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 轉發:Spring Boot 訪問Neo4j,springneo4j

轉發:Spring Boot 訪問Neo4j,springneo4j

編輯:JAVA綜合教程

轉發:Spring Boot 訪問Neo4j,springneo4j


(原文:https://my.oschina.net/syic/blog/798104)

在Spring Boot中訪問Neo4j,通過Spring Data Neo4j可以輕易地實現。

例如,現有如下圖所示的數據模型:

這表示有兩個節點:用戶和組,並且這兩個節點的關系是從屬關系,即用戶從屬於組。

根據這個數據模型,使用Spring Data Neo4j進行建模。

用戶節點建模:

package com.demo.neo4j.domain;

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

@NodeEntity
public class User {
   @GraphId
   private Long id;
   private String name;

   @Relationship(type = "從屬", direction = Relationship.OUTGOING)
   private Group group;

   public User() {
   }

   public Long getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

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

   public Group getGroup() {
      return group;
   }

   public void setGroup(Group group) {
      this.group = group;
   }
}

  

組節點建模:

package com.demo.neo4j.domain;

import org.neo4j.ogm.annotation.GraphId;

public class Group {
    @GraphId
    private Long id;
    private String name;

    public Group() {
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

  

數據持久化設計:

package com.demo.neo4j.repository;

import com.demo.neo4j.domain.User;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends GraphRepository<User> {
    User findByName(String name);
}

  

使用嵌入式方式連接數據庫:

compiler=org.neo4j.ogm.compiler.MultiStatementCypherCompiler
driver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver
URI=file:///test/graph.db

  

測試用例:

package com.demo.neo4j.test;

import com.demo.neo4j.config.Neo4jConfig;
import com.demo.neo4j.domain.Group;
import com.demo.neo4j.domain.User;
import com.demo.neo4j.repository.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Neo4jConfig.class})
public class Neo4jTest {
    private static Logger log = LoggerFactory.getLogger(Neo4jTest.class);

    @Autowired
    private UserRepository userRepository;

    @Before
    public void addData() {
        Group group = new Group();
        group.setName("admins");

        User user = new User();
        user.setName("admin");
        user.setGroup(group);

        userRepository.save(user);
        Assert.notNull(user.getId());
    }

    @Test
    public void getData() {
        User user = userRepository.findByName("admin");
        Assert.notNull(user);
        log.info("\n================== user name={}; group name={} ==================",
                user.getName(), user.getGroup().getName());
    }
}

  

運行結果:

================== user name=admin; group name=admins ==================

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved