赞
踩
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
# neo4j配置
spring.data.neo4j.uri= bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456
@NodeEntity:标明是一个节点实体
@RelationshipEntity:标明是一个关系实体
@Id:实体主键
@Property:实体属性
@GeneratedValue:实体属性值自增
@StartNode:开始节点(可以理解为父节点)
@EndNode:结束节点(可以理解为子节点)
@Data
@NodeEntity("Student") //与图数据库进行绑定
public class Person implements Serializable {
@Id
@GeneratedValue
private Long id;
@Property("name")
private String name;
}
Neo4jRepositore里 封装了简单的crud
@Repository
public interface PersonRepository extends Neo4jRepository<Person,Long> {
//此语句是已存在的节点,创建节点之间的关系
@Query("match (n:Student {name:{0}}),(m:Student {name:{2}})" +
"create (n)-[:关系demo{relation:{1}}]->(m)")
void createRelation(String from, String relation, String to);
/**
* 查询某个节点的所有子节点
* @param pId * @return
*/
@Query("Match (p:Student ) -[*]->(s:Student) where id(p)={0} return s")
List<Person> findChildList(Long pId);
}
@Autowired private PersonRepository personRepository; @Test public void testCreate() { /* //查询 Optional<Person> byId = personRepository.findById(353L); byId.orElse(null); */ Person person = new Person(); person.setName("李四"); //创建节点 使用Neo4jRepositore里的方法 personRepository.save(person); } //原生语句进行创建关系 @Test public void createPersonRelation2() { personRepository.createRelation("张三","好友","李四"); } //原生语句 查询某个节点的所有子节点 @Test public void findByid() { List<Person> byId = personRelationRepository.findChildList(352L); System.out.println(byId); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。