网上查找好多资料和博客 整理一套可运行的demo 自己记录方便自己以后查看 如有错误内容请指正
工具类参考https://www.cnblogs.com/shiyixin0125/p/11057342.html
第一步 pom文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.0.RELEASE
spring.boot
neo4japi
0.0.1-SNAPSHOT
war
neo4japi
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-data-neo4j
com.google.guava
guava
28.0-jre
org.springframework.boot
spring-boot-maven-plugin
第二步 application.properties
#neo4j 配置
spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456
第三步 Neo4jConfig 配置类
package spring.boot.neo4japi;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
/**
* @ClassName Neo4jConfig
* @Description 当操作neo4j图数据库数据需映射到实体类时 该配置类内容可注释
* 该配置类主要获取 Session
* @Author
* @Date 2019/10/31 9:48
* @Version 1.0
**/
@Configuration
@EnableNeo4jRepositories(basePackages = "spring.boot.neo4japi.Repository")
public class Neo4jConfig {
@Value("${spring.data.neo4j.uri}")
private String url;
@Value("${spring.data.neo4j.username}")
private String username;
@Value("${spring.data.neo4j.password}")
private String password;
@Bean(name = "session")
public Session neo4jSession() {
Driver driver = GraphDatabase.driver(url, AuthTokens.basic(username, password));
return driver.session();
}
}
第四步 util工具类
package spring.boot.neo4japi.util;
import com.google.common.collect.Lists;
import org.neo4j.driver.Record;
import org.neo4j.driver.internal.value.PathValue;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Path;
import org.neo4j.driver.types.Relationship;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @ClassName NodeUtil
* @Description 该工具类是当操作neo4j无法映射到pojo时 解析数据
* @Author
* @Date 2019/10/31 11:10
* @Version 1.0
**/
public class NodeUtil {
/**
* records 转 map
*
* @param records
* @return
*/
public static Map>> getGrap(List records) {
Map>> map = new HashMap<>(2);
List nodeList = new ArrayList<>();
List edgeList = new ArrayList<>();
records.forEach(e->{
// data 为cql cypher查询语句命名的别名
PathValue pathValue =(PathValue)e.get("data");
Path path = pathValue.asPath();
Iterable nodes = path.nodes();
Iterable relationships = path.relationships();
nodeList.addAll(Lists.newArrayList(nodes));
edgeList.addAll(Lists.newArrayList(relationships));
});
map.put("nodes",node2List((nodeList.stream().distinct().collect(Collectors.toList()))));
map.put("edges",edge2List((edgeList.stream().distinct().collect(Collectors.toList()))));
return map;
}
/**
* nodes 转list
*
* @param nodes
* @return
*/
public static List
第五步 pojo层
package spring.boot.neo4japi.pojo;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
/**
* @ClassName Person
* @Description TODO
* @Autho
* @Date 2019/10/31 9:11
* @Version 1.0
**/
@NodeEntity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
private String born;
public Person() {
}
public Person(Long id, String name, String born) {
this.id = id;
this.name = name;
this.born = born;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBorn() {
return born;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", born='" + born + '\'' +
'}';
}
public void setBorn(String born) {
this.born = born;
}
}
package spring.boot.neo4japi.pojo;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
/**
* @ClassName Movie
* @Description TODO
* @Author
* @Date 2019/10/31 9:13
* @Version 1.0
**/
@NodeEntity
public class Movie {
@Id
@GeneratedValue
private Long id;
private String title;
private String released;
public Movie() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getReleased() {
return released;
}
public void setReleased(String released) {
this.released = released;
}
public Movie(Long id, String title, String released) {
this.id = id;
this.title = title;
this.released = released;
}
}
第六步 dao层
package spring.boot.neo4japi.Repository;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import spring.boot.neo4japi.pojo.Person;
import java.util.List;
/**
* @ClassName PersonRepository
* @Description TODO
* @Author
* @Date 2019/10/31 9:22
* @Version 1.0
**/
@Repository
public interface PersonRepository extends CrudRepository {
@Query("MATCH (p:Person) return p")
List findPersonList();
@Query("CREATE (p:Person{name:{name},born:{born}}) return p")
List inserPerson(@Param("name") String name, @Param(("born")) String born);
@Query("MATCH (p:Person{name:{name},born:{born}}) DELETE p")
Person deletePersonByNameAndBorn(@Param("name") String name, @Param("born") String born);
@Query("MATCH (p:Person{name:{name},born:{born}}) set p.name={name2} return p ")
Person updatePersonByNameAndBorn(@Param("name") String name,@Param("born") String born,@Param("name2")String name2);
}
package spring.boot.neo4japi.Repository;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import spring.boot.neo4japi.pojo.Movie;
import java.util.List;
/**
* @ClassName MovieRepsitory
* @Description TODO
* @Author
* @Date 2019/10/31 10:29
* @Version 1.0
**/
@Repository
public interface MovieRepsitory extends CrudRepository {
@Query("CREATE (m:Movie{title:{title},released:{released}}) return m")
Movie addMovie(@Param("title") String title, @Param("released") String released);
@Query("MATCH (m:Movie) return m")
List findMovie();
}
第七步 service层
package spring.boot.neo4japi.service;
import spring.boot.neo4japi.pojo.Movie;
import spring.boot.neo4japi.pojo.Person;
import java.util.List;
/**
* @ClassName Neo4jService
* @Description TODO
* @Author
* @Date 2019/10/31 9:42
* @Version 1.0
**/
public interface Neo4jService {
List inserPerson(String name, String born);
Person updatePerson(String name, String born, String 王六);
Person deletePerson(String name, String born);
List findPerson();
Movie inserMovie(String title, String released);
List findMovie();
}
第八步 serviceimpl
package spring.boot.neo4japi.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.boot.neo4japi.Repository.MovieRepsitory;
import spring.boot.neo4japi.Repository.PersonRepository;
import spring.boot.neo4japi.pojo.Movie;
import spring.boot.neo4japi.pojo.Person;
import spring.boot.neo4japi.service.Neo4jService;
import java.util.List;
/**
* @ClassName Neo4jServiceimpl
* @Description TODO
* @Author
* @Date 2019/10/31 9:42
* @Version 1.0
**/
@Service
public class Neo4jServiceimpl implements Neo4jService {
@Autowired
private PersonRepository personrepository;
@Autowired
private MovieRepsitory movieRepsitory;
@Override
public List inserPerson(String name, String born) {
List people = personrepository.inserPerson(name, born);
return people;
}
@Override
public Person updatePerson(String name, String born, String name2) {
return personrepository.updatePersonByNameAndBorn(name, born, name2);
}
@Override
public Person deletePerson(String name, String born) {
return personrepository.deletePersonByNameAndBorn(name, born);
}
@Override
public List findPerson() {
return personrepository.findPersonList();
}
@Override
public Movie inserMovie(String title, String released) {
return movieRepsitory.addMovie(title, released);
}
@Override
public List findMovie() {
return movieRepsitory.findMovie();
}
}
第九步 controller
package spring.boot.neo4japi.controller;
import org.neo4j.driver.Record;
import org.neo4j.driver.Session;
import org.neo4j.driver.StatementResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import spring.boot.neo4japi.pojo.Movie;
import spring.boot.neo4japi.pojo.Person;
import spring.boot.neo4japi.service.Neo4jService;
import spring.boot.neo4japi.util.NodeUtil;
import java.util.List;
import java.util.Map;
/**
* @ClassName Neo4jController
* @Description TODO
* @Author
* @Date 2019/10/31 9:41
* @Version 1.0
**/
@Controller
public class Neo4jController {
@Autowired
private Neo4jService neo4jService;
@Autowired
private Session session;
/**
* @Description:添加节点
* @Param: []
* @return: java.util.List
* @Author: genglinbo
* @Date: 2019/10/31
*/
@RequestMapping(value = "add")
@ResponseBody
public List addPerson() {
String name = "王七";
String born = "1988";
List list = neo4jService.inserPerson(name, born);
String title = "生死时速";
String released = "是";
Movie movie = neo4jService.inserMovie(title, released);
System.out.println(movie);
return list;
}
/**
* @Description:修改节点
* @Param: []
* @return: spring.boot.neo4japi.pojo.Person
* @Author: genglinbo
* @Date: 2019/10/31
*/
@RequestMapping(value = "update")
@ResponseBody
public Person updatePerson() {
String name = "王五";
String born = "1988";
Person person = neo4jService.updatePerson(name, born, "王六");
return person;
}
/**
* @Description:删除节点
* @Param: []
* @return: spring.boot.neo4japi.pojo.Person
* @Author: genglinbo
* @Date: 2019/10/31
*/
@RequestMapping(value = "delete")
@ResponseBody
public Person deletePerson() {
String name = "王六";
String born = "1988";
Person person = neo4jService.deletePerson(name, born);
return person;
}
/**
* @Description:查询所有的Person节点
* @Param: []
* @return: java.util.List
* @Author: genglinbo
* @Date: 2019/10/31
*/
@RequestMapping(value = "find")
@ResponseBody
public List findPerson() {
List list = neo4jService.findPerson();
List list2 = neo4jService.findMovie();
System.out.println(list2);
return list;
}
/**
* @Description:拼接cql查询
* @Param: []
* @return: java.util.Map>>
* @Author: genglinbo
* @Date: 2019/10/31
*/
@RequestMapping(value = "find2")
@ResponseBody
public Map>> getPerson() {
// Person person = new Person();
//person.setName("王五");
String name = "王";//模糊查询
String cql = "match data=(n:Person) where n.name=~" + "'.*" + name + ".*' " + " return data";
//StatementResult statementResult=session.run("match data=(n:Person) where return data");
StatementResult statementResult = session.run(cql);
if (statementResult != null) {
List list = statementResult.list();
Map>> map = NodeUtil.getGrap(list);
return map;
}
return null;
}
/**
* @Description:创建关系
* @Param: []
* @return: void
* @Author: genglinbo
* @Date: 2019/10/31
*/
@RequestMapping(value = "insert2")
@ResponseBody
public Map>> createRelationShip() {
String name = "王五";
String title = "生死时速";
String cql = "match (n:Person),(m:Movie) where n.name='"+name+"' and m.title='"+title+"' create data= (n)-[:出演]->(m) return data ";
StatementResult result = session.run(cql);
if (result != null) {
List list = result.list();
Map>> grap = NodeUtil.getGrap(list);
return grap;
}
return null;
}
}
目录结构