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 +
'}';
}
}
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> {
@Query("match(p:Person) where p.money>{money} return p")
List<Person> personList(@Param("money") double money);
@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:/
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);
}
}
}