基于spring-boot-starter-data-neo4j操作Neo4j数据库
我翻阅了好多基于spring-boot-starter-data-neo4j来操作neorj数据库的博客,总之没有找到我想要的答案,现在按照自己的思路搞出来了,特分享下:
思路:
1.我想让不同类型的节点都可以用同一种关系,通过属性来区分关系类别
比如 唐僧和孙悟空是师徒关系,孙悟空拥有金箍棒 人物有:唐僧、孙悟空;武器有:金箍棒
2.不同的节点都具有共同的属性,可以方便进行关系的CRUD
进入正题:
1.包结构如图:
3.pom文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.2.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.ikcai</groupId> 12 <artifactId>enginegraph</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>EngineGraph</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-data-neo4j</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 <!-- add this dependency if you want to use the embedded driver --> 31 <dependency> 32 <groupId>org.neo4j</groupId> 33 <artifactId>neo4j-ogm-embedded-driver</artifactId> 34 </dependency> 35 36 <!-- add this dependency if you want to use the HTTP driver --> 37 <dependency> 38 <groupId>org.neo4j</groupId> 39 <artifactId>neo4j-ogm-http-driver</artifactId> 40 </dependency> 41 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 </dependencies> 48 49 <build> 50 <plugins> 51 <plugin> 52 <groupId>org.springframework.boot</groupId> 53 <artifactId>spring-boot-maven-plugin</artifactId> 54 </plugin> 55 </plugins> 56 </build> 57 58 </project>
4.配置文件:GraphDBConfiguration
1 package com.ikcai.enginegraph.config; 2 3 /** 4 * @类功能说明: neo4j配置类 5 * @创建人: 赵力 6 * @创建时间: 2019-01-22 11:56 7 */ 8 9 import org.springframework.boot.autoconfigure.domain.EntityScan; 10 import org.springframework.context.annotation.Configuration; 11 import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; 12 import org.springframework.transaction.annotation.EnableTransactionManagement; 13 14 @Configuration 15 @EnableNeo4jRepositories(basePackages = "com.ikcai.enginegraph.respository") 16 @EntityScan(basePackages = "com.ikcai.enginegraph.domain") 17 @EnableTransactionManagement 18 public class GraphDBConfiguration{ 19 }
5.关系实体对象 BaseRelationship
1 package com.ikcai.enginegraph.domain.base; 2 3 import com.fasterxml.jackson.annotation.JsonIgnore; 4 import com.ikcai.enginegraph.constant.NodeType; 5 import com.ikcai.enginegraph.domain.base.abs.AllBaseEntity; 6 import org.neo4j.ogm.annotation.EndNode; 7 import org.neo4j.ogm.annotation.Property; 8 import org.neo4j.ogm.annotation.RelationshipEntity; 9 import org.neo4j.ogm.annotation.StartNode; 10 11 import java.time.LocalDateTime; 12 13 /** 14 * @类功能说明: 关系实体对象 15 * @创建人: 赵力 16 * @创建时间: 2019-01-23 10:58 17 */ 18 @RelationshipEntity(type=NodeType.REF) 19 public class BaseRelationship<S extends BaseNode, E extends BaseNode> extends AllBaseEntity { 20 21 @Property 22 private String name; //关系名称 23 24 @Property 25 private String createTime; //创建时间 26 27 @JsonIgnore 28 @StartNode 29 private S start; 30 31 @JsonIgnore 32 @EndNode 33 private E end; 34 35 public BaseRelationship(){ 36 } 37 38 public BaseRelationship(String name,S start,E end){ 39 this.name =name; 40 this.createTime=LocalDateTime.now().toString(); 41 this.start=start; 42 this.end=end; 43 } 44 45 @Override 46 public String toString() { 47 return "BaseRelationship{" + 48 "name='" + name + '\'' + 49 ", createTime='" + createTime + '\'' + 50 ", start=" + start + 51 ", end=" + end + 52 '}'; 53 } 54 55 public String getName() { 56 return name; 57 } 58 59 public void setName(String name) { 60 this.name = name; 61 } 62 63 public String getCreateTime() { 64 return createTime; 65 } 66 67 public void setCreateTime(String createTime) { 68 this.createTime = createTime; 69 } 70 71 public S getStart() { 72 return start; 73 } 74 75 public void setStart(S start) { 76 this.start = start; 77 } 78 79 public E getEnd() { 80 return end; 81 } 82 83 public void setEnd(E end) { 84 this.end = end; 85 } 86 }
6.基础节点对象 BaseNode
1 package com.ikcai.enginegraph.domain.base; 2 3 import com.ikcai.enginegraph.constant.NodeType; 4 import com.ikcai.enginegraph.domain.base.abs.AllBaseEntity; 5 import org.neo4j.ogm.annotation.Relationship; 6 7 import java.util.Set; 8 9 /** 10 * @类功能说明: 基础节点对象 11 * @创建人: 赵力 12 * @创建时间: 2019-01-23 10:58 13 */ 14 public class BaseNode extends AllBaseEntity { 15 16 private String name; //名称 17 18 @Relationship(type=NodeType.REF) 19 private Set<BaseRelationship> relationships; //关系列表 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 public Set<BaseRelationship> getRelationships() { 30 return relationships; 31 } 32 33 public void setRelationships(Set<BaseRelationship> relationships) { 34 this.relationships = relationships; 35 } 36 }
7.具体代码位置:https://gitee.com/mailzhaoli/enginegraph/tree/master
8.测试:
8.1添加任务关系:
8.2添加人物武器关系