当前位置: 首页 > news >正文

neo4j(spring) 使用示例

文章目录

  • 前言
  • 一、neo4j是什么
  • 二、开始编码
    • 1. yml 配置
    • 2. crud 测试
    • 3. node relation 与java中对象的关系
    • 4. 编码测试
  • 总结


前言

图数据库先驱者 neo4j:neo4j官网地址

  • 可以选择桌面版安装等多种方式,我这里采用的是docker安装

  • 直接执行docker安装命令:

    docker run -d -p 7474:7474 -p 7687:7687 --name neo4j -e "NEO4J_AUTH=neo4j/password"  neo4jchina/neo4j-chs
    

    如果无法下载的话,请更新下docker仓库镜像源地址

  • 可以参考 docker镜像源地址


一、neo4j是什么

  1. Neo4j 是一个高性能、开源的图数据库管理系统,主要用于存储、管理和查询具有复杂关系的数据。它采用属性图模型来处理数据,其中数据被表示为节点(Nodes)和关系(Relationships)的集合,形成了图(Graph)结构。
  2. Neo4j 使用 Cypher 查询语言,是一种图形查询语言。写的比较好的一遍关于 Cypher语法 的文章

二、开始编码

组件版本
springboot2.7.6
spring-boot-starter-data-neo4j2.7.6
hutool-all5.8.4

1. yml 配置

server:port: 8080
spring:neo4j:uri: bolt://localhost:7687authentication:username: neo4jpassword: passworddata:neo4j:database: neo4j
logging:level:org.springframework.data.neo4j: DEBUG

这里连接的是我本地docker 安装的neo4j
本地安装截图

有多个端口默认7474为管理页面,7687为服务端口,所以yml这里用7687端口


  • 桌面安装也很好用,这里采用windows安装
    桌面版本

可以自己新建数据库,而docker中是无法自己创建数据库的

2. crud 测试

  1. 构思graph 的结构
  2. 确定多个relation 关系
  3. 确定各个关系的两个node 节点
    首先要规划好这些关系,然后构造出一幅图出来
    例如:
    最终的图

这是一个电影关系

  1. 导演拍摄电影白蛇传
  2. 白蛇传中有主演 小青 法海
  3. 主演的穿着

3. node relation 与java中对象的关系

  • 我想构造 node 节点的 人(导演) ,电影(白蛇传) ; 人和电影的 “关系”

分析如下: 人和电影有关系,人和衣服有关系
由于人中的关系较多,所以这里分散下,我把人和电影的关系,放到电影中
这个图中,只有三个node,即是 人 电影 衣服
有三个关系 关系 关系1 穿着

  • 我现在构造下 人和电影的关系
  1. node 人
@Node("Person")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person extends BaseNode {@Id@GeneratedValueprivate Long id;@Propertyprivate Integer born;@Propertyprivate String name;public Person(Integer born, String name) {this.born = born;this.name = name;}
}
  1. node 电影
@Data
@Node("Movie")
@NoArgsConstructor
@AllArgsConstructor
public class Movie extends BaseNode {@Id@GeneratedValueprivate Long id;@Propertyprivate Integer released;@Propertyprivate String tagline;@Propertyprivate String title;@Relationship(type = "关系", direction = Relationship.Direction.INCOMING)private List<Relation> relations;@Relationship(type = "关系1", direction = Relationship.Direction.OUTGOING)private List<Relation> relationList;
}

将关系放在电影中,电影和人有两种关系, 导演和主演两种关系(这个是relatio 的意义)
“关系” “关系1” 是relation 的type
有两种关系类型,而且每种关系可能有多种,所以这里用集合,如果确认关系为单个,用单个对象也可以

  1. relation 关系/ 关系1
@Data
@RelationshipProperties
public class Relation extends BaseRelation {@Id@GeneratedValueprivate Long id;private List<String> roles;@TargetNodeprivate Person person;
}

