• spring boot 2.0 neo4j 使用


    参考文档

      官方文档

    • http://spring.io/projects/spring-data-neo4j#learn
    • https://docs.spring.io/spring-data/neo4j/docs/5.1.2.RELEASE/reference/html/
    • https://neo4j.com/docs/
    • https://neo4j.com/docs/developer-manual/current/

      第三方使用文档

    • https://blog.csdn.net/appleyk/article/category/7408344  系列文档
    • https://blog.csdn.net/u013946356/article/details/81739079

      中文手册(比较滞后)

    • https://www.w3cschool.cn/neo4j/

    安装 maven 包

    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-http-driver</artifactId>
      <version>3.1.4</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-neo4j</artifactId>
      <version>2.1.0.RELEASE</version>
    </dependency>

    添加 neo4j 配置

    application.yml

    spring:
      data:
        neo4j:
          username: neo4j
          password: 1234
          uri: http://172.16.235.175:7474

    添加配置类

    @Configuration
    @EnableNeo4jRepositories(basePackages = "com.example.demo.repository")
    @EnableTransactionManagement
    public class Neo4jConfig {
    
        @Value("${spring.data.neo4j.uri}")
        private String databaseUrl;
    
        @Value("${spring.data.neo4j.username}")
        private String userName;
    
        @Value("${spring.data.neo4j.password}")
        private String password;
    
        @Bean
        public SessionFactory sessionFactory() {
            org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder()
                    .uri(databaseUrl)
                    .credentials(userName, password)
                    .build();
            return new SessionFactory(configuration, "com.example.demo.entity");
        }
    
        @Bean
        public Neo4jTransactionManager transactionManager() {
            return new Neo4jTransactionManager(sessionFactory());
        }
    }

    添加 Neo4j 节点类

    @NodeEntity
    public class SGNode {
        private Long count;
        private Long error;
        private Double max;
        private Double min;
    
        /**
         * Neo4j会分配的ID(节点唯一标识 当前类中有效)
         */
        @Id
        @GeneratedValue
        private Long id;
    
        private String name;
    
        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;
        }
    
        @Relationship(type = "call")
        private List<CallRelation> calls;
    
        public SGNode() {
            count = 0L;
            error = 0L;
            max = 0.0;
            min = 0.0;
            calls = new ArrayList<>();
        }
    
        public SGNode(String name) {
            this();
            this.name = name;
        }
    
        public Long getCount() {
            return count;
        }
    
        public void setCount(Long count) {
            this.count = count;
        }
    
        public Long getError() {
            return error;
        }
    
        public void setError(Long error) {
            this.error = error;
        }
    
        public Double getMax() {
            return max;
        }
    
        public void setMax(Double max) {
            this.max = max;
        }
    
        public Double getMin() {
            return min;
        }
    
        public void setMin(Double min) {
            this.min = min;
        }
    
        public List<CallRelation> getCalls() {
            return calls;
        }
    
        public void setCalls(List<CallRelation> calls) {
            this.calls = calls;
        }
    
        public void addCalls(SGNode node, Long count) {
            CallRelation relation = new CallRelation(this, node, count);
            this.calls.add(relation);
        }
    }

    添加 Neo4j 关系类

    @RelationshipEntity(type = "call")
    public class CallRelation {
    
        public CallRelation() {
            this.name = "call";
        }
    
    
        public CallRelation(SGNode start, SGNode end, Long count) {
            this();
            this.startNode = start;
            this.endNode = end;
            this.count = count;
        }
    
        /**
         * Neo4j会分配的ID(节点唯一标识 当前类中有效)
         */
        @Id
        @GeneratedValue
        private Long id;
    
        private String name;
    
        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;
        }
    
        /**
         * 定义关系的起始节点 == StartNode
         */
    
        @StartNode
        private SGNode startNode;
    
        /**
         * 定义关系的终止节点 == EndNode
         */
    
        @EndNode
        private SGNode endNode;
    
    
        /**
         * 定义关系的属性
         */
    
        @Property(name = "count")
        private Long count;
    
        public SGNode getStartNode() {
            return startNode;
        }
    
        public void setStartNode(SGNode startNode) {
            this.startNode = startNode;
        }
    
        public SGNode getEndNode() {
            return endNode;
        }
    
        public void setEndNode(SGNode endNode) {
            this.endNode = endNode;
        }
    
        public Long getCount() {
            return count;
        }
    
        public void setCount(Long count) {
            this.count = count;
        }
    }

    添加 Reponsitory

    @Repository
    public interface SGNodeReponsitory extends Neo4jRepository<SGNode, Long> {
      // 此处用法可见 https://docs.spring.io/spring-data/neo4j/docs/5.1.2.RELEASE/reference/html/#_query_methods SGNode findByName(@Param(
    "name") String name); }

    使用 Demo

    @RestController
    @RequestMapping("/sg")
    public class SGNodeController {
    
        @Autowired
        SGNodeReponsitory sgNodeReponsitory;
    
        @DeleteMapping("/delete")
        public String delete() {
            sgNodeReponsitory.deleteAll();
            return "OK";
        }
    
        @GetMapping("/add")
        public String add() {
            addNodes();
            return "OK";
        }
    
        @GetMapping("/get")
        public String relation() {
            SGNode node = sgNodeReponsitory.findByName("tsp");
            List<Long> ids = new ArrayList<>();
            ids.add(node.getId());
            Iterable<SGNode> result = sgNodeReponsitory.findAllById(ids, 1);
            return "OK";
        }
    
    
        private void addNodes() {
            sgNodeReponsitory.deleteAll();
    
            List<SGNode> list = new ArrayList<>();
    
            SGNode node = new SGNode("tsp");
            list.add(node);
    
            for (Integer i = 1; i <= 10; i++) {
                node = new SGNode("tsp" + i);
                node.setCount(new Random().nextLong());
                node.setError(new Random().nextLong());
                node.setMax(new Random().nextDouble());
                node.setMin(new Random().nextDouble());
                list.add(node);
            }
    
            sgNodeReponsitory.saveAll(list);
    
            SGNode start = sgNodeReponsitory.findByName("tsp1");
            SGNode end = sgNodeReponsitory.findByName("tsp");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp2");
            end = sgNodeReponsitory.findByName("tsp");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp9");
            end = sgNodeReponsitory.findByName("tsp7");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp7");
            end = sgNodeReponsitory.findByName("tsp2");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp2");
            end = sgNodeReponsitory.findByName("tsp8");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp");
            end = sgNodeReponsitory.findByName("tsp3");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp");
            end = sgNodeReponsitory.findByName("tsp4");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp6");
            end = sgNodeReponsitory.findByName("tsp3");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp3");
            end = sgNodeReponsitory.findByName("tsp5");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
    
            start = sgNodeReponsitory.findByName("tsp5");
            end = sgNodeReponsitory.findByName("tsp10");
            start.addCalls(end, new Random().nextLong());
            sgNodeReponsitory.save(start);
        }
    }

    执行 Add 操作之后 

  • 相关阅读:
    使用Oracle PROFILE控制会话空闲时间
    ORACLE sid,pid,spid总结
    总结:基于Oracle Logminer数据同步
    从操作系统rm数据文件后,利用句柄与rman恢复的过程。(已验证)
    SPOJ GCDEX (数论)
    C++类构造函数
    [置顶] 删除:大数据取舍之道读书笔记
    paip.c++ qt 图片处理 检测损坏的图片
    paip.c++ qt 目录遍历以及文件操作
    AJAX最简单的原理以及应用
  • 原文地址:https://www.cnblogs.com/fqybzhangji/p/9952111.html
Copyright © 2020-2023  润新知