1. The jar package to be imported by MAVEN project
<!--neo4j--
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.3.2</version>
</dependency>
2. jar package corresponding to spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
3, load the neo4j drive configuration
@Configuration @enableneo4jrepositories ("Com.neo4j.repository") @EnableTransactionManagement Public classNeo4japplicationextendsneo4jconfiguration { Public Static Final intNeo4j_port = 7474; @Bean Publicsessionfactory getsessionfactory () {return NewSessionfactory ("Com.neo4j.domain"); }//Configure Transaction @Bean @Qualifier ("Neo4jtransactionmanager") PublicNeo4jtransactionmanager Neo4jtransactionmanager ()throwsException {return NewNeo4jtransactionmanager (GetSession ()); }}
4, the connection method uses Httpdriver
Add configuration file Ogm.properties
Driver=org.neo4j.ogm.drivers.http.driver.httpdriver
Uri=http://neo4j:[email protected]:7474
5. Domain Entity Configuration
//node annotations (you can add label labels)@NodeEntity Public classthing{//ID of the node in neo4j PrivateLong ID; PublicLong getId () {returnID; } @Override Public Booleanequals (Object o) {if( This==o)return true; if(O = =NULL|| id = =NULL|| GetClass ()! =O.getclass ())return false; Entity entity=(Entity) o; if(!id.equals (entity.id))return false; return true; } @Override Public inthashcode () {return(id = =NULL) ? -1: Id.hashcode (); } //Properties PrivateString userId; PrivateString name; PrivateString desc; //Configuring Transformations@DateString ("Yy-mm-dd") PrivateDate DateTime; //Set Relationship@Relationship (type = "Have_prop", direction=relationship.outgoing) List<Property> Properties =NewArraylist<property>(); @Relationship (Type= "Have_service", direction=relationship.outgoing) Set<Service> Services =NewHashset<service>(); @Relationship (Type= "Have_prop") Set<ThingPropRelation> proprelations =NewHashset<thingproprelation>(); @Relationship (Type= "Have_service") Set<ThingServiceRelation> servicerelations =NewHashset<thingservicerelation>(); @Relationship (Type= "Have_service") Set<ThingServiceRelation> servicerelations =NewHashset<thingservicerelation>();}
Set the relationship of a node
//set up a relationship entity@RelationshipEntity (type= "Have_prop") Public classThingproprelationextendsEntity {//Start Node@StartNode Thing Thing; //End Node@EndNode property; Publicthingproprelation () {} PublicThing getthing () {returnthing; } Public voidsetthing (Thing Thing) { This. Thing =thing; } PublicProperty GetProperty () {returnProperty ; } Public voidSetProperty (property) { This. property =Property ; }}
6. Set Repository
// Interface Inheritance Graphrepository // provides the basic save Modify Delete query function // Special queries can be implemented by @query annotations Public Interface extends Graphrepository<thing> { Thing findbyname (String name); @Query ("MATCH (t:thing {name:{0}})-[r:have_prop]-> (p) RETURN P") iterable<Property> getthingpropertybythingname (String thingname);}
7. Application
@Autowired Private
Call
Thing Thing = Thingrepository.findbyname ("Thing");
NEO4J and Springdata Integration