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;

    @Property(name = "name")
    private String name;

    @Property(name = "born")
    private int born;

    public Long getNodeId() {
        return nodeId;
    }

    public void setNodeId(Long nodeId) {
        this.nodeId = nodeId;
    }


    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;
    }
}
  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 {

    @Autowired
    private PersonRepository personRepository;
    @Autowired
    private MovieRepository movieRepository;
    @Autowired
    private DirectedRepository directedRepository;
    @Autowired
    private ActedInRepository actedInRepository;

    @Override
    public Person addPerson(Person person){
        return personRepository.save(person);
    }
    @Override
    public Person findOnePerson(long id){
        return personRepository.findById(id).get();
    }
    @Override
    public void deleteOnePerson(long id){
        personRepository.deleteById(id);
    }
    @Override
    public Movie addMovie(Movie movie){
        return movieRepository.save(movie);
    }
    @Override
    public Movie findOneMovie(long id){
        return movieRepository.findById(id).get();
    }
    @Override
    public Directed directed(Directed directed){
        return directedRepository.save(directed);
    }
    @Override
    public ActedIn actedIn(ActedIn actedIn) {
        return actedInRepository.save(actedIn);
    }
}

代码

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

猜你喜欢

转载自www.cnblogs.com/jpfss/p/11232218.html