这个是关系的定义 relation
由于是任何电影的对应关系,我将关系放到了电影中,所以这里要声明一下目标节点为人 person

  1. 开始定义人和衣服的关系
@Data
@Node("Clothe")
@NoArgsConstructor
@AllArgsConstructor
public class Clothe extends BaseNode {@Id@GeneratedValueprivate Long id;@Propertyprivate String remark;public Clothe(String name) {this.remark = name;}
}

衣服是节点 人是节点 人和衣服是关系

@Node("Person")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person extends BaseNode {@Id@GeneratedValueprivate Long id;@Propertyprivate Integer born;@Propertyprivate String name;public Person(Integer born, String name) {this.born = born;this.name = name;}@Relationship(type = "穿着", direction = Relationship.Direction.OUTGOING)private List<Chuan> chuanList;}

改造之前的人,将关系放到人中 chuanList type 为穿着,这里确定一定是多个,一个人可能穿很多件衣服
接下来是 Chuan 的relation 所以内容中应该有目标节点

  1. 穿的relation
@Data
@RelationshipProperties
public class Chuan extends BaseRelation {@Id@GeneratedValueprivate Long id;@Propertyprivate String brand;@TargetNodeprivate Clothe clothe;public Chuan(String brand, Clothe clothe) {this.brand = brand;this.clothe = clothe;}
}

是的,这里有目标节点 Clothe

  1. 构造完毕
    大家可以仔细体会下,这个图和java对象的对应关系,只要理解了,那么后续的图就可以自己构造了~~

4. 编码测试

  • dao层,给出一个示例,剩下都一样,与spring-data-jpa一样
