SpringBoot 整合Neo4j

1. 导入jar包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rpp</groupId>
    <artifactId>springboot_neo4j_demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-bolt-driver</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2. 建立实体类

@NodeEntity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    @Property("cid")
    private int pid;
    private String name;
    private String character;
    private double money;
    private int age;
    private String description;
    @Relationship(type = "Friends", direction = Relationship.OUTGOING)
    private Set<Person> friendsPerson;

    public Person(Long id, int pid, String name, String character, double money, int age, String description) {
        this.id = id;
        this.pid = pid;
        this.name = name;
        this.character = character;
        this.money = money;
        this.age = age;
        this.description = description;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", pid=" + pid +
                ", name='" + name + '\'' +
                ", character='" + character + '\'' +
                ", money=" + money +
                ", age=" + age +
                ", description='" + description + '\'' +
                ", friendsPerson=" + friendsPerson +
                '}';
    }

    //省略 getxx() setxx()
}

3. 数据持久化类

import com.rpp.bean.Person;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {
     
    /**
     * 查看money 大于指定值的Person 列表
     */
    //@Query("match(p:Person) where p.money>{0} return p")
    @Query("match(p:Person) where p.money>{money} return p")
    List<Person> personList(@Param("money") double money);

    /**
     * 指定开始的名字 和 结束的名字 查询最短路径  限定深度为4以层包含4
     */
    @Query("match p=shortestPath((person:Person{name:{startName}})-[*1..4]-(person2:Person {name:{endName}})) return p")
    List<Person> shortestPath(@Param("startName") String startName, @Param("endName") String endName);

    @Query("match p =(person:Person {name:{name}})-[*1..2]-(:Person) return p")
    List<Person> personListDept(@Param("name") String name);
}

4. 配置文件 application.yml

spring:
  data:
    neo4j:
      username: neo4j
      password: 123456
      uri:  bolt://127.0.0.1:7687
      #uri: http://127.0.0.1:7474 
      #uri: file:///target/graph.db

5. 编写服务类

import com.rpp.bean.Person;
import com.rpp.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("personService")
public class Neo4jPersonService {

    @Autowired
    private PersonRepository personRepository;

    public List<Person> getAll() {
        List<Person> datas = new ArrayList<>();
        personRepository.findAll().forEach(person -> datas.add(person));
        return datas;
    }

    public Person save(Person person) {
        return personRepository.save(person);
    }

    public List<Person> personList(double money) {
        return personRepository.personList(money);
    }

    public List<Person> shortestPath(String startName, String endName) {
        return personRepository.shortestPath(startName, endName);
    }

    public List<Person> personListDept(String name) {
        return personRepository.personListDept(name);
    }
}

6. 编写测试类

import com.rpp.bean.Person;
import com.rpp.service.Neo4jPersonService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.List;

@SpringBootApplication
public class Neo4jBootAppMain {
    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(Neo4jBootAppMain.class, args);
        
        Neo4jPersonService personService = applicationContext.getBean("personService", Neo4jPersonService.class);
        Person person = new Person();
        person.setName("testboot");
        person.setMoney(12345.45);
        person.setCharacter("A");
        person.setAge(11);
        Person p1 = personService.save(person);
        System.out.println(p1);
        System.out.println(personService.getAll());
        List<Person> personList = personService.personList(1000);
        System.out.println(personList);
        List<Person> personList2 = personService.shortestPath("王启年", "九品射手燕小乙");
        System.out.println(personList2);
        List<Person> personList3 = personService.personListDept("范闲");
        for (Person pe : personList3) {
            System.out.println(pe);
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值