首页 > 代码库 > 转发:Spring Boot 访问Neo4j

转发:Spring Boot 访问Neo4j

(原文: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;@NodeEntitypublic 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;@Repositorypublic interface UserRepository extends GraphRepository<User> {    User findByName(String name);}

  

使用嵌入式方式连接数据库:

compiler=org.neo4j.ogm.compiler.MultiStatementCypherCompilerdriver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriverURI=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 ==================

 

转发:Spring Boot 访问Neo4j