参考文章:
Spring Boot:Boot2.0版本整合Neo4j
Neo4j系列-整合SpringBoot(五)
Spring Data Neo4j - Reference Documentation
springboot整合图像数据库Neo4j
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
org.fiend
neo4j
0.0.1-SNAPSHOT
neo4j
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-neo4j
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
server:
port: 18016
max-http-header-size: 8192
spring:
application:
name: spring-boot-neo4j
neo4j: # neo4j配置
uri: bolt://101.101.15.110:7687
username: neo4j
password: 123abc
@NodeEntity:声明该类为Neo4j的节点类
@Id:Neo4j的主键id,必须为长整型
@Property:Neo4j的节点属性值,支持8种基本类型外加String
package org.fiend.entity;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
/**
* 创建一个Person实体类
* @author langpf 2019/3/7
*/
@NodeEntity(label = "Person")
public class Person {
@Id
@GeneratedValue
private Long nodeId;
@Property(name = "name")
private String name;
@Property(name = "born")
private int born;
public Long getNodeId() {
return nodeId;
}
public void setNodeId(Long nodeId) {
this.nodeId = nodeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBorn() {
return born;
}
public void setBorn(int born) {
this.born = born;
}
}
package org.fiend.repository;
import org.fiend.entity.Person;
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;
/**
* @author langpf 2019/4/19
*/
@Repository
public interface PersonRepository extends Neo4jRepository {
@Query("MATCH (n:Person) RETURN n ")
List getPersonNodeList();
@Query("create (n:Person{name:{name},born:{born}}) RETURN n ")
List addPersonNodeList(@Param("name") String name, @Param("born")int born);
}
package org.fiend.entity;
import org.neo4j.ogm.annotation.*;
/**
* 关系节点类型
* @author langpf 2019/3/7
*/
@RelationshipEntity(type = "PersonRelation") // type为空时默认为class名称
public class PersonRelation {
@Id
@GeneratedValue
private Long id;
//开始节点
@StartNode
private Person startNode;
//结束节点
@EndNode
private EndNode endNode;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Person getStartNode() {
return startNode;
}
public void setStartNode(Person startNode) {
this.startNode = startNode;
}
public EndNode getEndNode() {
return endNode;
}
public void setEndNode(EndNode endNode) {
this.endNode = endNode;
}
}
package org.fiend.repository;
import org.fiend.entity.PersonRelation;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import java.util.List;
/**
* @author langpf 2019/4/24
*/
public interface PersonRelationRepository extends Neo4jRepository {
@Query("match p=(n:Person)<-[r:PersonRelation]->(n1:Person) where p.name={firstName} and n1.name={secondName} return p")
List findPersonRelationByName(@Param("firstName")String firstName, @Param("secondName")String secondName);
@Query ("match (fu:Person),(su:Person) where fu.name={firstName} and su.name={secondName}, create p=(fu)-[r:PersonRelation]->(su) return p")
List addPersonRelation(@Param("firstName")String firstName, @Param("secondName") String secondName);
}
package io.fredia.femicro.graph.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.fredia.femicro.graph.entity.UserNode;
import io.fredia.femicro.graph.repository.UserRelationRepository;
import io.fredia.femicro.graph.repository.UserRepository;
@Service
public class Neo4jService {
@Autowired
private UserRepository userRepository;
@Autowired
private UserRelationRepository userRelationRepository;
public int addUser(UserNode userNode) {
userRepository.addUserNodeList(userNode.getName(), userNode.getAge());
return 1;
}
public List getUserNodeList() {
return userRepository.getUserNodeList();
}
}
package org.fiend.controller;
import org.fiend.entity.Person;
import org.fiend.service.PersonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author Administrator
*/
@Controller
public class PersonController {
Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
PersonService personService;
@RequestMapping(value = "hi")
@ResponseBody
public String hi() {
return "hi";
}
@GetMapping("/addPerson")
@ResponseBody
public Object addPerson(Person person) {
return personService.addPerson(person);
}
}
启动项目后,本地请求
http://localhost:12012/addUserNode
若想使用嵌入式的Neo4j驱动,可以引入如下jar包:
org.neo4j
neo4j-ogm-embedded-driver
${neo4j-ogm.version}
也可以按照官方的方式新建工程:
1.
2.
3.