springboot整合图像数据库Neo4j
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了springboot整合图像数据库Neo4j,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5664字,纯文字阅读大概需要9分钟。
内容图文

- () 表示节点
- [] 表示关系
- {} 表示属性
- > 表示关系的方向
示例:
// 查询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=123qwe
User节点
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") publicclass UserNode { //图形id @GraphId private Long nodeId; //属性 @Property private String userId; //属性 @Property private String userName; //属性 @Property privateint age; public Long getNodeId() { return nodeId; } publicvoid setNodeId(Long nodeId) { this.nodeId = nodeId; } public String getUserId() { return userId; } publicvoid setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } publicvoid setUserName(String userName) { this.userName = userName; } publicint getAge() { return age; } publicvoid 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") publicclass UserRelation { @GraphId private Long id; //开始节点 @StartNode private UserNode startNode; //结束节点 @EndNode private EndNode endNode; public Long getId() { return id; } publicvoid setId(Long id) { this.id = id; } public UserNode getStartNode() { return startNode; } publicvoid setStartNode(UserNode startNode) { this.startNode = startNode; } public EndNode getEndNode() { return endNode; } publicvoid 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 { @Autowired private 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 { @Autowired private 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全部内容,希望文章能够帮你解决springboot整合图像数据库Neo4j所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。