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、執行結果