1.下载(主要分为社区版和企业版)
https://neo4j.com/deployment-center/

下载完对应的解压即可,也可通过docker进行部署(需要映射7474和7687两个端口)
这里使用windos社区版本进行操作
解压文件后进入bin目录下,在搜索框输入cmd进入命令行窗口
neo4j console
通过查看日志输出找到neo4j访问地址在浏览器输入
默认的用户名和密码为neo4j,首次登陆后需要修改密码
2。创建springboot项目
建议springboot版本为3.2.4,或者通过统一管理设置neo4j版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
设置配置文件格式为yml
spring:
neo4j:
uri: bolt://localhost:7687 #如果是dokcer部署将localhost换成自己的ip地址端口换成自己映射的端口
authentication:
username: neo4j
password: 123456789
在dao包下创建一个PersonRepository
Person对应数据库的实体,Long对应的是主键类型
Match相当于Mysql中的select,()为节点,s相当于别名,:student为标签,由于id为内部自动生成所以使用id(s)进行查找,如果有name的话可以使用s.name进行查找,$为占位符,查询语句必须要有return进行数据返回
package com.exmaple.neotest.dao;
import com.exmaple.neotest.entity.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {
@Query("MATCH (s:student) WHERE id(s) = $i RETURN s")
List<Person> findAllById(long i);
}
创建一个名为Person的实体类
@Node("student")设置查找的标签名
@Property 映射属性
@Id 设置这个字段为主键
@GeneratedValue 这个字段为自动生成
package com.exmaple.neotest.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.Node;
import org.springframework.data.neo4j.core.schema.Property;
import java.io.Serializable;
@Data
@Node("student")
public class Person implements Serializable {
@Id
@GeneratedValue
private long id;
@Property
private String name;
}
创建测试类
package com.exmaple.neotest;
import com.exmaple.neotest.dao.PersonRepository;
import com.exmaple.neotest.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class NeoTestApplicationTests {
@Autowired
private PersonRepository personRepository;
@Test
public void contextLoads() {
List<Person> byName = personRepository.findAllById(0);
System.out.println(byName);
}
}
如有错误欢迎指正,感谢阅读。
