当前位置:   article > 正文

Spring-Boot+Neo4j☞创建多点关系网_springboot 集成neo4j 创建关系

springboot 集成neo4j 创建关系

基于前两篇的简单实战后,本篇我们继续讲Neo4j的应用,模拟公司内部员工之间的关系,当然,关系可能是上下级(管理),也可能是同级(同事),甚至也有可能是其他一些特殊的关系,比如说,互相喜欢啊...etc

 

本文参考地址:https://spring.io/guides/gs/accessing-data-neo4j/

 

 

一、Spring-Boot目录结构图

 

 

 

 

 

 

二、先有节点(顶点)才有关系(边)

 

 

(1)创建Employee(员工)节点实体--【包含两个关系】

 

 

  1. package com.appleyk.data.nodeentity;
  2. import java.util.Collections;
  3. import java.util.HashSet;
  4. import java.util.Optional;
  5. import java.util.Set;
  6. import java.util.stream.Collectors;
  7. import org.neo4j.ogm.annotation.GraphId;
  8. import org.neo4j.ogm.annotation.NodeEntity;
  9. import org.neo4j.ogm.annotation.Relationship;
  10. @NodeEntity
  11. public class Employee {
  12. @GraphId private Long id;
  13. private String name;
  14. public Employee(String name) {
  15. this.name = name;
  16. }
  17. /**
  18. * Neo4j 并没有真正的双向关系,我们只有在查询的时候忽略关系的方向
  19. * 可以参考下面这个链接对neo4j的关系作出正确的理解:
  20. * https://dzone.com/articles/modelling-data-neo4j
  21. */
  22. @Relationship(type = "同事", direction = Relationship.UNDIRECTED)
  23. //@Relationship(type = "同事")
  24. public Set<Employee> colleagues;
  25. @Relationship(type = "管理")
  26. public Set<Employee> manages;
  27. /*
  28. * 指定同事关系 --->
  29. */
  30. public void worksWith(Employee person) {
  31. if (colleagues == null) {
  32. colleagues = new HashSet<>();
  33. }
  34. colleagues.add(person);
  35. }
  36. /*
  37. * 指定管理关系 --->
  38. */
  39. public void management(Employee person) {
  40. if (manages == null) {
  41. manages = new HashSet<>();
  42. }
  43. manages.add(person);
  44. }
  45. /**
  46. * 列出该节点(Employee)的关系网
  47. */
  48. public String toString() {
  49. /* java8新特新
  50. * Optional.ofNullable(arg) 参数可以是null
  51. * 如果值不为null,orElse方法返回Optional实例(关系)的值
  52. * Collections.emptySet():防止空指针出现
  53. * |
  54. * |
  55. * V
  56. * 当代码需要一个集合而这个集合可能不存在,此时尽量使用空集合而不是null
  57. */
  58. return this.name + " 同事 => "
  59. + Optional.ofNullable(this.colleagues).orElse(
  60. Collections.emptySet()).stream().map(
  61. person -> person.getName()).collect(Collectors.toList())
  62. + " 管理 => "
  63. + Optional.ofNullable(this.manages).orElse(
  64. Collections.emptySet()).stream().map(
  65. person -> person.getName()).collect(Collectors.toList());
  66. }
  67. public String getName() {
  68. return name;
  69. }
  70. public void setName(String name) {
  71. this.name = name;
  72. }
  73. }

 

 

 

 

 

(2)创建Employee增删改查接口

 

 

  1. package com.appleyk.data.Repository;
  2. import org.springframework.data.neo4j.repository.GraphRepository;
  3. import org.springframework.stereotype.Repository;
  4. import com.appleyk.data.nodeentity.Employee;
  5. @Repository
  6. public interface EmployeeRepository extends GraphRepository<Employee>{
  7. Employee findByName(String name);
  8. }

 

 

 

 

 

 

 

 

三、Spring-Boot启动时创建节点及节点之间的关系

 

 

