【java-Neo4j 5开发入门篇】-最新Java开发Neo4j
系列文章目录
前言
上一篇文章讲解了Neo4j的基本使用,本篇文章对Java操作Neo4j进行入门级别的阐述,方便读者快速上手对Neo4j的开发。
一、开发环境与代码
1.docker 部署Neo4j
#这里使用docker部署Neo4j,需要镜像加速的需要自行配置
docker run --name neo4j -itd -p 7474:7474 -p 7687:7687 -v ./data:/data neo4j:5.25.1-community
二、开发代码
1.原生Java开发
- 官方给出了Java开发Neo4j的官方教程地址:Neo4j-java官方文档
- Github的样例代码库地址:Github样例库地址
上述文档教程和代码样例可以快速帮你了解如何使用java对Neo4j进行操作。下面我们聊一下使用Springboot3框架对Neo4j的开发。
2.SpringBoot3开发
这里以我写的一个demo案例进行阐述,该demo主要帮大家入门,自定义方法需要自己研究或等我后面更新。。
- 项目目录结构
- pom依赖
jdk21,springboot3.3.5
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.develop</groupId>
<artifactId>neo4j-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>neo4j-java</name>
<description>neo4j-java</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--连接 neo4j,版本要对应数据库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- yml配置
spring:
application:
name: neo4j-java
neo4j:
uri: bolt://localhost:7687
authentication:
username: neo4j
password: 123456
data:
neo4j:
database: neo4j
logging:
level:
org.neo4j.ogm.drivers.bolt.request.BoltRequest: DEBUG
org.neo4j.driver: INFO
- 实体(节点)类
package com.develop.neo4jjava.domain;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
/**
* @author wanChen
* @ClassName Person
* @Description:
* @date 2024/11/18 17:23
* @Version 1.0
*/
@Node
@Data
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
private int born;
}
- 继承工厂接口
package com.develop.neo4jjava.domain.repository;
import com.develop.neo4jjava.domain.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author wanChen
* @ClassName PersonRepository
* @Description:
* @date 2024/11/18 17:24
* @Version 1.0
*/
@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long>{
//自定义根据name查询
List<Person> findByName(String name);
//自定义修改方法,实现类需要先根据name查出数据(id)再进行修改
Person updateByKey(String name);
}
- 创建工具类
package com.develop.neo4jjava.utils;
import com.develop.neo4jjava.domain.Person;
import com.develop.neo4jjava.domain.repository.PersonRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
/**
* @author wanChen
* @ClassName Neo4jUtil
* @Description:
* @date 2024/11/18 17:26
* @Version 1.0
*/
@Component
public class Neo4jUtil {
@Resource
private PersonRepository personRepository;
public Person createPerson(Person person) {
personRepository.updateByKey(person.getName());
return personRepository.save(person);
}
public Iterable<Person> getAllPersons() {
return personRepository.findAll();
}
public Person getPersonById(Long id) {
return personRepository.findById(id).orElse(null);
}
public void deletePerson(Long id) {
personRepository.deleteById(id);
}
}
- 代码测试
package com.develop.neo4jjava;
import com.develop.neo4jjava.domain.Person;
import com.develop.neo4jjava.utils.Neo4jUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Neo4jJavaApplicationTests {
@Autowired
private Neo4jUtil neo4jUtil;
@Test
void addNode() {
Person person = new Person();
person.setBorn(2001);
person.setName("张三");
System.out.println(neo4jUtil.createPerson(person));
}
@Test
void getAllNode(){
System.out.println(neo4jUtil.getAllPersons());
}
@Test
void deleteNode(){
neo4jUtil.deletePerson(71L);
}
@Test
void getNodeById(){
System.out.println(neo4jUtil.getPersonById(1L));
}
}
三、可视化
引用其他博主对可视化的收集:Neo4j可视化工具
四、总结
本文主要以代码demo的方式进行阐述,后续的工作主要是涉及基于Neo4j做一些知识图谱,并且结合ES做全文检索的应用场景。等设计方案过一段落后,会再和大家继续探究Neo4j的使用。
原文地址:https://blog.csdn.net/qq_40570699/article/details/143905723
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!