博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot 2.0 neo4j 使用
阅读量:6934 次
发布时间:2019-06-27

本文共 8040 字,大约阅读时间需要 26 分钟。

参考文档

  官方文档

  • 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")@EnableTransactionManagementpublic 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 节点类

@NodeEntitypublic 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
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
getCalls() { return calls; } public void setCalls(List
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

@Repositorypublic interface SGNodeReponsitory extends Neo4jRepository
{
  // 此处用法可见 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
ids = new ArrayList<>(); ids.add(node.getId()); Iterable
result = sgNodeReponsitory.findAllById(ids, 1); return "OK"; } private void addNodes() { sgNodeReponsitory.deleteAll(); List
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 操作之后 

 

转载于:https://www.cnblogs.com/fqybzhangji/p/9952111.html

你可能感兴趣的文章
使用 qrcodejs 生成二维码的几个问题
查看>>
ES6-Promise对象
查看>>
记录一次面试题
查看>>
Flutter Exception降到万分之几的秘密
查看>>
Fiddler抓取数据并分析(完整的配置教程)
查看>>
Keras入门(一)搭建深度神经网络(DNN)解决多分类问题
查看>>
【思维导图-索引篇】搞定数据库索引就是这么简单
查看>>
Kotlin如何避免“!!”(非空断言)
查看>>
我理解的 iOS 与 Android 的区别
查看>>
vue(2)
查看>>
Ts + React + Mobx 实现移动端浏览器控制台
查看>>
聊聊 scala 的模式匹配
查看>>
阿里推出 PolarFS 分布式文件系统:将存储与计算分开(附论文)
查看>>
【数据科学系统学习】机器学习算法 # 西瓜书学习记录 [6] 朴素贝叶斯实践
查看>>
vue 优化CDN加速
查看>>
Linux常用命令
查看>>
PHP 实现Office word 关键词添加背景色
查看>>
搞懂 Javascript 继承原理
查看>>
CentOS(linux) 下JDK的安装
查看>>
iview-admin webpack打包总结的几个问题
查看>>