(1)

 

 

 

 

 

  1. package com.appleyk;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import org.springframework.boot.CommandLineRunner;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.boot.builder.SpringApplicationBuilder;
  8. import org.springframework.boot.web.support.SpringBootServletInitializer;
  9. import org.springframework.context.annotation.Bean;
  10. import com.appleyk.data.Repository.EmployeeRepository;
  11. import com.appleyk.data.nodeentity.Employee;
  12. /**
  13. *
  14. * 下面是一个典型的结构:
  15. *
  16. * com +- example +- myproject +- Application.java --
  17. * 注意这个位置,习惯性的放在项目的一开始,也就是根包的第一层 | + - domain | +- Customer.java | +-
  18. * CustomerRepository.java | + - service | +- CustomerService.java | + - web +-
  19. * CustomerController.java
  20. *
  21. *
  22. * 文件将声明 main 方法, 还有基本的 @Configuration
  23. *
  24. * @author yukun24@126.com
  25. * @date 2017年12月1日08:46:41
  26. */
  27. @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
  28. public class Application extends SpringBootServletInitializer{
  29. /**
  30. * SpringApplication类提供了一种从main()方法启动Spring应用的便捷方式。 在很多情况下, 你只需委托给
  31. * SpringApplication.run这个静态方法:
  32. *
  33. * @param args
  34. */
  35. public static void main(String[] args) {
  36. SpringApplication.run(Application.class, args);
  37. }
  38. @Override
  39. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  40. return application.sources(Application.class);
  41. }
  42. /**
  43. * Spring-Boot启动的时候,加载、创建、初始化数据
  44. * @param personRepository
  45. * @return
  46. */
  47. @Bean
  48. CommandLineRunner demo(EmployeeRepository employeeRepository) {
  49. return args -> {
  50. //先删除Employees节点,再创建
  51. employeeRepository.deleteAll();
  52. Employee boss = new Employee("总裁");
  53. Employee manager= new Employee("项目经理");
  54. Employee p1 = new Employee("员工1");
  55. Employee p2 = new Employee("员工2");
  56. List<Employee> team = Arrays.asList(boss,manager,p1,p2);
  57. System.err.println("连接neo4j图库没创建关系节点之前...");
  58. team.stream().forEach(person -> System.err.println("\t" + person.toString()));
  59. employeeRepository.save(boss);
  60. employeeRepository.save(manager);
  61. employeeRepository.save(p1);
  62. employeeRepository.save(p2);
  63. boss = employeeRepository.findByName(boss.getName());
  64. /*
  65. * Boss管理经理(一级直属关系)
  66. */
  67. boss.management(manager);
  68. /*
  69. * 同时 Boss和其他人是同事关系,反之亦成立
  70. */
  71. boss.worksWith(manager);
  72. boss.worksWith(p1);
  73. boss.worksWith(p2);
  74. //建立关系
  75. employeeRepository.save(boss);
  76. /*
  77. * 经理管理员工1和2(直接关系)
  78. * 先查节点,再添加关系(边)
  79. */
  80. manager = employeeRepository.findByName(manager.getName());
  81. manager.management(p1);
  82. manager.management(p2);
  83. /*
  84. * 由于上面我们已经知道了Boss和经理是同事,反之亦然,下面只添加经理和其他人的同事关系
  85. */
  86. manager.worksWith(p1);
  87. manager.worksWith(p2);
  88. employeeRepository.save(manager);
  89. /*
  90. * 员工1和员工2是同事关系,此层关系不考虑方向,因此创建一个就Ok
  91. */
  92. p1 = employeeRepository.findByName(p1.getName());
  93. p1.worksWith(p2);
  94. employeeRepository.save(p1);
  95. System.out.println("创建关系节点之后,通过人名查找节点查询节点信息...");
  96. team.stream().forEach(person -> System.out.println(
  97. "\t" + employeeRepository.findByName(person.getName()).toString()));
  98. };
  99. }
  100. }

 

 

 

 

 

 

 

(2)启动Spring-Boot

 

 

 

 

 

 

 

 

 

 

 

(3)Neo4j图库-- Employee员工关系图谱查询(ALL)

 

 

 

 

 

 

 

四、查询关系(findByName)

 

 

(1)

 

  1. package com.appleyk.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.appleyk.data.Repository.EmployeeRepository;
  7. import com.appleyk.data.nodeentity.Employee;
  8. import com.appleyk.result.ResponseMessage;
  9. import com.appleyk.result.ResponseResult;
  10. @RestController
  11. @RequestMapping("/rest/v1.0.1/database/employee")
  12. public class EmployeeController {
  13. @Autowired
  14. EmployeeRepository employeeRepository;
  15. @RequestMapping("/get")
  16. public ResponseResult GetEmployees(@RequestParam(value = "name") String name) {
  17. Employee employee = employeeRepository.findByName(name);
  18. if (employee != null) {
  19. /*
  20. * 打印下name的关系,以下关系!=null,就是一个set集合,集合未做处理!
  21. */
  22. System.out.println(employee.getManages());
  23. System.out.println("========上管理【" + employee.getName() + "】下同事============");
  24. System.out.println(employee.getColleagues());
  25. return new ResponseResult(ResponseMessage.OK);
  26. }
  27. return new ResponseResult(ResponseMessage.INTERNAL_SERVER_ERROR);
  28. }
  29. }

 

 

 

 

 

 

(2)

 

 

 

 

(3)

 

 

 

 

 

 

五、项目GitHub地址

 

 

SpringBoot集成neo4j实现关系网创建和查询

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/727785
推荐阅读
相关标签
  

闽ICP备14008679号