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

spring boot 2.0 neo4j 使用的更多相关文章
- Spring Boot 2.0官方文档之 Actuator(转)
执行器(Actuator)的定义 执行器是一个制造业术语,指的是用于移动或控制东西的一个机械装置,一个很小的改变就能让执行器产生大量的运动. An actuator is a manufacturin ...
- springboot2.0(一):【重磅】Spring Boot 2.0权威发布
就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- 业余草分享 Spring Boot 2.0 正式发布的新特性
就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner
Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...
- Spring Boot 2.0(四):使用 Docker 部署 Spring Boot
Docker 技术发展为微服务落地提供了更加便利的环境,使用 Docker 部署 Spring Boot 其实非常简单,这篇文章我们就来简单学习下. 首先构建一个简单的 Spring Boot 项目, ...
- spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析
spring boot升级到2.0.0以后,项目突然报出 NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBu ...
- Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收藏
云收藏项目已经开源2年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时很新的技术,现在看来其实很多新技术是没有必要使用的,但做为学习案例来讲确实是一个绝佳的 Spring ...
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
随机推荐
- css3多個佈局
分欄佈局: column-count:分欄數 column-gap:欄間間距 column-rule:欄間線條 記得消除瀏覽器兼容:-moz-和-webkit- 盒佈局: display:box 彈性 ...
- python三种回收机制
Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...
- 应用调试(四)系统调用SWI
目录 应用调试(四)系统调用SWI 系统调用 SWI代码片段分析 分析sys_write 构造sys_hello 应用程序调用SWI 嵌入汇编语法 测试APP 参考 title: 应用调试(四)系统调 ...
- 驱动调试(三)oops确定函数PC
目录 驱动调试(三)oops确定函数PC 什么是oops 流程简述 代码仓库 模块例子分析 找到PC值 判断是否属于模块 查看符号表 找到模块 反汇编模块 内核例子分析 找到PC值 判断是否属于模块 ...
- Vue基础之es6
什么是ECMAScript,以及es6的诞生? 1997年 ECMAScript 1.0 诞生 1999年12月 ECMAScript 3.0诞生,它 是一个巨大的成功,在业界得到了广泛的支持,它奠定 ...
- MapReduce 概述
定义 Hadoop MapReduce 是一个分布式运算程序的编程框架,用于轻松编写分布式应用程序,以可靠,容错的方式在大型集群(数千个节点)上并行处理大量数据(TB级别),是用户开发 “基于 Had ...
- G1 垃圾收集器入门
最近在复习Java GC,因为G1比较新,JDK1.7才正式引入,比较艰难的找到一篇写的很棒的文章,粘过来mark下.总结这篇文章和其他的资料,G1可以基本稳定在0.5s到1s左右的延迟,但是并不能保 ...
- 流程控制if、while、for
if判断 if判断想执行第一个条件,if后的判断必须是True 1 什么是if判断 判断一个条件如果成立则做...不成立则做....2 为何要有if判断 让计算机能够像人一样具有判断的能力3 如何 ...
- JavaLinkedHashSet练习
题目三: 1.键盘录入一个字符串,去掉其中重复字符 2.打印出不同的那些字符,必须保证顺序.例如输入:aaaabbbcccddd,打印结果为:abcd.尝试用两种方法解决字符串删除问题,一是使用Has ...
- [Luogu P1516]青蛙的约会
按照题意,显然可以列出同余方程,k即为所求天数,再将其化为不定方程 ,那么对这个方程用扩展欧几里德算法即可得出k,q的一组解,但是方程有解的充要条件是(m – n) 和L不同时为零并且x – y是m ...