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 ...
随机推荐
- 整合Spring5+Struts2.5+Hibernate5+maven
1. 使用Eclipse创建Maven项目 2. 配置pom.xml引入需要的依赖包 <dependencies> <dependency> <groupId>ju ...
- 使用mongo-express管理mongodb数据库
前面的话 本文将详细介绍一款用nodejs开发的基于Web的mongodb数据库管理工具mongo-express 安装 首先,全局安装 mongo-express 包 npm install -g ...
- Ueditor注意的地方
复制粘贴内容到编辑器上时,一些标签的属性会被过滤,在config.js里添加白名单配置项,例如: whitList: { a: ['target', 'href', 'title', 'class', ...
- Vivado如何使用命令行创建工程
前言 vivado中采用TCL脚本语言来作为其命令解释语言.除去可以普通的图形界面流程还可以使用tcl脚本创建工程并导入相关源文件. 流程 1.首先还是要打开vivado图形主界面. 2.在某路径 ...
- MT【315】勾股数
(高考压轴题)证明以下命题:(1)对任意正整数$a$都存在正整数$b,c(b<c)$,使得$a^2,b^2,c^2$成等差数列.(2)存在无穷多个互不相似的三角形$\Delta_n$,其边长$a ...
- Java【第七篇】面向对象之类设计
Java类的继承 类继承语法规则 < 修饰符> class < 子类名称> [extends < 父类>] { <属性和方法的声明> } 类的继承 子 ...
- apache beam ElasticSearchIO 遇到异常后job中断执行 自己定制beam IO
可以将 ElasticSearchIO.java 源码拷贝至自己的项目.改名为MyElasticSearchIO.java.自己定制实现.这时编译会遇到auto.value失败.在pom.xml中加入 ...
- DP擎天
DP! 黄题: 洛谷P2101 命运石之门的选择 假装是DP(分治 + ST表) CF 982C Cut 'em all! 树形贪心 洛谷P1020 导弹拦截 单调队列水题 绿题: 洛谷P1594 护 ...
- windows下用pycharm安装tensorflow简易教程
https://blog.csdn.net/heros_never_die/article/details/79760616 最近开始学习深度学习的相关知识,准备实战一下,看了一些关于tensorfl ...
- expansion pattern ‘Frame&’ contains no argument packs
camera/CameraImpl.h::: error: expansion pattern ‘Frame&’ contains no argument packs void read_fr ...