Spring Boot:Boot2.0版本整合Neo4j
前面介绍了Boot 1.5版本集成Neo4j,Boot 2.0以上版本Neo4j变化较大。
场景还是电影人员关系
Boot 2.0主要变化
- GraphRepository在Boot2.0下不支持了,调整为Neo4jRepository。
对应的findById和deleteById也要调整。 - @GraphId也不支持需要改成@Id @GeneratedValue 。
- 链接方式调整为bolt://192.168.1.8:7687
Demo
- pom文件
调整boot的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
- application.yml配置
spring:
data:
neo4j:
uri: bolt://192.168.1.8:7687
username: neo4j
password: 123456
- 模型类调整为
Person类
package com.github.davidji80.springboot.neo4j.model;
import org.neo4j.ogm.annotation.*;
@NodeEntity(label = "Person")
public class Person {
@Id
@GeneratedValue
private Long nodeId;
<span class="hljs-meta">@Property</span>(name = <span class="hljs-string">"name"</span>)
<span class="hljs-keyword">private</span> String name;
<span class="hljs-meta">@Property</span>(name = <span class="hljs-string">"born"</span>)
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> born;
<span class="hljs-function"><span class="hljs-keyword">public</span> Long <span class="hljs-title">getNodeId</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> nodeId;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setNodeId</span><span class="hljs-params">(Long nodeId)</span> </span>{
<span class="hljs-keyword">this</span>.nodeId = nodeId;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> name;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>{
<span class="hljs-keyword">this</span>.name = name;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getBorn</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> born;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setBorn</span><span class="hljs-params">(<span class="hljs-keyword">int</span> born)</span> </span>{
<span class="hljs-keyword">this</span>.born = born;
}
}
- DAO调整为
PersonRepository
package com.github.davidji80.springboot.neo4j.dao;
import com.github.davidji80.springboot.neo4j.model.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
public interface PersonRepository extends Neo4jRepository<Person, Long> {
}
- Service层调整
package com.github.davidji80.springboot.neo4j.service.impl;
import com.github.davidji80.springboot.neo4j.dao.ActedInRepository;
import com.github.davidji80.springboot.neo4j.dao.DirectedRepository;
import com.github.davidji80.springboot.neo4j.dao.MovieRepository;
import com.github.davidji80.springboot.neo4j.dao.PersonRepository;
import com.github.davidji80.springboot.neo4j.model.ActedIn;
import com.github.davidji80.springboot.neo4j.model.Directed;
import com.github.davidji80.springboot.neo4j.model.Movie;
import com.github.davidji80.springboot.neo4j.model.Person;
import com.github.davidji80.springboot.neo4j.service.MovieServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class MovieServiceImpl implements MovieServer {
@<span class="hljs-keyword">Autowired</span>
private PersonRepository personRepository;
@<span class="hljs-keyword">Autowired</span>
private MovieRepository movieRepository;
@<span class="hljs-keyword">Autowired</span>
private DirectedRepository directedRepository;
@<span class="hljs-keyword">Autowired</span>
private ActedInRepository actedInRepository;
@<span class="hljs-keyword">Override</span>
public Person addPerson(Person person){
<span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">personRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">person</span>);
}
@<span class="hljs-keyword">Override</span>
public Person findOnePerson(long id){
<span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">personRepository</span><span class="hljs-selector-class">.findById</span>(<span class="hljs-selector-tag">id</span>)<span class="hljs-selector-class">.get</span>();
}
@<span class="hljs-keyword">Override</span>
public void deleteOnePerson(long id){
<span class="hljs-selector-tag">personRepository</span><span class="hljs-selector-class">.deleteById</span>(<span class="hljs-selector-tag">id</span>);
}
@<span class="hljs-keyword">Override</span>
public Movie addMovie(Movie movie){
<span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">movieRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">movie</span>);
}
@<span class="hljs-keyword">Override</span>
public Movie findOneMovie(long id){
<span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">movieRepository</span><span class="hljs-selector-class">.findById</span>(<span class="hljs-selector-tag">id</span>)<span class="hljs-selector-class">.get</span>();
}
@<span class="hljs-keyword">Override</span>
public Directed directed(Directed directed){
<span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">directedRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">directed</span>);
}
@<span class="hljs-keyword">Override</span>
public ActedIn actedIn(ActedIn actedIn) {
<span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">actedInRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">actedIn</span>);
}
}
代码
https://github.com/DavidJi80/springboot
v0.8.1
参考
https://blog.csdn.net/d597180714/article/details/81079848
https://www.cnblogs.com/zhangboyu/p/7580262.html
原文地址:https://www.jianshu.com/p/1aeeefb4fc7a
Spring Boot:Boot2.0版本整合Neo4j的更多相关文章
- Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)
原文:https://blog.csdn.net/hubo_88/article/details/80671192 Spring Boot Admin 用于监控基于 Spring Boot 的应用,它 ...
- Spring Boot 2.0 新特性
这是一篇总结文章,主要收集 Spring Boot 2.0 相对于 Spring Boot 1.x 的新特性,本章节并不提供实践性质的源代码.在 Spring Boot 系列文章中会持续退出实践章节. ...
- spring boot 2.0(一)权威发布spring boot2.0
Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...
- spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析
spring boot升级到2.0.0以后,项目突然报出 NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBu ...
- Spring Boot 2.0 教程 | 快速集成整合消息中间件 Kafka
欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...
- Spring Boot 2.0 快速集成整合消息中间件 Kafka
欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...
- Spring Boot 2.0 整合携程Apollo配置中心
原文:https://www.jianshu.com/p/23d695af7e80 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够 ...
- spring boot 2.0 整合 elasticsearch6.5.3,spring boot 2.0 整合 elasticsearch NoNodeAvailableException
原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 原码云项目地址:https://gi ...
- Spring Boot 2.0 整合 FreeMarker 模板引擎
本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战. 1. 创建新的项目 2. 填写项目配置信息 3. 勾选web 模块 4. 勾选freemarker模 ...
随机推荐
- jQuery实现textarea高度根据内容自适应
//jQuery实现textarea高度根据内容自适应 $.fn.extend({ txtaAutoHeight: function () { return this.each(function () ...
- Solr+HBase
- 如何将Map键值的下划线转为驼峰
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:如何将Map键值的下划线转为驼峰: 例,将HashMap实例extMap键值下划线转为驼峰: 代码: HashMap<String ...
- 百度ueditor解决页面组件被覆盖问题
本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:百度ueditor解决页面组件被覆盖问题: 在使用ueditor的过程中,会出现表单组件被ueditor覆盖的问题,解决的方式如下: ue ...
- [转]C#中用NamedPipe进程间通信
转自:http://blog.csdn.net/jinjazz/archive/2009/02/03/3861143.aspx 本文只是一个测试例子,核心代码是kernel32.dll中的一组wind ...
- Odoo中使用的部分表名及用途
res_users 用户res_groups 用户组(角色)res_lang 语言res_partner 供应商/客户/联系人res_font 字体res_company 公司res_bank 银行r ...
- sql join 的一次小使用
表为: 列名:站号,模式名,偏差,日期,要素 试图查询每个站中最小的那个偏差的模式名 create table B as SELECT stationid,min(abserror) as minab ...
- JAVA_环境配置
1:系统环境 windows10 64位 jdk版本:jdk-8u131-windows-x64.exe,下载地址:http://www.oracle.com/technetwork/java/jav ...
- python 为 class 添加新的属性和方法
通过继承: >>> class Point(namedtuple('Point', ['x', 'y'])): ... __slots__ = () ... @property .. ...
- String、StringBuffer和StringBuilder源码解析
1.String 1.1类的定义 public final class String implements java.io.Serializable, Comparable<String> ...