(Original: https://my.oschina.net/syic/blog/798104)
Access to the neo4j in spring boot is easy to implement with spring Data neo4j.
For example, the existing data model is as shown:
This means that there are two nodes: the user and the group, and the relationship of the two nodes is a dependency, that is, the user belongs to the group.
Based on this data model, the spring data neo4j is used 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 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; }}
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);}
To connect to a database using an inline method:
Compiler=org.neo4j.ogm.compiler.multistatementcyphercompilerdriver= Org.neo4j.ogm.drivers.embedded.driver.embeddeddriveruri=file:///test/graph.db
Test Case:
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 ()); }}
Operation Result:
================== user name=admin; Group Name=admins ==================
Forwarding: Spring Boot access neo4j