1创建一个springboot项目引入neo4j的依赖
<!-- neo4j依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency>
2、配置文件设置连接neo4j的地址
# neo4j配置 spring.data.neo4j.uri= bolt://192.168.0.100:7687 spring.data.neo4j.username=neo4j spring.data.neo4j.password=testpasswd
3、创建person的实体类和接口PersonRepository
package com.example.demo.entity; import org.neo4j.ogm.annotation.GeneratedValue; import org.neo4j.ogm.annotation.Id; import org.neo4j.ogm.annotation.NodeEntity; import org.neo4j.ogm.annotation.Property; import java.io.Serializable; //节点的标签类型 @NodeEntity(label = "person") public class Person implements Serializable { //neo4j自动生成的id @Id @GeneratedValue private Long id; //节点的属性 @Property private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.example.demo.dao; import com.example.demo.entity.Person; import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface PersonRepository extends Neo4jRepository<Person,Long> { @Query("Match (p:person) return p") List<Person> findParentList(); }
4、创建RelatlionShip的关系类和接PersonRelatlionShipRepository
package com.example.demo.entity; import org.neo4j.ogm.annotation.*; import java.io.Serializable; //关系实体 必须有起始节点和结束节点 @RelationshipEntity(type="personRelationShip") public class RelatlionShip implements Serializable { @Id @GeneratedValue private Long id; //起始节点 @StartNode private Person startPerson; //结束节点 @EndNode private Person endPerson; //属性 @Property private String relation; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Person getStartPerson() { return startPerson; } public void setStartPerson(Person startPerson) { this.startPerson = startPerson; } public Person getEndPerson() { return endPerson; } public void setEndPerson(Person endPerson) { this.endPerson = endPerson; } public String getRelation() { return relation; } public void setRelation(String relation) { this.relation = relation; } }
package com.example.demo.dao; import com.example.demo.entity.RelatlionShip; import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.stereotype.Repository; @Repository public interface PersonRelatlionShipRepository extends Neo4jRepository<RelatlionShip,Long> { @Query("match (n:person {name:{0}}), (m:person {name:{2}}) create (n)-[:关系{relation:{1}}]->(m)") void createRelation(String from,String relation,String to); }
5、创建测试类DemoApplicationTests
package com.example.demo;
import com.example.demo.dao.PersonRelatlionShipRepository;
import com.example.demo.dao.PersonRepository;
import com.example.demo.entity.Person;
import com.example.demo.entity.RelatlionShip;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import sun.print.PeekGraphics;
import java.util.List;
import java.util.Optional;
@SpringBootTest
class DemoApplicationTests {
@Autowired
PersonRepository personRepository;
@Autowired
PersonRelatlionShipRepository personRelatlionShipRepository;
@Test
void testFindById() {
//用id查询
Optional<Person> person = personRepository.findById(54L);
System.out.println(person.get().getName());
//orElse函数的作用是如果表达式的结果为空值(null),则返回一个指定的默认值作为替代
person.orElse(null);
}
@Test
void testFindPersonList() {
List<Person> list = personRepository.findParentList();
for(Person person:list){
System.out.println(person.getName());
}
}
@Test
void testDeleteById() {
//用id删除
personRepository.deleteById(2L);
}
@Test
void testCreateEntity() {
//增加节点
Person newPerson = new Person();
newPerson.setName("大侠");
personRepository.save(newPerson);
}
@Test
void testCreateRelation() {
//构建关系
Person father = new Person();
father.setName("爸爸");
personRepository.save(father);
Person child = new Person();
child.setName("child");
personRepository.save(child);
//创建关系
RelatlionShip relatlionShip = new RelatlionShip();
relatlionShip.setStartPerson(father);
relatlionShip.setEndPerson(child);
relatlionShip.setRelation("父子");
personRelatlionShipRepository.save(relatlionShip);
}
@Test
void testCreateRelationCql() {
personRelatlionShipRepository.createRelation("大侠","斩杀","李四");
}
@Test
void testDeleteRelationById() {
//用id删除关系
personRelatlionShipRepository.deleteById(18L);
}
}
6、运行测试方法
<1>清空数据
MATCH (n)
DETACH DELETE n
<2>造数据演示数据
CREATE (p1:person{name:"张三"})
CREATE (p2:person{name:"李四"})
CREATE (p3:person{name:"王五"})
查看数据
MATCH (p:person)
return p
<3>演示根据id查找person的方法
运行结果:
<4>运行自己写的方法,返回所有person对象
运行结果
<5>创建一个person 大侠
运行后在neo4j里查询
<5>调用方法创建对象之间的联系
运行后查看结果:
<5>调用自己写的语句方法,创建关系
运行结果:
标签:集成,springboot,Person,org,person,import,neo4j,public From: https://www.cnblogs.com/yclh/p/18056051