springboot neo4j的配置代碼
neo4j是一個圖形數據庫,有一個做關系圖譜的需求裡面需要使用到圖形數據庫。
w3c教程:https://www.w3cschool.cn/neo4j/
中文版的數據庫可以通過image屬性顯示圖片,官網的動畫效果是通過3D.js實現的;
pom導入配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-http-driver</artifactId> <version>3.1.4</version> </dependency>
配置數據庫:
#neo4j spring.data.neo4j.username=neo4j spring.data.neo4j.password=123 spring.data.neo4j.uri=http://192.168.100.106:7474 package com.koala.console.configuration; import org.neo4j.ogm.session.SessionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; import org.springframework.data.neo4j.transaction.Neo4jTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableNeo4jRepositories(basePackages = "com.koala.console.repository.neo4j") @EnableTransactionManagement public class Neo4jConfig { @Value("${blotUri}") private String uri; @Value("${spring.data.neo4j.uri}") private String databaseUrl; @Value("${spring.data.neo4j.username}") private String userName; @Value("${spring.data.neo4j.password}") private String password; @Bean public SessionFactory sessionFactory() { org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder() .uri(databaseUrl) .credentials(userName, password) .build(); return new SessionFactory(configuration, "com.koala.console.model.neo4j"); } @Bean public Neo4jTransactionManager transactionManager() { return new Neo4jTransactionManager(sessionFactory()); } }
使用neo4j:
neo4j基本由兩部分組成,節點和關系,關系用來指示兩個節點的方向和關聯屬性:
節點bean:
package com.koala.console.model.neo4j; import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonIgnore; import com.google.common.collect.Lists; import lombok.Data; import org.neo4j.ogm.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; @Data @NodeEntity public class FaceNode { public FaceNode() { } public FaceNode(String name) { this.objId = name; } @Id @GeneratedValue private Long id; /** * objId */ private String objId; @JSONField(serialize = false) @JsonIgnore @Transient private String startName; @JSONField(serialize = false) @JsonIgnore @Transient private String endName; @Transient private Integer pathNum; @Transient private String intimacy; @Property(name = "scenes") private String scenes; @Property(name = "image") private String symbol; @Transient private Integer[] symbolSize = {60,60}; @JSONField(serialize = false) @Transient private MultipartFile faceImg; @JSONField(serialize = false) @Transient private String startTime; @JSONField(serialize = false) @Transient private String endTime; @Relationship(type = "relation") private List<FaceNodePro> faceNodePros = Lists.newArrayList(); private List<FaceNode> children = Lists.newArrayList(); public void addEndNode(FaceNode endNode, String title) { FaceNodePro pro = new FaceNodePro(title, this, endNode); this.faceNodePros.add(pro); } public void addStartNode(FaceNode startNode, String title) { FaceNodePro pro = new FaceNodePro(title, startNode, this); this.faceNodePros.add(pro); } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getObjId() { return objId; } public void setObjId(String objId) { this.objId = objId; } public String getSymbol() { return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } public List<FaceNodePro> getFaceNodePros() { return faceNodePros; } public void setFaceNodePros(List<FaceNodePro> faceNodePros) { this.faceNodePros = faceNodePros; } public String getStartName() { return startName; } public void setStartName(String startName) { this.startName = startName; } public String getEndName() { return endName; } public void setEndName(String endName) { this.endName = endName; } public MultipartFile getFaceImg() { return faceImg; } public void setFaceImg(MultipartFile faceImg) { this.faceImg = faceImg; } public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; } public String getScenes() { return scenes; } public void setScenes(String scenes) { this.scenes = scenes; } }
關系bean:
package com.koala.console.model.neo4j; import com.fasterxml.jackson.annotation.JsonBackReference; import org.neo4j.ogm.annotation.*; @RelationshipEntity(type = "relation") public class FaceNodePro { public FaceNodePro() { } public FaceNodePro(String intimacy, FaceNode startNode, FaceNode endNode) { this.intimacy = intimacy; this.startNode = startNode; this.endNode = endNode; } @Id @GeneratedValue private Long nodeProId; /** * 親密度 */ @Property private String intimacy; @JsonBackReference @StartNode private FaceNode startNode; @EndNode private FaceNode endNode; public Long getNodeProId() { return nodeProId; } public void setNodeProId(Long nodeProId) { this.nodeProId = nodeProId; } public String getIntimacy() { return intimacy; } public void setIntimacy(String intimacy) { this.intimacy = intimacy; } public FaceNode getStartNode() { return startNode; } public void setStartNode(FaceNode startNode) { this.startNode = startNode; } public FaceNode getEndNode() { return endNode; } public void setEndNode(FaceNode endNode) { this.endNode = endNode; } }
Repository實現:
@Repository public interface FaceNodeRepository extends Neo4jRepository<FaceNode, Long> { FaceNode findByObjId(@Param("objId") String objId); /** * 查詢節點Node指定層級的圖 * * @param * @return */ @Query(value = "MATCH n=(:FaceNode{objId:{objId}})-[*..6]->() RETURN n") List<FaceNode> findByHierarchical(@Param("objId") String objId); }
剩下的使用都是很簡單的事情。
我收集瞭一些會使用到的語句:
查詢節點Node所有的關系 match(Node{name:'5c26219bd3e2dca5322110bb'})-[:PLAYED_IN]->(yf)return Node,yf 查詢節點Node的2層關系圖 MATCH n=(:Node{name:"5c262177d3e2dca5322110b3"})-[*..1]-() return n 兩個陌生人之間的所有最短認識路徑 MATCH n = allshortestPaths((小訊:朋友圈{姓名:"小訊"})-[*..6]-(小菲:朋友圈{姓名:"小菲"})) return n 查詢節點Node指向的所有節點 MATCH (:Node { name: '5c262137d3e2dca5322110a7' })-->(movie)RETURN movie; 查詢標簽Node所有節點 match(n)--(m:Node)return n; 批量創建Node節點的朋友圈 MATCH (Node:Node {name:"4j2ap"})FOREACH (name in ["Johan","Rajesh","Anna","Julia","Andrew"] |CREATE (Node)-[:FRIEND]->(:Person {name:name})) 查詢兩個節點有向關系條數 match (:FaceNode{name:"gong"})-[r:PLAYED_IN]->(:FaceNode{name:"eza2e"})return count(r)
參考:
轉載自:https://www.cxymm.net/article/sinat_21184471/87092034
版權聲明:本文為博主原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/sinat_21184471/article/details/87092034
到此這篇關於springboot neo4j的文章就介紹到這瞭,更多相關springboot neo4j內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- JAVA入門教學之快速搭建基本的springboot(從spring boot到spring cloud)
- SpringBoot返回Json對象報錯(返回對象為空{})
- 解決springboot 實體類String轉Date類型的坑
- springboot 緩存@EnableCaching實例
- springboot使用redis的詳細步驟