springboot_neo4j

neo4j是一个图形数据库,有一个做关系图谱的需求里面需要使用到图形数据库。

w3c教程:https://www.w3cschool.cn/neo4j/

中文版的数据库可以通过image属性显示图片,官网的动画效果是通过3D.js实现的;

pom导入配置

springboot_neo4j
1  <dependency>
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-data-neo4j</artifactId>
4         </dependency>
5  <dependency>
6             <groupId>org.neo4j</groupId>
7             <artifactId>neo4j-ogm-http-driver</artifactId>
8             <version>3.1.4</version>
9         </dependency>
View Code

 

配置数据库:

springboot_neo4j
 1 #neo4j
 2 spring.data.neo4j.username=neo4j
 3 spring.data.neo4j.password=123
 4 spring.data.neo4j.uri=http://192.168.100.106:7474
 5 package com.koala.console.configuration;
 6 
 7 import org.neo4j.ogm.session.SessionFactory;
 8 import org.springframework.beans.factory.annotation.Value;
 9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
12 import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
13 import org.springframework.transaction.annotation.EnableTransactionManagement;
14 
15 @Configuration
16 @EnableNeo4jRepositories(basePackages = "com.koala.console.repository.neo4j")
17 @EnableTransactionManagement
18 public class Neo4jConfig {
19 
20     @Value("${blotUri}")
21     private String uri;
22 
23     @Value("${spring.data.neo4j.uri}")
24     private String databaseUrl;
25 
26     @Value("${spring.data.neo4j.username}")
27     private String userName;
28 
29     @Value("${spring.data.neo4j.password}")
30     private String password;
31 
32     @Bean
33     public SessionFactory sessionFactory() {
34         org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder()
35                 .uri(databaseUrl)
36                 .credentials(userName, password)
37                 .build();
38         return new SessionFactory(configuration, "com.koala.console.model.neo4j");
39     }
40 
41     @Bean
42     public Neo4jTransactionManager transactionManager() {
43         return new Neo4jTransactionManager(sessionFactory());
44     }
45 
46     
47 }
View Code

使用neo4j:

neo4j基本由两部分组成,节点和关系,关系用来指示两个节点的方向和关联属性:

节点bean:

springboot_neo4j
  1 package com.koala.console.model.neo4j;
  2 
  3 import com.alibaba.fastjson.annotation.JSONField;
  4 import com.fasterxml.jackson.annotation.JsonIgnore;
  5 import com.google.common.collect.Lists;
  6 import lombok.Data;
  7 import org.neo4j.ogm.annotation.*;
  8 import org.springframework.web.multipart.MultipartFile;
  9 
 10 import java.util.List;
 11 
 12 @Data
 13 @NodeEntity
 14 public class FaceNode {
 15 
 16     public FaceNode() {
 17     }
 18 
 19     public FaceNode(String name) {
 20         this.objId = name;
 21     }
 22 
 23     @Id
 24     @GeneratedValue
 25     private Long id;
 26 
 27     /**
 28      * objId
 29      */
 30     private String objId;
 31 
 32 
 33     @JSONField(serialize = false)
 34     @JsonIgnore
 35     @Transient
 36     private String startName;
 37     @JSONField(serialize = false)
 38     @JsonIgnore
 39     @Transient
 40     private String endName;
 41 
 42     @Transient
 43     private Integer pathNum;
 44 
 45     @Transient
 46     private String intimacy;
 47 
 48     @Property(name = "scenes")
 49     private String scenes;
 50 
 51     @Property(name = "image")
 52     private String symbol;
 53 
 54     @Transient
 55     private Integer[] symbolSize = {60,60};
 56 
 57     @JSONField(serialize = false)
 58     @Transient
 59     private MultipartFile faceImg;
 60     @JSONField(serialize = false)
 61     @Transient
 62     private String startTime;
 63 
 64     @JSONField(serialize = false)
 65     @Transient
 66     private String endTime;
 67 
 68 
 69 
 70     @Relationship(type = "relation")
 71     private List<FaceNodePro> faceNodePros = Lists.newArrayList();
 72 
 73     private List<FaceNode>  children = Lists.newArrayList();
 74 
 75     public void addEndNode(FaceNode endNode, String title) {
 76         FaceNodePro pro = new FaceNodePro(title, this, endNode);
 77         this.faceNodePros.add(pro);
 78     }
 79 
 80     public void addStartNode(FaceNode startNode, String title) {
 81         FaceNodePro pro = new FaceNodePro(title, startNode, this);
 82         this.faceNodePros.add(pro);
 83     }
 84 
 85     public Long getId() {
 86         return id;
 87     }
 88 
 89     public void setId(Long id) {
 90         this.id = id;
 91     }
 92 
 93     public String getObjId() {
 94         return objId;
 95     }
 96 
 97     public void setObjId(String objId) {
 98         this.objId = objId;
 99     }
100 
101     public String getSymbol() {
102         return symbol;
103     }
104 
105     public void setSymbol(String symbol) {
106         this.symbol = symbol;
107     }
108 
109     public List<FaceNodePro> getFaceNodePros() {
110         return faceNodePros;
111     }
112 
113     public void setFaceNodePros(List<FaceNodePro> faceNodePros) {
114         this.faceNodePros = faceNodePros;
115     }
116 
117     public String getStartName() {
118         return startName;
119     }
120 
121     public void setStartName(String startName) {
122         this.startName = startName;
123     }
124 
125     public String getEndName() {
126         return endName;
127     }
128 
129     public void setEndName(String endName) {
130         this.endName = endName;
131     }
132 
133     public MultipartFile getFaceImg() {
134         return faceImg;
135     }
136 
137     public void setFaceImg(MultipartFile faceImg) {
138         this.faceImg = faceImg;
139     }
140 
141     public String getStartTime() {
142         return startTime;
143     }
144 
145     public void setStartTime(String startTime) {
146         this.startTime = startTime;
147     }
148 
149     public String getEndTime() {
150         return endTime;
151     }
152 
153     public void setEndTime(String endTime) {
154         this.endTime = endTime;
155     }
156 
157     public String getScenes() {
158         return scenes;
159     }
160 
161     public void setScenes(String scenes) {
162         this.scenes = scenes;
163     }
164 }
View Code

 

