参考文档

  官方文档

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

spring boot 2.0 neo4j 使用的更多相关文章

  1. Spring Boot 2.0官方文档之 Actuator(转)

    执行器(Actuator)的定义 执行器是一个制造业术语,指的是用于移动或控制东西的一个机械装置,一个很小的改变就能让执行器产生大量的运动. An actuator is a manufacturin ...

  2. springboot2.0(一):【重磅】Spring Boot 2.0权威发布

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  3. 业余草分享 Spring Boot 2.0 正式发布的新特性

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  4. Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner

    Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...

  5. Spring Boot 2.0(四):使用 Docker 部署 Spring Boot

    Docker 技术发展为微服务落地提供了更加便利的环境,使用 Docker 部署 Spring Boot 其实非常简单,这篇文章我们就来简单学习下. 首先构建一个简单的 Spring Boot 项目, ...

  6. spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析

    spring boot升级到2.0.0以后,项目突然报出 NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBu ...

  7. Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收藏

    云收藏项目已经开源2年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时很新的技术,现在看来其实很多新技术是没有必要使用的,但做为学习案例来讲确实是一个绝佳的 Spring ...

  8. Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...

  9. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

随机推荐

  1. EntityFramework Core笔记:查询数据(3)

    1. 基本查询 1.1 加载全部数据 using System.Linq; using (var context = new LibingContext()) { var roles = contex ...

  2. mysql的时区错误问题: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one

    问题:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.. ...

  3. 洛谷 P1443 马的遍历

    终于遇到一个简单纯粹一点的bfs了...... 题目链接:https://www.luogu.org/problemnew/show/P1443 题目是求到达一个点的最短步数 也就是说我只要bfs遍历 ...

  4. Codeforces Round #551 (Div. 2) 题解

    CF1153A 直接做啊,分类讨论即可 #include<iostream> #include<string.h> #include<string> #includ ...

  5. 机器学习之正则化【L1 & L2】

    前言 L1.L2在机器学习方向有两种含义:一是L1范数.L2范数的损失函数,二是L1.L2正则化 L1范数.L2范数损失函数 L1范数损失函数: L2范数损失函数: L1.L2分别对应损失函数中的绝对 ...

  6. java调用matlab绘图

    一 注意事项 1: MatLab的版本必须是2006b+(包括2006b或更高版本),因为只有在这些版本中才有MATLAB Builder for Java(也叫Java Builder). 2: 运 ...

  7. C++回顾day03---<纯虚函数和抽象类以及虚析构函数,delete使用>

    一:纯虚函数和抽象类 纯虚函数是一个在基类中说明的虚函数,在基类中没有定义,要求任何派生类都定义自己的版本 纯虚函数为各个派生类提供一个公共接口 纯虚函数的形式: virtual 类型 函数名(参数列 ...

  8. Quartz.net 3.x使用总结(一)——入门介绍

    1.Quartz.net简介 Quartz.NET是一个强大.开源.轻量级的任务调度框架.任务调度在我们的开发中经常遇到,如说:每天晚上三点让程序或网站执行某些代码,或者每隔5秒种执行一个方法等.Wi ...

  9. Spark 用户自定义函数 Java 示例

    Spark UDF Java 示例 在这篇文章中提到了用Spark做用户昵称文本聚类分析,聚类需要选定K个中心点,然后迭代计算其他样本点到中心点的距离.由于中文文字分词之后(n-gram)再加上昵称允 ...

  10. strace -> System call tracer

    我只想告诉你一件事: strace 可以让你知道程序调用了哪些syscall.