当前位置: 首页 > 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 与之前的版本使用还是有很多区别的,在网上找了很多,没有找到合适的,自己摸索了两天,搞了一个出来,希望可以帮助到你


http://www.mrgr.cn/news/28760.html

相关文章:

  • Linux:RPM软件包管理以及yum软件包仓库
  • 用 Python 实现将长 Markdown 文档从二级标题开始拆分
  • 【车载以太网】【SOME/IP】vsomeip代码解析--routing_manager
  • 【课程学习】信号检测与估计II
  • NC 排序
  • 硬件工程师笔试面试——显示器件
  • fiddler抓包03_汉化
  • 基于代理的分布式身份管理方案
  • Linux:软件包管理器 yum和编辑器-vim使用
  • 『功能项目』回调函数处理死亡【54】
  • leetcode-枚举算法
  • Redhat 8/9 缺少 compat-db47
  • SpringBoot开发——统一返回和统一异常处理
  • idea一个窗口打开多个仓库的代码
  • 硬件工程师笔试面试——无线通讯模块
  • string的模拟实现and友元
  • VS Code + WSL 用法摘记
  • 4.提升客户服务体验:ChatGPT在客服中的应用(4/10)
  • canal消费binlog异常排查
  • 数据结构之栈(python)