SpringBoot + docker + neo4j
下拉镜像
docker pull neo4j
启动镜像
docker run -d -p 7473:7473 -p 7687:7687 -p 7474:7474 neo4j
打开浏览器:http://192.168.31.146:7474/browser/
用户名/密码初始值为:neo4j
首次登陆需要修改密码
登陆后界面
新建springboot项目,添加pom引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.voodoodyne.jackson.jsog/jackson-jsog -->
<dependency>
<groupId>com.voodoodyne.jackson.jsog</groupId>
<artifactId>jackson-jsog</artifactId>
<version>1.1.</version>
</dependency>
添加Actor类
package org.mythsky.neo4jdemo; import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity; @JsonIdentityInfo(generator = JSOGGenerator.class)
@NodeEntity
public class Actor {
@GraphId Long id;
private String name;
private int born; 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;
} public int getBorn() {
return born;
} public void setBorn(int born) {
this.born = born;
}
}
添加Movie类
package org.mythsky.neo4jdemo; import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship; import java.util.ArrayList;
import java.util.List; @JsonIdentityInfo(generator = JSOGGenerator.class)
@NodeEntity
public class Movie {
@GraphId
Long id;
String title;
String year;
String tagline;
@Relationship(type = "ACTS_IN",direction =Relationship.INCOMING)
List<Role> roles = new ArrayList<>(); public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getYear() {
return year;
} public void setYear(String year) {
this.year = year;
} public String getTagline() {
return tagline;
} public void setTagline(String tagline) {
this.tagline = tagline;
} public List<Role> getRoles() {
return roles;
} public void setRoles(List<Role> roles) {
this.roles = roles;
} public Movie() { } public Role addRole(Actor actor, String name){
Role role=new Role(name,actor,this);
this.roles.add(role);
return role; }
}
添加Role类
package org.mythsky.neo4jdemo; import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode; @JsonIdentityInfo(generator = JSOGGenerator.class)
@RelationshipEntity(type = "ACTS_IN")
public class Role {
@GraphId
Long id;
String role;
@StartNode
Actor actor;
@EndNode
Movie movie; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getRole() {
return role;
} public void setRole(String role) {
this.role = role;
} public Actor getActor() {
return actor;
} public void setActor(Actor actor) {
this.actor = actor;
} public Movie getMovie() {
return movie;
} public void setMovie(Movie movie) {
this.movie = movie;
} public Role(String role, Actor actor, Movie movie) { this.role = role;
this.actor = actor;
this.movie = movie;
} public Role() { }
}
添加查询接口MovieRepository
package org.mythsky.neo4jdemo; import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; @Repository
public interface MovieRepository extends GraphRepository<Movie> {
Movie findByTitle(@Param("title") String title);
}
添加配置类
package org.mythsky.neo4jdemo; import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories public class Neo4jConfig {
@Bean
public SessionFactory sessionFactory() {
return new SessionFactory("org.mythsky.neo4jdemo");
} @Bean
public Neo4jTransactionManager transactionManager() {
return new Neo4jTransactionManager(sessionFactory());
}
}
添加配置文件ogm.properties
compiler=org.neo4j.ogm.compiler.MultiStatementCypherCompiler
driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://192.168.31.146:7474
username = neo4j
password = your own password
单元测试
package org.mythsky.neo4jdemo; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@ContextConfiguration(classes = {Neo4jConfig.class})
@SpringBootTest
public class Neo4jDemoApplicationTests {
private static Logger logger= LoggerFactory.getLogger(Neo4jDemoApplicationTests.class);
@Autowired
MovieRepository movieRepository;
@Before
public void initData(){
movieRepository.deleteAll(); Movie matrix1=new Movie();
matrix1.setTitle("The Matrix");
matrix1.setYear("1999-03-31"); Movie matrix2=new Movie();
matrix2.setTitle("The Matrix Reloaded");
matrix2.setYear("2003-05-07"); Movie matrix3=new Movie();
matrix3.setTitle("The Matrix Revolutions");
matrix3.setYear("2003-10-27"); Actor keanu=new Actor();
keanu.setName("Keanu Reeves"); Actor laurence=new Actor();
laurence.setName("Laurence Fishburne"); Actor carrieanne=new Actor();
carrieanne.setName("Carrie-Anne Moss"); matrix1.addRole(keanu,"Neo");
matrix1.addRole(laurence,"Morpheus");
matrix1.addRole(carrieanne,"Trinity");
movieRepository.save(matrix1);
Assert.assertNotNull(matrix1.getId()); matrix2.addRole(keanu,"Neo");
matrix2.addRole(laurence,"Morpheus");
matrix2.addRole(carrieanne,"Trinity");
movieRepository.save(matrix2);
Assert.assertNotNull(matrix2.getId()); matrix3.addRole(keanu,"Neo");
matrix3.addRole(laurence,"Morpheus");
matrix3.addRole(carrieanne,"Trinity");
movieRepository.save(matrix3);
Assert.assertNotNull(matrix3.getId());
}
@Test
public void get() {
Movie movie=movieRepository.findByTitle("The Matrix");
Assert.assertNotNull(movie);
logger.info("===movie===movie:{},{}",movie.getTitle(),movie.getYear());
for(Role role:movie.getRoles()){
logger.info("=====actor:{},role:{}",role.getActor().getName(),role.getRole());
}
} }
运行测试
在浏览器查看数据
SpringBoot + docker + neo4j的更多相关文章
- windows环境 springboot+docker开发环境搭建与hello word
1,下载安装 docker toolbox 下载地址:http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ docker t ...
- SpringBoot Docker Mysql安装,Docker安装Mysql
SpringBoot Docker Mysql安装,Docker安装Mysql ================================ ©Copyright 蕃薯耀 2018年4月8日 ht ...
- SpringBoot Docker入门,SpringBoot Docker安装
SpringBoot Docker入门,SpringBoot Docker安装 ================================ ©Copyright 蕃薯耀 2018年4月8日 ht ...
- 【快学SpringBoot】SpringBoot+Docker构建、运行、部署应用
前言 Docker技术发展为当前流行的微服务提供了更加便利的环境,使用SpringBoot+Docker部署和发布应用,其实也是一件比较简单的事情.当前,前提是得有Docker的基础. 源码在文末 文 ...
- springboot docker jenkins 自动化部署并上传镜像
springboot + docker + jenkins自动化部署项目,jenkins.mysql.redis都是docker运行的,并且没有使用虚拟机,就在阿里云服务器(centos7)运行 1. ...
- 凭借SpringBoot整合Neo4j,我理清了《雷神》中错综复杂的人物关系
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 虽然距离中秋放假还要熬过漫长的两天,不过也有个好消息,今天是<雷神4>上线Disney+流媒体的日子 ...
- 第三十七章 springboot+docker(手动部署)
一.下载centos镜像 docker pull hub.c.163.com/library/centos:latest docker tag containId centos:7 docker ru ...
- Java开源博客My-Blog(SpringBoot+Docker)系列文章
My Blog 1.Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦 2.My-Blog搭建过程:如何让一个网站从零到可以上线访问 3.将数据的初始化放到 ...
- 【第三十七章】 springboot+docker(手动部署)
一.下载centos镜像 docker pull hub.c.163.com/library/centos:latest docker tag containId centos:7 docker ru ...
随机推荐
- redis概览
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括string(字符串 ...
- MFC载入BMP图片
两步 hBitmap = (HBITMAP)LoadImage(NULL,fullPathName,IMAGE_BITMAP,120,120,LR_LOADFROMFILE);//载入图片 m_pic ...
- security.php
<?php /** * */ class Security { public function csrf_verify() { if(count($_POST) == 0) { return ' ...
- hdu 5086 数列连续和求和
http://acm.hdu.edu.cn/showproblem.php?pid=5086 求一段数列里面所有连续和的和,卡精度 规律很明显,数列里面每个数都被加了i*(n+1-i)次 注意下精度即 ...
- hdu4742
题意:给定3维的n(<=100000)个点,求最长不下降子序列长度(对于1和2两个点,2可以放在1后面,x1<=x2,y1<=y2,z1<=z2 ),并求出有多少种方案. 思路 ...
- AngularJS 单元测试 Karma jasmine
当AngularJS项目越来越大时候,需要进行单元测试,可以先开发功能再进行测试,也可以先进行测试. 一.karma 是一个基于Node.js(先要安装)的JavaScript测试执行过程管理工具( ...
- ASP.NET Web API 框架研究 Self Host模式下的消息处理管道
Self Host模式下的ASP.NET Web API与WCF非常相似,都可以寄宿在任意类型的托管应用程序中,宿主可以是Windows Form .WPF.控制台应用以及Windows Servic ...
- delphi PosAnsi
function ValidateName(n: string): string; var banned, res: string; i,j: integer; begin res:= n; bann ...
- .net MVC, webAPI,webForm集成steeltoe+springcloud实现调用服务中心服务的总结
开始之前,如果没接触过Autofac的,可以移步到Autofac官方示例学习一下怎么使用:https://github.com/autofac/Examples .net 下集成steeltoe进行微 ...
- socket粗解
百度定义:网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket通信流程: 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一 ...