关系bean:

springboot_neo4j
 1 package com.koala.console.model.neo4j;
 2 
 3 import com.fasterxml.jackson.annotation.JsonBackReference;
 4 import org.neo4j.ogm.annotation.*;
 5 
 6 @RelationshipEntity(type = "relation")
 7 public class FaceNodePro {
 8 
 9     public FaceNodePro() {
10     }
11 
12     public FaceNodePro(String intimacy, FaceNode startNode, FaceNode endNode) {
13         this.intimacy = intimacy;
14         this.startNode = startNode;
15         this.endNode = endNode;
16     }
17 
18     @Id
19     @GeneratedValue
20     private Long nodeProId;
21 
22     /**
23      * 亲密度
24      */
25     @Property
26     private String intimacy;
27 
28     @JsonBackReference
29     @StartNode
30     private FaceNode startNode;
31 
32     @EndNode
33     private FaceNode endNode;
34 
35     public Long getNodeProId() {
36         return nodeProId;
37     }
38 
39     public void setNodeProId(Long nodeProId) {
40         this.nodeProId = nodeProId;
41     }
42 
43     public String getIntimacy() {
44         return intimacy;
45     }
46 
47     public void setIntimacy(String intimacy) {
48         this.intimacy = intimacy;
49     }
50 
51     public FaceNode getStartNode() {
52         return startNode;
53     }
54 
55     public void setStartNode(FaceNode startNode) {
56         this.startNode = startNode;
57     }
58 
59     public FaceNode getEndNode() {
60         return endNode;
61     }
62 
63     public void setEndNode(FaceNode endNode) {
64         this.endNode = endNode;
65     }
66 }
View Code

 

Repository实现:
springboot_neo4j
 1 @Repository
 2 public interface FaceNodeRepository extends Neo4jRepository<FaceNode, Long> {
 3 
 4     FaceNode findByObjId(@Param("objId") String objId);
 5 
 6     /**
 7      * 查询节点Node指定层级的图
 8      *
 9      * @param
10      * @return
11      */
12     @Query(value = "MATCH n=(:FaceNode{objId:{objId}})-[*..6]->() RETURN n")
13     List<FaceNode> findByHierarchical(@Param("objId") String objId);
14 
15 }
View Code

 

剩下的使用都是很简单的事情。

我收集了一些会使用到的语句:

springboot_neo4j
 1 查询节点Node所有的关系
 2 match(Node{name:'5c26219bd3e2dca5322110bb'})-[:PLAYED_IN]->(yf)return Node,yf
 3 查询节点Node的2层关系图
 4 MATCH n=(:Node{name:"5c262177d3e2dca5322110b3"})-[*..1]-() return n
 5 两个陌生人之间的所有最短认识路径
 6 MATCH n = allshortestPaths((小讯:朋友圈{姓名:"小讯"})-[*..6]-(小菲:朋友圈{姓名:"小菲"})) return n
 7 查询节点Node指向的所有节点
 8 MATCH (:Node { name: '5c262137d3e2dca5322110a7' })-->(movie)RETURN movie;
 9 查询标签Node所有节点
10 match(n)--(m:Node)return n;
11 批量创建Node节点的朋友圈
12 MATCH (Node:Node {name:"4j2ap"})FOREACH (name in ["Johan","Rajesh","Anna","Julia","Andrew"] |CREATE (Node)-[:FRIEND]->(:Person {name:name}))
13 查询两个节点有向关系条数
14 match (:FaceNode{name:"gong"})-[r:PLAYED_IN]->(:FaceNode{name:"eza2e"})return count(r)
View Code

参考:

转载自:https://www.cxymm.net/article/sinat_21184471/87092034
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/sinat_21184471/article/details/87092034
上一篇:从源码分析DEARGUI之添加字体


下一篇:【网站制作设计】静态与动态网页的特点及区别