Spring Boot:Boot2.0版本整合Neo4j

前面介绍了Boot 1.5版本集成Neo4j,Boot 2.0以上版本Neo4j变化较大。
场景还是电影人员关系

Boot 2.0主要变化

  1. GraphRepository在Boot2.0下不支持了,调整为Neo4jRepository。
    对应的findById和deleteById也要调整。
  2. @GraphId也不支持需要改成@Id @GeneratedValue 。
  3. 链接方式调整为bolt://192.168.1.8:7687

Demo

  1. 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>
  1. application.yml配置
spring:
  data:
    neo4j:
      uri: bolt://192.168.1.8:7687
      username: neo4j
      password: 123456
  1. 模型类调整为
    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;
}

}

  1. 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> {
}

  1. 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
posted @ 2019-07-23 15:47  星朝  阅读(2011)  评论(0编辑  收藏  举报