IT序号网

spring boot 2.0 neo4j 使用

wyy 2021年06月14日 数据库 243 0

参考文档

  官方文档

  • 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(</span>"${spring.data.neo4j.uri}"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String databaseUrl; 
 
@Value(</span>"${spring.data.neo4j.username}"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String userName; 
 
@Value(</span>"${spring.data.neo4j.password}"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String password; 
 
@Bean 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SessionFactory sessionFactory() { 
    org.neo4j.ogm.config.Configuration configuration </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> org.neo4j.ogm.config.Configuration.Builder() 
            .uri(databaseUrl) 
            .credentials(userName, password) 
            .build(); 
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> SessionFactory(configuration, "com.example.demo.entity"<span style="color: #000000;">); 
} 
 
@Bean 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Neo4jTransactionManager transactionManager() { 
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> Neo4jTransactionManager(sessionFactory()); 
} 

}

复制代码

添加 Neo4j 节点类

复制代码
@NodeEntity 
public class SGNode { 
    private Long count; 
    private Long error; 
    private Double max; 
    private Double min; 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * Neo4j会分配的ID(节点唯一标识 当前类中有效) 
 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> 
@Id 
@GeneratedValue 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Long id; 
 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String name; 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Long getId() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> id; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setId(Long id) { 
    </span><span style="color: #0000ff;">this</span>.id =<span style="color: #000000;"> id; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getName() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setName(String name) { 
    </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name; 
} 
 
@Relationship(type </span>= "call"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">private</span> List&lt;CallRelation&gt;<span style="color: #000000;"> calls; 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SGNode() { 
    count </span>= 0L<span style="color: #000000;">; 
    error </span>= 0L<span style="color: #000000;">; 
    max </span>= 0.0<span style="color: #000000;">; 
    min </span>= 0.0<span style="color: #000000;">; 
    calls </span>= <span style="color: #0000ff;">new</span> ArrayList&lt;&gt;<span style="color: #000000;">(); 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SGNode(String name) { 
    </span><span style="color: #0000ff;">this</span><span style="color: #000000;">(); 
    </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Long getCount() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> count; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setCount(Long count) { 
    </span><span style="color: #0000ff;">this</span>.count =<span style="color: #000000;"> count; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Long getError() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> error; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setError(Long error) { 
    </span><span style="color: #0000ff;">this</span>.error =<span style="color: #000000;"> error; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Double getMax() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> max; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setMax(Double max) { 
    </span><span style="color: #0000ff;">this</span>.max =<span style="color: #000000;"> max; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Double getMin() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> min; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setMin(Double min) { 
    </span><span style="color: #0000ff;">this</span>.min =<span style="color: #000000;"> min; 
} 
 
</span><span style="color: #0000ff;">public</span> List&lt;CallRelation&gt;<span style="color: #000000;"> getCalls() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> calls; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setCalls(List&lt;CallRelation&gt;<span style="color: #000000;"> calls) { 
    </span><span style="color: #0000ff;">this</span>.calls =<span style="color: #000000;"> calls; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> addCalls(SGNode node, Long count) { 
    CallRelation relation </span>= <span style="color: #0000ff;">new</span> CallRelation(<span style="color: #0000ff;">this</span><span style="color: #000000;">, node, count); 
    </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.calls.add(relation); 
} 

}

复制代码

添加 Neo4j 关系类

复制代码
@RelationshipEntity(type = "call") 
public class CallRelation { 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> CallRelation() { 
    </span><span style="color: #0000ff;">this</span>.name = "call"<span style="color: #000000;">; 
} 
 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> CallRelation(SGNode start, SGNode end, Long count) { 
    </span><span style="color: #0000ff;">this</span><span style="color: #000000;">(); 
    </span><span style="color: #0000ff;">this</span>.startNode =<span style="color: #000000;"> start; 
    </span><span style="color: #0000ff;">this</span>.endNode =<span style="color: #000000;"> end; 
    </span><span style="color: #0000ff;">this</span>.count =<span style="color: #000000;"> count; 
} 
 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * Neo4j会分配的ID(节点唯一标识 当前类中有效) 
 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> 
@Id 
@GeneratedValue 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Long id; 
 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String name; 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Long getId() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> id; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setId(Long id) { 
    </span><span style="color: #0000ff;">this</span>.id =<span style="color: #000000;"> id; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getName() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setName(String name) { 
    </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name; 
} 
 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * 定义关系的起始节点 == StartNode 
 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> 
 
@StartNode 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> SGNode startNode; 
 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * 定义关系的终止节点 == EndNode 
 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> 
 
@EndNode 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> SGNode endNode; 
 
 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * 定义关系的属性 
 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> 
 
@Property(name </span>= "count"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Long count; 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SGNode getStartNode() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> startNode; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setStartNode(SGNode startNode) { 
    </span><span style="color: #0000ff;">this</span>.startNode =<span style="color: #000000;"> startNode; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SGNode getEndNode() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> endNode; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setEndNode(SGNode endNode) { 
    </span><span style="color: #0000ff;">this</span>.endNode =<span style="color: #000000;"> endNode; 
} 
 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Long getCount() { 
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> count; 
} 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setCount(Long count) { 
    </span><span style="color: #0000ff;">this</span>.count =<span style="color: #000000;"> 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(</span>"/delete"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String delete() { 
    sgNodeReponsitory.deleteAll(); 
    </span><span style="color: #0000ff;">return</span> "OK"<span style="color: #000000;">; 
} 
 
@GetMapping(</span>"/add"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String add() { 
    addNodes(); 
    </span><span style="color: #0000ff;">return</span> "OK"<span style="color: #000000;">; 
} 
 
@GetMapping(</span>"/get"<span style="color: #000000;">) 
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String relation() { 
    SGNode node </span>= sgNodeReponsitory.findByName("tsp"<span style="color: #000000;">); 
    List</span>&lt;Long&gt; ids = <span style="color: #0000ff;">new</span> ArrayList&lt;&gt;<span style="color: #000000;">(); 
    ids.add(node.getId()); 
    Iterable</span>&lt;SGNode&gt; result = sgNodeReponsitory.findAllById(ids, 1<span style="color: #000000;">); 
    </span><span style="color: #0000ff;">return</span> "OK"<span style="color: #000000;">; 
} 
 
 
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> addNodes() { 
    sgNodeReponsitory.deleteAll(); 
 
    List</span>&lt;SGNode&gt; list = <span style="color: #0000ff;">new</span> ArrayList&lt;&gt;<span style="color: #000000;">(); 
 
    SGNode node </span>= <span style="color: #0000ff;">new</span> SGNode("tsp"<span style="color: #000000;">); 
    list.add(node); 
 
    </span><span style="color: #0000ff;">for</span> (Integer i = 1; i &lt;= 10; i++<span style="color: #000000;">) { 
        node </span>= <span style="color: #0000ff;">new</span> SGNode("tsp" +<span style="color: #000000;"> i); 
        node.setCount(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
        node.setError(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
        node.setMax(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextDouble()); 
        node.setMin(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextDouble()); 
        list.add(node); 
    } 
 
    sgNodeReponsitory.saveAll(list); 
 
    SGNode start </span>= sgNodeReponsitory.findByName("tsp1"<span style="color: #000000;">); 
    SGNode end </span>= sgNodeReponsitory.findByName("tsp"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp2"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp9"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp7"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp7"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp2"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp2"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp8"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp3"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp4"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp6"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp3"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp3"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp5"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
 
    start </span>= sgNodeReponsitory.findByName("tsp5"<span style="color: #000000;">); 
    end </span>= sgNodeReponsitory.findByName("tsp10"<span style="color: #000000;">); 
    start.addCalls(end, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random().nextLong()); 
    sgNodeReponsitory.save(start); 
} 

}

复制代码

执行 Add 操作之后 

原文地址:https://www.cnblogs.com/fengqiaoyebo/p/9952111.html


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

通过Spring Data Neo4J操作您的图形数据库