创建一个Spring Boot项目并集成Neo4j图数据库,可以按照以下步骤进行。假设你已经安装了Java、Maven和IDE(如IntelliJ IDEA或Eclipse)。

1. 创建Spring Boot项目

你可以使用Spring Initializr来创建一个新的Spring Boot项目。访问 Spring Initializr并配置以下选项:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 选择最新稳定版本
  • Project Metadata:
  • Group: com.example
  • Artifact: springboot-neo4j-demo
  • Name: springboot-neo4j-demo
  • Description: Demo project for Spring Boot with Neo4j
  • Package name: com.example.springbootneojdemo
  • Packaging: Jar
  • Java Version: 8或更高
  • Dependencies: Spring Data Neo4j

点击“Generate”按钮下载生成的项目压缩包,解压后导入到你的IDE中。

2. 配置Neo4j

application.properties文件中添加Neo4j的配置:

spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=your_password
  • 1.
  • 2.
  • 3.
3. 创建实体类

创建一个简单的实体类来表示图数据库中的节点。例如,创建一个Person类:

package com.example.springbootneo4jdemo.model;

import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.Relationship;

import java.util.HashSet;
import java.util.Set;

@Node("Person")
public class Person {

    @Id
    @GeneratedValue
    private Long id;

    @Property("name")
    private String name;

    @Relationship(type = "FRIEND_OF", direction = Relationship.Direction.OUTGOING)
    private Set<Person> friends = new HashSet<>();

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    // Getters and Setters
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
4. 创建Repository接口

创建一个Repository接口来处理数据库操作:

package com.example.springbootneo4jdemo.repository;

import com.example.springbootneo4jdemo.model.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;

public interface PersonRepository extends Neo4jRepository<Person, Long> {
    Person findByName(String name);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
5. 创建服务类

创建一个服务类来处理业务逻辑:

package com.example.springbootneo4jdemo.service;

import com.example.springbootneo4jdemo.model.Person;
import com.example.springbootneo4jdemo.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonService {

    @Autowired
    private PersonRepository personRepository;

    public Person createPerson(String name) {
        return personRepository.save(new Person(name));
    }

    public Person findPersonByName(String name) {
        return personRepository.findByName(name);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
6. 创建控制器

创建一个控制器来处理HTTP请求:

package com.example.springbootneo4jdemo.controller;

import com.example.springbootneo4jdemo.model.Person;
import com.example.springbootneo4jdemo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/persons")
public class PersonController {

    @Autowired
    private PersonService personService;

    @PostMapping
    public Person createPerson(@RequestParam String name) {
        return personService.createPerson(name);
    }

    @GetMapping("/{name}")
    public Person findPersonByName(@PathVariable String name) {
        return personService.findPersonByName(name);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
7. 运行项目

确保Neo4j服务正在运行,然后在IDE中运行Spring Boot应用程序。你可以通过以下URL测试API:

  • 创建一个Person: POST http://localhost:8080/persons?name=John
  • 查找一个Person: GET http://localhost:8080/persons/John

通过以上步骤,你已经成功创建了一个简单的Spring Boot应用程序,并集成了Neo4j图数据库。你可以根据需要扩展这个示例,添加更多的实体、关系和业务逻辑。