首先我们创建一个springboot项目, 创建过程中, 可以选择spring data neo4j的引用
application.properties配置如下
spring.neo4j.uri=bolt://localhost:7687 # 注意协议为bolt, 不是http如果不是本地启动的, 需要改为对应的ip
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=123456 #此处为自己设置的密码
然后我们创建一个实体类:
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
/**
* @author quyj
* @date 2023-03-04 21:14
*/
@Node("student") // 关联的节点
public class Student {
@Id
@GeneratedValue
private Long id;
@Property("name")
private String name;
@Property("age")
private Integer age;
public Long getId() {
return id;
}
public Student setId(Long id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public Student setName(String name) {
this.name = name;
return this;
}
public Integer getAge() {
return age;
}
public Student setAge(Integer age) {
this.age = age;
return this;
}
}
我们再创建一个持久层的Repository:
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
/**
* @author quyj
* @date 2023-03-04 21:16
*/
@Repository
public interface StudentRepository extends Neo4jRepository<Student, Long> {
}
下面我们可以进行测试, 在test包下创建一个测试用例
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Neo4jdemoApplicationTests {
@Autowired
private StudentRepository studentRepository;
@Test
void contextLoads() {
Student student = new Student();
student.setName("小王");
student.setAge(22);
studentRepository.save(student);
}
}
执行后我们可以到页面看一下, 小王节点已经生成了
到这里, neo4j腿碰腿教程已经完结了, 这系列只是一个简单的入门教程, 没有任何复杂的操作, 后续如果有时间可能会随缘更新一些具体业务场景下的使用, 当然只是随缘。