码迷,mamicode.com
首页 > 编程语言 > 详细

转发:Spring Boot 访问Neo4j

时间:2016-12-03 01:35:30      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:etl   设计   ann   ota   not   com   tor   关系   pos   

(原文: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 ==================

 

转发:Spring Boot 访问Neo4j

标签:etl   设计   ann   ota   not   com   tor   关系   pos   

原文地址:http://www.cnblogs.com/chrischen662/p/6127514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!