@Repository
public interface ClotheRepository extends Neo4jRepository<Clothe, Long> {
}
  • 测试用例
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.xuni.neo4j.entity.Clothe;
import com.xuni.neo4j.entity.Movie;
import com.xuni.neo4j.entity.Person;
import com.xuni.neo4j.relation.Chuan;
import com.xuni.neo4j.relation.Relation;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.Arrays;
import java.util.List;/*** @author fulin* @since 2024/8/13 9:32* <p>* 参考文档* <a href="https://docs.spring.io/spring-data/neo4j/docs/6.1.7/reference/html/#sdn-mixins"> Spring Data Neo4j </a>* </p>*/
@SpringBootTest
@Slf4j
class MovieRepositoryTest {@Autowiredprivate MovieRepository movieRepository;@Autowiredprivate PersonRepository personRepository;@Autowiredprivate RelationRepository relationRepository;/*** //     * @see 数据库效果.png* 初始化数据*/@Testvoid initData() {Movie movie = new Movie();movie.setTagline("民间故事");movie.setTitle("白蛇传");movie.setReleased(2024);// 吴家骀>>>导演了>>> 白蛇传Relation relation = new Relation();relation.setRoles(Arrays.asList("导演", "编剧"));movie.setRelations(Arrays.asList(relation));Person person = new Person(34, "吴家骀");relation.setPerson(person);// 白蛇传的主演是法海Person person1 = new Person(35, "法海");Relation relation1 = new Relation();relation1.setRoles(Arrays.asList("主演"));relation1.setPerson(person1);movie.setRelationList(Arrays.asList(relation1));personRepository.save(person1);personRepository.save(person);movieRepository.save(movie);addRelationship();}void addRelationship() {Person person1 = new Person(18, "小青");personRepository.save(person1);Movie movie = movieRepository.findAll().get(0);List<Relation> relationList = movie.getRelationList();Relation relation1 = new Relation();relation1.setRoles(Arrays.asList("主演"));relation1.setPerson(person1);relationList.add(relation1);movieRepository.save(movie);addClothe();}@Autowiredprivate ClotheRepository clotheRepository;void addClothe() {List<Clothe> clotheList = CollUtil.newArrayList();clotheList.add(new Clothe("T恤"));clotheList.add(new Clothe("牛仔"));clotheList.add(new Clothe("衬衫"));clotheList.add(new Clothe("帽子"));clotheRepository.saveAll(clotheList);Person person = personRepository.findAll().get(2);List<Chuan> chuanList = CollUtil.newArrayList();chuanList.add(new Chuan("阿迪", clotheRepository.findAll().get(1)));chuanList.add(new Chuan("安踏", clotheRepository.findAll().get(2)));person.setChuanList(chuanList);personRepository.save(person);}/*** 查询所有数据*/@Testvoid movieQuery() {List<Movie> movieList = movieRepository.findAll();log.info("movieList:{}", JSONUtil.toJsonPrettyStr(movieList));}/*** 删除所有数据*/@Testvoid deleteAll() {movieRepository.deleteAll();personRepository.deleteAll();relationRepository.deleteAll();clotheRepository.deleteAll();}@Testvoid 单步自定义查询() {// MATCH (n:Movie)-[r:`关系`|`关系1`]-(p:Person) return n,p;// MATCH (n:Movie)-[r:`关系`]-(p:Person) return n,p;List<Movie> movieList = movieRepository.queryMovie();log.info("movieList:{}", JSONUtil.toJsonPrettyStr(movieList.get(0)));}@Testvoid 关系自定义查询() {// MATCH ()-->() RETURN count(*);Long count =  movieRepository.queryRelations();log.info("count:{}", count);}
}

总结

spring-boot-starter-data-neo4j 2.7.6 与之前的版本使用还是有很多区别的,在网上找了很多,没有找到合适的,自己摸索了两天,搞了一个出来,希望可以帮助到你

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 如何快速上手一个Github的开源项目
  • 【深度】为GPT-5而生的「草莓」模型!从快思考—慢思考到Self-play RL的强化学习框架
  • vue组件的生命周期
  • 常用集合(Set等)
  • CTF夺旗赛经验总结及落地实践,零基础入门到精通,收藏这一篇就够了
  • 跟着问题学12——GRU详解
  • 应用targetSdkVersion升级指导
  • 探索C语言与Linux编程:获取当前用户ID与进程ID
  • 中秋节特别游戏:给玉兔投喂月饼
  • Mac端口扫描工具
  • C++——打印以下图案:用字符数组方法。
  • golang学习笔记28——golang中实现多态与面向对象
  • 摄影社团管理系统
  • MySQL-DQL(数据查询语言)
  • Avalonia第三方UI库Semi.Avalonia用法详解
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • avalon2.2的VM生成过程
  • ES6 学习笔记(一)let,const和解构赋值
  • gcc介绍及安装
  • Git 使用集
  • iOS小技巧之UIImagePickerController实现头像选择
  • java 多线程基础, 我觉得还是有必要看看的
  • Javascripit类型转换比较那点事儿,双等号(==)
  • js继承的实现方法
  • js正则,这点儿就够用了
  • LeetCode29.两数相除 JavaScript
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 阿里云前端周刊 - 第 26 期
  • - 概述 - 《设计模式(极简c++版)》
  • 普通函数和构造函数的区别
  • 设计模式(12)迭代器模式(讲解+应用)
  • 说说动画卡顿的解决方案
  • 无服务器化是企业 IT 架构的未来吗?
  • 一加3T解锁OEM、刷入TWRP、第三方ROM以及ROOT
  • 怎么将电脑中的声音录制成WAV格式
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 正则表达式-基础知识Review
  • ​iOS实时查看App运行日志
  • ​用户画像从0到100的构建思路
  • #QT(智能家居界面-界面切换)
  • $L^p$ 调和函数恒为零
  • (1)bark-ml
  • (C语言)球球大作战
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (分类)KNN算法- 参数调优
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (三)docker:Dockerfile构建容器运行jar包
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (正则)提取页面里的img标签
  • (转)Google的Objective-C编码规范