springboot整合图像数据库Neo4j
生活随笔
收集整理的這篇文章主要介紹了
springboot整合图像数据库Neo4j
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
百度百科: Neo4j是一個高性能的,NOSQL圖形數據庫,它將結構化數據存儲在網絡上而不是表中。它是一個嵌入式的、基于磁盤的、具備完全的事務特性的Java持久化引擎,但是它將結構化數據存儲在網絡(從數學角度叫做圖)上而不是表中。Neo4j也可以被看作是一個高性能的圖引擎,該引擎具有成熟數據庫的所有特性。程序員工作在一個面向對象的、靈活的網絡結構下而不是嚴格、靜態的表中——但是他們可以享受到具備完全的事務特性、企業級的數據庫的所有好處。 Neo4j因其嵌入式、高性能、輕量級等優勢,越來越受到關注. 查詢語句: 不是SQL,而是Cypher Cypher關鍵點
- () 表示節點
- [] 表示關系
- {} 表示屬性
- > 表示關系的方向
示例:
//查詢a的一個叫duchong的朋友 match (a)-[:Friend]->b where b.name ='duchong' return b一、安裝Neo4j
社區版下載地址:
https://neo4j.com/artifact.php?name=neo4j-community-3.4.10-windows.zip
配置環境變量
NEO4J_HOME
Path
%NEO4J_HOME%\bin
配置完成,cmd
neo4j.bat console
啟動完成 瀏覽器訪問地址為:
localhost:7474
初始用戶名和密碼均為neo4j
二、springboot整合
添加依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-neo4j</artifactId></dependency>properties配置
#neo4j spring.data.neo4j.uri=http://localhost:7474 spring.data.neo4j.username=neo4j spring.data.neo4j.password=123qweUser節點
package com.dc.sb.web.neo4j;import org.neo4j.ogm.annotation.GraphId; import org.neo4j.ogm.annotation.NodeEntity; import org.neo4j.ogm.annotation.Property;/*** 節點實體類型-別名為User* @author DUCHONG* @since 2018-12-17 11:32**/ @NodeEntity(label = "User") public class UserNode {//圖形id @GraphIdprivate Long nodeId;//屬性 @Propertyprivate String userId;//屬性 @Propertyprivate String userName;//屬性 @Propertyprivate int age;public Long getNodeId() {return nodeId;}public void setNodeId(Long nodeId) {this.nodeId = nodeId;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }UserRelation節點
package com.dc.sb.web.neo4j;import org.neo4j.ogm.annotation.EndNode; import org.neo4j.ogm.annotation.GraphId; import org.neo4j.ogm.annotation.RelationshipEntity; import org.neo4j.ogm.annotation.StartNode;/*** 關系節點類型* @author DUCHONG* @since 2018-12-17 11:39**/ @RelationshipEntity(type = "UserRelation") public class UserRelation {@GraphIdprivate Long id;//開始節點 @StartNodeprivate UserNode startNode;//結束節點 @EndNodeprivate EndNode endNode;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public UserNode getStartNode() {return startNode;}public void setStartNode(UserNode startNode) {this.startNode = startNode;}public EndNode getEndNode() {return endNode;}public void setEndNode(EndNode endNode) {this.endNode = endNode;} }UserRepository
package com.dc.sb.web.neo4j.dao;import com.dc.sb.web.neo4j.UserNode; import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.repository.GraphRepository; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Component;import java.util.List;/*** @author DUCHONG* @since 2018-12-17 11:42**/ @Component public interface UserRepository extends GraphRepository<UserNode> {@Query("MATCH (n: User) RETURN n")List<UserNode> getUserNodeList();@Query("CREATE (n: User{age:{age},userName:{userName}}) RETURN n")List<UserNode> addUserNodeList(@Param("userName") String userName, @Param("age") int age);}UserReloationReposiroty
package com.dc.sb.web.neo4j.dao;import com.dc.sb.web.neo4j.UserRelation; import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.repository.GraphRepository; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Component;import java.util.List;/*** 用戶關系DO* @author DUCHONG* @since 2018-12-17 13:53**/ @Component public interface UserRelationRepository extends GraphRepository<UserRelation> {@Query("match p= (n:User)<- [r: UserRelation]-> (n1:User) wheren.userId=(firstUserId) and n1.userId =(secondUserId) return p")List<UserRelation> findUserRelationByEachId (@Param(" firstUserId") String firstUserId, @Param ("secondUserId") String secondUserId) ;@Query ("match(fu:User) ,(su:User) where fu. userId=[firstUserId]and su.userId=[ secondUserId, create p= (fu)- [r:UserRelation]-> (su) return p")List<UserRelation> addUserRelation (@Param(" firstUserId") String firstUserId, @Param("secondUserId") String secondUserId) ; }Service
package com.dc.sb.web.neo4j.service;import com.dc.sb.web.neo4j.UserNode; import com.dc.sb.web.neo4j.dao.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;/*** @author DUCHONG* @since 2018-12-17 14:02**/ @Service public class Neo4jUserService {@Autowiredprivate UserRepository userRepository;public void addUserNode(UserNode userNode){userRepository.addUserNodeList(userNode.getUserName(),userNode.getAge());} }Controller
package com.dc.sb.web.neo4j.controller;import com.dc.sb.web.neo4j.UserNode; import com.dc.sb.web.neo4j.service.Neo4jUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author DUCHONG* @since 2018-12-17 14:14**/ @RestController public class Neo4jController {@Autowiredprivate Neo4jUserService userService;@RequestMapping("/addUserNode")public String addUserNode(){UserNode userNode=new UserNode();userNode.setUserName("duchong");userNode.setAge(20);userService.addUserNode(userNode);return "add success ";} }瀏覽器訪問localhost:8080/addUserNode
查看Neo4j 數據庫
?
完整代碼已托管到GitHub ---》歡迎fork
?
轉載于:https://www.cnblogs.com/geekdc/p/10131658.html
總結
以上是生活随笔為你收集整理的springboot整合图像数据库Neo4j的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kubernetes 支持 OpenAP
- 下一篇: vue+lowdb+express