百度百科:  
  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=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的更多相关文章

  1. 凭借SpringBoot整合Neo4j,我理清了《雷神》中错综复杂的人物关系

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 虽然距离中秋放假还要熬过漫长的两天,不过也有个好消息,今天是<雷神4>上线Disney+流媒体的日子 ...

  2. springboot整合springdata-jpa

    1.简介  SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...

  3. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

  4. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  5. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  6. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  7. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  8. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  9. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

随机推荐

  1. ./startup.sh: /bin/sh^M: bad interpreter: 没有那个文件或目录 解决办法

    这是因为Linux上 的catalina.sh文件格式给修改了,看不出来,这样就必须通过vim编辑下,变为正常的格式,在catalina.sh的命令模式下输入  ( :set ff=unix ),接着 ...

  2. Android中UI线程与后台线程交互设计的6种方法

    在android的设计思想中,为了确保用户顺滑的操作体验.一些耗时的任务不能够在UI线程中运行,像访问网络就属于这类任务.因此我们必须要重新开启 一个后台线程运行这些任务.然而,往往这些任务最终又会直 ...

  3. Java基础学习-extends继承(成员变量,局部变量,成员方法)

    package extend; /*面向对象-继承: * 多个类的共同成员变量和成员方法.抽取到另一个类中(父类),我们多个类就可以访问到父类的成员了 * */ class Game{ String ...

  4. 原创:项目管理的理论与实践 讲座的PPT

    业余时间做的两个PPT,曾经给公司同事讲过,PPT内容毕竟还是不够全面,如果有不清楚的地方,欢迎提问 项目管理的理论与实践 虚拟案例-超市管理系统

  5. 学习window.open()及问题分析

    以前对window.open()理解的不透彻,最近因为产品需要,重新学习了一下,以下为一些收获和问题总结: 调用方式:window.open(url , winName , style); url:弹 ...

  6. Win 10 +python3.5 之sklearn 的安装

    一.文件下载 1.sklearn 需要在 numpy+mkl  安装之后和scipy 安装之后才可以安装. 2.scipy 在numpy+mkl安装之后才可以安装. 因此,三个软件的安装顺序是:num ...

  7. php 设计数据库连接池

    摘要 之前总是以脚本面向过程的方式写PHP代码,所以很大程度上来说,既不规范,也不安全,更不容易维护.为了代码的重用,准备写一套自己的工具库,这样的话,以后写项目的时候就可以很轻松的进行使用啦. 今天 ...

  8. python库之selectors

    在之前的博客中已经总结过分别在windows和linux操作系统下实现socket高并发(I/O异步)的方法,可以参考基于epoll的TP传输层实现和Windows之IOCP 下面对Python中实现 ...

  9. jqprint网页打印时有页码和URL

    环境360浏览器或IE,解决方法在浏览器->文件->打印,把页码页脚勾选去掉. IE浏览器,打印->页码设置,把页眉页脚都置成空.

  10. leetcode 21.Merge Two Sorted Lists ,java

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...