• 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 操作之后 

  • 相关阅读:
    OpenCV运动检测跟踪(blob track)框架组成模块详解
    C# 控制台应用程序中输出彩色字体
    C#获取隐藏的文件
    用c#怎么比较两张图片的不同
    清除浏览器缓存
    C#判断系统是否已经连接上网络
    如何删除VS2005中显示的最近项目
    设计模式:模板方法模式(Template Method)
    【SQL】数据库对象中查找某一关键字
    【问答】.NET面试题
  • 原文地址:https://www.cnblogs.com/sea520/p/11845133.html
Copyright © 2020-2023  润新知