springboot整合图像数据库Neo4j
- () 表示节点
- [] 表示关系
- {} 表示属性
- > 表示关系的方向
示例:
//查询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")
public class UserNode {
//图形id
@GraphId
private Long nodeId;
//属性
@Property
private String userId;
//属性
@Property
private String userName;
//属性
@Property
private 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 { @GraphId
private Long id;
//开始节点
@StartNode
private UserNode startNode;
//结束节点
@EndNode
private 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 { @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
springboot整合图像数据库Neo4j的更多相关文章
- 凭借SpringBoot整合Neo4j,我理清了《雷神》中错综复杂的人物关系
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 虽然距离中秋放假还要熬过漫长的两天,不过也有个好消息,今天是<雷神4>上线Disney+流媒体的日子 ...
- springboot整合springdata-jpa
1.简介 SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
随机推荐
- 第七次scrum meeting记录
文章负责:张华杰 日期:2017年10月31日 会议地点:主楼主南201 各组员工作情况 团队成员 昨日完成任务 明日要完成任务 赵晓宇 课程列表页面搭建 issue20 课程列表页面搭建(part ...
- New Concept English three (31)
35w/m 45 True eccentrics never deliberately set out to draw attention to themselves. They disregard ...
- Intellij IDEA带参数启动Springboot注意事项
问题 不同版本的spring-boot-maven-plugin的jvm参数配置有所不同,同时与通过main方法启动springboot程序传递参数也有所不同. 分析 在运行main方法时,可以通过j ...
- 实战JAVA 高并发设计
一.同步(Synchronous)和异步(Asynchronous) 同步和异步通常用来形容一次方法调用,同步方法,调用者必须等到方法调用返回后,才能继续后续的行为,异步方法调用会立即返回,调用者就可 ...
- AS3里面的错误代码
ActionScript 3 出现2048安全策略服务,一种原因是因为843端口下发策略文件有问题,另一种原因是Socket端口有问题,可以用telnet来测试. 其它AS3错误代码的意义可以在官网文 ...
- rem第一天
Rem为单位 CSS3的出现,他同时引进了一些新的单位,包括我们今天所说的rem.在W3C官网上是这样描述rem的——“font size of the root element” .下面我们就一起来 ...
- js的搜索框
第一种 单独一个form表单提交 <div class="hc-prm-search search flr"> <form action="/user ...
- 初识ADO.NET
摘要 作为.NET框架最重要的组件之一,ADO.NET扮演着应用程序与数据交互的重要的角色.本文将从宏观的角度来探讨ADO.NET,和大家一起了解ADO.NET来龙去脉以及ADO.NET的主要组成部分 ...
- String类的一些常规方法
String类 String类常用方法: ①length(): length() 长度 方法** 对比:数组.length 属性** 一般情况下,一个数字,一个字母,一个汉 ...
- python 无序模块,hashlib模块
无序模块什么是无序模块,将字符串转化为十六进制的字符串类型,主要运用在网络编程中.1,json模块 1,用于网络传输,跨语言,可用的类型有 int str 列表-->元祖会变为列表 字典--&g ...