springboot + neo4j 图形数据库 整合(本地):
一、Windows环境中安装neo4j
1、安装jdk,配置jdk环境变量(此处不讲解jdk的安装配置教程)
2、下载neo4,neo4j社区下载 [下载地址](https://neo4j.com/download-center/)
3.3.5版本
链接:https://pan.baidu.com/s/1dfDfGYN1WEvj91F2MrYdxQ
提取码:1jqb
解压到指定目录
3、配置neo4j环境变量
主目录设置为变量:
编辑path变量,在path变量值的最后输入:
%NEO4J_HOME%\bin
(Neo4j支持三种网络协议(Protocol),分别是Bolt,HTTP和HTTPS,默认的连接器配置有三种,为了使用这三个端口,需要在
Windows防火墙中创建Inbound Rules,允许通过端口7687,7474和7473访问本机。)
4、启动neo4j
进入neo4j的安装目录,启用DOS命令行窗口,输入neo4j.bat console命令:
访问:http://localhost:7474/
默认的host是bolt://localhost:7687,默认的用户是neo4j,默认的密码是:neo4j,第一次成功connect到Neo4j服务器之后,需要重置密码。
以上本地安装配置结束!
二、创建springboot项目 整合neo4j
使用gradle搭建,build.gradle配置如下:
1、springboot 相关jar包:
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
compileJava.options.encoding = 'UTF-8'
group = 'com.hbkj'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
archivesBaseName = "lobby-match"
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
jcenter()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
testCompile 'org.springframework.boot:spring-boot-starter-test'
compile group: 'io.netty', name: 'netty-all', version: '4.1.15.Final'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
2、neo4j 相关jar包
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-neo4j', version: '2.0.5.RELEASE'
compile group: 'org.neo4j', name: 'neo4j-ogm-embedded-driver', version: '2.0.5'
compile group: 'org.neo4j', name: 'neo4j-ogm-http-driver', version: '2.0.5'
//lombok
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.0'
3、配置application.yml
server:
port: 8888
spring:
data:
neo4j:
uri: bolt://localhost:7687
username: neo4j
password: 123456
4、创建实体类
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.Index;
import org.neo4j.ogm.annotation.NodeEntity;
@Data
@NodeEntity
public class Student {
@Id
@GeneratedValue
private Long id;
@Index
private long userId;
private String name;
private long age;
}
5、创建节点处理类 Cypher(注解方式编写sql)
import com.luzz.domain.bean.Student;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 节点处理类
*/
@Repository
public interface StudentRepository extends Neo4jRepository<Student, Long> {
/**
* 创建节点
* @param userId
* @param name
* @param age
*/
@Query("create (:Student {userId:{userId},name:{name},age:{age}})")
void createNode(@Param("userId") long userId, @Param("name") String name, @Param("age") long age);
/**
* 查询所有节点
* @return
*/
@Query("match (n:Student) return n;")
List<Student> getAllStudentNode();
/**
* 查询所有节点的所有关系
* @return
*/
@Query("match (a:Student)-[*1..]->(b:Student)return a,b;")
List<Student> getAllStudentNodeRelation();
/**
* 创建2节点的关系 上下关系
* @param superUserId
* @param lowerUserId
* @param time
* @return
*/
@Query("match (a:Student),(b:ResellerRelation) where a.userId = {superUserId} and b.userId = {lowerUserId} create (a) - [r:RESELLER_ACTED_IN{since:{time}}] -> (b) return a,b;")
List<Student> addStudentNodeReseller(@Param("superUserId") long superUserId, @Param("lowerUserId") long lowerUserId, @Param("time") long time);
/**
* 根据id查询单个节点
* @param userId
* @return
*/
@Query("match (n:Student) where n.userId ={userId} return n")
Student findByUserId(@Param("userId") long userId);
/**
* 根据userId查询节点直属 2级关系
* @param userId
* @return
*/
@Query("match (a:Student)-[*1..1]->(b:Student) where a.userId = {userId} return a,b")
List<Student> findAllDirectlyByUserId(@Param("userId") long userId);
/**
* 根据userId 查询该节点的所有关系
* @param userId
* @return
*/
@Query("match (a:Student)-[*1..]->(b:Student) where a.userId = {userId} return a,b")
List<Student> findAllStudentByUserId(@Param("userId") long userId);
/**
* 根据userId 修改节点 属性
* @param userId
* @param name
* @return
*/
@Query("match (n:Student) where n.userId = {userId} set n.name ={name} return n")
Student updateStudentNode(@Param("userId") long userId, @Param("name") long name);
/**
* 删除节点关系
* @param superUserId
* @param lowerUserId
*/
@Query("match (n1:Student),(n2:Student) where n1.userId={superUserId} AND n2.userId={lowerUserId} optional match (n1)-[r]-(n2) delete r;")
void deleteStudentNodeRelation(@Param("superUserId") long superUserId, @Param("lowerUserId") long lowerUserId);
}
可继承Neo4jRepository,可不继承,Neo4jRepository方法如下:
可使用save方法来根据对象创建节点
6、创建业务层StudentService StudentServiceImpl
StudentService.java
import com.luzz.domain.bean.Student;
import java.util.List;
public interface StudentService {
void createNode(Student student);
/**
* 查询所有节点
* @return
*/
List<Student> getAllStudentNode();
/**
* 查询所有节点的所有关系
* @return
*/
List<Student> getAllStudentNodeRelation();
/**
* 创建2节点的关系 上下关系
* @param superUserId
* @param lowerUserId
* @param time
* @return
*/
List<Student> addStudentNodeReseller(long superUserId, long lowerUserId, long time);
/**
* 根据id查询单个节点
* @param userId
* @return
*/
Student findByUserId(long userId);
/**
* 根据userId查询节点直属 2级关系
* @param userId
* @return
*/
List<Student> findAllDirectlyByUserId(long userId);
/**
* 根据userId 查询该节点的所有关系
* @param userId
* @return
*/
List<Student> findAllStudentByUserId(long userId);
/**
* 根据userId 修改节点 属性
* @param userId
* @param name
* @return
*/
Student updateStudentNode(long userId, long name);
/**
* 删除节点关系
* @param superUserId
* @param lowerUserId
*/
void deleteStudentNodeRelation(long superUserId, long lowerUserId);
}
StudentServiceImpl.java
import com.luzz.domain.bean.Student;
import com.luzz.domain.service.StudentRepository;
import com.luzz.domain.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
StudentRepository repository;
@Override
public void createNode(Student student) {
repository.save(student);
}
@Override
public List<Student> getAllStudentNode() {
return null;
}
@Override
public List<Student> getAllStudentNodeRelation() {
return null;
}
@Override
public List<Student> addStudentNodeReseller(long superUserId, long lowerUserId, long time) {
return null;
}
@Override
public Student findByUserId(long userId) {
return repository.findByUserId(userId);
}
@Override
public List<Student> findAllDirectlyByUserId(long userId) {
return null;
}
@Override
public List<Student> findAllStudentByUserId(long userId) {
return null;
}
@Override
public Student updateStudentNode(long userId, long name) {
return null;
}
@Override
public void deleteStudentNodeRelation(long superUserId, long lowerUserId) {
}
}
7、测试Controller
import com.luzz.domain.bean.Student;
import com.luzz.domain.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/studentRelation")
public class TestController {
@Autowired
StudentService service;
@GetMapping("/test")
public String test() {
//创建节点
Student student = new Student();
student.setUserId(100005);
student.setName("李七");
student.setAge(23);
service.createNode(student);
Student st = service.findByUserId(100005);
System.out.println("查询结果:" + st);
return st.toString();
}
}
8、执行结果