百度百科:

Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。它是一个

嵌入式的、基于

磁盘的、具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络(从数学角度叫做图)上而不是表中。Neo4j也可以被看作是一个高性能的图引擎,该引擎具有成熟数据库的所有特性。程序员工作在一个面向对象的、灵活的网络结构下而不是严格、静态的表中——但是他们可以享受到具备完全的事务特性、企业级的数据库的所有好处。

Neo4j因其嵌入式、高性能、轻量级等优势,越来越受到关注.

查询语句:

不是SQL,而是Cypher

Cypher关键点

() 表示节点

[] 表示关系

{} 表示属性

> 表示关系的方向

示例:

//查询a的一个叫duchong的朋友

match (a)-[:Friend]->b

where b.name='duchong'

return b

一、安装Neo4j

社区版下载地址:

配置环境变量

NEO4J_HOME

c329fa669b9cca5f2930278b23789884.png

Path

%NEO4J_HOME%\bin

2bd0aadc72c7093f1d4eeb5f5475c984.png

配置完成,cmd

neo4j.bat console

55b17e48ca607ab5a1bb24f09584d98b.png

启动完成 浏览器访问地址为:

localhost:7474

初始用户名和密码均为neo4j

e8edf6e5cd36b3c3420dd32ff96eaf86.png

二、springboot整合

添加依赖

org.springframework.boot

spring-boot-starter-data-neo4j

properties配置

#neo4j

spring.data.neo4j.uri=http://localhost:7474

spring.data.neo4j.username=neo4j

spring.data.neo4j.password=123qwe

User节点

packagecom.dc.sb.web.neo4j;importorg.neo4j.ogm.annotation.GraphId;importorg.neo4j.ogm.annotation.NodeEntity;importorg.neo4j.ogm.annotation.Property;/*** 节点实体类型-别名为User

*@authorDUCHONG

*@since2018-12-17 11:32

**/@NodeEntity(label= "User")public classUserNode {//图形id

@GraphIdprivateLong nodeId;//属性

@PropertyprivateString userId;//属性

@PropertyprivateString userName;//属性

@Propertyprivate intage;publicLong getNodeId() {returnnodeId;

}public voidsetNodeId(Long nodeId) {this.nodeId =nodeId;

}publicString getUserId() {returnuserId;

}public voidsetUserId(String userId) {this.userId =userId;

}publicString getUserName() {returnuserName;

}public voidsetUserName(String userName) {this.userName =userName;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}

}

UserRelation节点

packagecom.dc.sb.web.neo4j;importorg.neo4j.ogm.annotation.EndNode;importorg.neo4j.ogm.annotation.GraphId;importorg.neo4j.ogm.annotation.RelationshipEntity;importorg.neo4j.ogm.annotation.StartNode;/*** 关系节点类型

*@authorDUCHONG

*@since2018-12-17 11:39

**/@RelationshipEntity(type= "UserRelation")public classUserRelation {

@GraphIdprivateLong id;//开始节点

@StartNodeprivateUserNode startNode;//结束节点

@EndNodeprivateEndNode endNode;publicLong getId() {returnid;

}public voidsetId(Long id) {this.id =id;

}publicUserNode getStartNode() {returnstartNode;

}public voidsetStartNode(UserNode startNode) {this.startNode =startNode;

}publicEndNode getEndNode() {returnendNode;

}public voidsetEndNode(EndNode endNode) {this.endNode =endNode;

}

}

UserRepository

packagecom.dc.sb.web.neo4j.dao;importcom.dc.sb.web.neo4j.UserNode;importorg.springframework.data.neo4j.annotation.Query;importorg.springframework.data.neo4j.repository.GraphRepository;importorg.springframework.data.repository.query.Param;importorg.springframework.stereotype.Component;importjava.util.List;/***@authorDUCHONG

*@since2018-12-17 11:42

**/@Componentpublic interface UserRepository extends GraphRepository{

@Query("MATCH (n: User) RETURN n")

ListgetUserNodeList();

@Query("CREATE (n: User{age:{age},userName:{userName}}) RETURN n")

List addUserNodeList(@Param("userName") String userName, @Param("age") intage);

}

UserReloationReposiroty

packagecom.dc.sb.web.neo4j.dao;importcom.dc.sb.web.neo4j.UserRelation;importorg.springframework.data.neo4j.annotation.Query;importorg.springframework.data.neo4j.repository.GraphRepository;importorg.springframework.data.repository.query.Param;importorg.springframework.stereotype.Component;importjava.util.List;/*** 用户关系DO

*@authorDUCHONG

*@since2018-12-17 13:53

**/@Componentpublic interface UserRelationRepository extends GraphRepository{

@Query("match p= (n:User) (n1:User) wheren.userId=(firstUserId) and n1.userId =(secondUserId) return p")

List 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 addUserRelation (@Param(" firstUserId") String firstUserId, @Param("secondUserId") String secondUserId) ;

}

Service

packagecom.dc.sb.web.neo4j.service;importcom.dc.sb.web.neo4j.UserNode;importcom.dc.sb.web.neo4j.dao.UserRepository;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;/***@authorDUCHONG

*@since2018-12-17 14:02

**/@Servicepublic classNeo4jUserService {

@AutowiredprivateUserRepository userRepository;public voidaddUserNode(UserNode userNode){

userRepository.addUserNodeList(userNode.getUserName(),userNode.getAge());

}

}

Controller

packagecom.dc.sb.web.neo4j.controller;importcom.dc.sb.web.neo4j.UserNode;importcom.dc.sb.web.neo4j.service.Neo4jUserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/***@authorDUCHONG

*@since2018-12-17 14:14

**/@RestControllerpublic classNeo4jController {

@AutowiredprivateNeo4jUserService userService;

@RequestMapping("/addUserNode")publicString addUserNode(){

UserNode userNode=newUserNode();

userNode.setUserName("duchong");

userNode.setAge(20);

userService.addUserNode(userNode);return "add success ";

}

}

浏览器访问localhost:8080/addUserNode

676eb059dde5be649e9306d5760957b8.png

查看Neo4j 数据库

835d759b5ff9bebec3590e954075352c.png

完整代码已托管到GitHub ---》欢迎fork

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