接下来记录一下springboot集成Neo4j的demo,以及一些踩坑
首先准备好开发环境,neo4j5.0+版本需要jdk17,jdk17需要springboot3.0+
JDK | 17 |
springboot | 3.0.13 |
neo4j | 5.15.0 |
接下来会演示在java程序中进行增删改查
创建一个节点实体类,用来传参和接收参数,启动时要求属性必须添加@Property注解
package org.example.entity;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.*;
import java.util.ArrayList;
import java.util.List;
@Node
@Data
public class Person {
@Id
@GeneratedValue
private Long id;
@Property
private String name;
}
创建一个接口去集成Neo4jRepository的调用增删改查方法,需要指定id类型和节点类型
package org.example.repository;
import org.example.entity.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {
}
测试类--新增节点
启动程序,然后去neo4j-desktop查看,如图:
import org.example.Neo4jApplication;
import org.example.entity.Person;
import org.example.repository.PersonRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Neo4jApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class test {
@Autowired
private PersonRepository personRepository;
@Test
public void test01(){
Person person = new Person();
person.setName("zhangsan");
personRepository.save(person);
}
}
测试类--新增关系
建一个关系实体类,关系也必须添加@ID和@GeneratedValue,注意导包路径;下面是我在网络上搜索的一些教程,但是我实测不能生效,生成的关系是一个节点,并非一个关系我会在后面贴出新的解决方案,不知道是bug还是什么。。。
package org.example.entity;
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.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;
@RelationshipEntity("FRIEND")
@Data
public class FriendOf {
@Id
@GeneratedValue
private Long id;
private String type; // 关系类型
private String since; // 友谊建立时间
@TargetNode
private Person person1;
@TargetNode
private Person person2;
public FriendOf(String type, String since, Person person1, Person person2) {
this.type = type;
this.since = since;
this.person1 = person1;
this.person2 = person2;
}
}
创建一个接口去继承Neo4jRepository的调用增删改查方法,需要指定id类型和节点类型
package org.example.repository;
import org.example.entity.FriendOf;
import org.springframework.data.neo4j.repository.Neo4jRepository;
public interface PersonRelationRepository extends Neo4jRepository<FriendOf, Long> {
}
调用测试类,需要自定义一个构造方法
@Test
public void test02(){
Person person1 = new Person("张三");
Person person2 = new Person("李四");
Person person3 = new Person("王五");
Person person4 = new Person("赵六");
FriendOf friend1 = new FriendOf("FRIEND","2023-05-14",person1,person2);
FriendOf friend2 = new FriendOf("FRIEND","2023-05-14",person1,person3);
FriendOf friend3 = new FriendOf("FRIEND","2023-05-14",person1,person4);
FriendOf friend4 = new FriendOf("FRIEND","2023-05-14",person2,person4);
personRelationRepository.save(friend1);
personRelationRepository.save(friend2);
personRelationRepository.save(friend3);
personRelationRepository.save(friend4);
}
我尝试修改后的解决方案,上面的bug是会将关系当作一个节点插入数据库,相当于api把关系变成了节点,把节点名当作了关系的奇怪bug,也就是将@startNode下的属性名当作关系名来链接(节点)-[关系名/这里的关系是@startNode注释的字段名/]-(关系/被变成了节点形式/)-[@endNode注释的字段名]-(节点),为了避免产生以上格式的错误,所以我用了以下的解决方法来成功添加关系,我在节点实体类中直接定义了关系节点,利用bug将字段名变成了关系名,如下:
package org.example.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.neo4j.ogm.annotation.StartNode;
import org.springframework.data.neo4j.core.schema.*;
@Node
@Data
@NoArgsConstructor
public class Person {
@Id
@GeneratedValue
private Long id;
@Property
private String name;
@StartNode
private Person friend;
public Person(String name) {
this.name = name;
}
public Person(String name,Person friend) {
this.name = name;
this.friend = friend;
}
}
@Test
public void test01() {
Person person1 = new Person("张三");
Person person2 = new Person("李四",person1);
Person person3 = new Person("王五",person2);
Person person4 = new Person("赵六",person3);
personRepository.save(person1);
personRepository.save(person2);
personRepository.save(person3);
personRepository.save(person4);
}
后续研究一下是什么问题造成的,这样的解决方案不能给关系加属性,恼💢