Forward: Spring Boot accesses Neo4j and springneo4j

Source: Internet
Author: User

Forward: Spring Boot accesses Neo4j and springneo4j

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

Access Neo4j in Spring Boot, which can be easily implemented through Spring Data Neo4j.

For example, the existing data model shown in:

This indicates that there are two nodes: user and group, and the relationship between the two nodes is subordinate, that is, the user belongs to the group.

Based on this Data model, use Spring Data Neo4j for modeling.

User node Modeling:

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 = "subordinate", direction = Relationship. OUTGOING) private 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 Group) {this. group = group ;}}

  

Group node Modeling:

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;    }}

  

Data persistence design:

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);}

  

Use an embedded method to connect to the database:

compiler=org.neo4j.ogm.compiler.MultiStatementCypherCompilerdriver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriverURI=file:///test/graph.db

  

Test cases:

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());    }}

  

Running result:

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.