1.引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency>
说明:这里引入neo4j的版本跟spring框架的版本有关系。需要注意不同的版本在neo4j的配置上可能存在差异,下面的代码书写上也存在差异。
这里使用的是:
2.配置文件添加配置
spring: data: neo4j: uri: bolt://localhost:7687 username: neo4j password: 123456
3.通过java实体创建节点代码
package com.xkj.org.dao;
import com.xkj.org.entity.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {
}
package com.xkj.org.entity;
import lombok.Data;
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")
@Data
public class Person implements Serializable{
@Id
@GeneratedValue
private Long id;
@Property
private String name;
}
package com.xkj.org;
import com.xkj.org.dao.PersonRepository;
import com.xkj.org.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class Neo4jApplication {
@Autowired
private PersonRepository repository;
@Test
public void test() {
List<Person> all = repository.findAll();
System.out.println(all);
}
}
4.通过java实体创建关系代码
package com.xkj.org.dao;
import com.xkj.org.entity.PersonRelationShip;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Component;
@Component
public interface PersonRelationShipRepository extends Neo4jRepository<PersonRelationShip, Long>{
}
package com.xkj.org.entity;
import lombok.Data;
import org.neo4j.ogm.annotation.*;
import java.io.Serializable;
@Data
@RelationshipEntity(type = "personRelationShip")
public class PersonRelationShip implements Serializable {
@Id
@GeneratedValue
private Long id;
@StartNode
private Person parent;
@EndNode
private Person child;
@Property
private String relation;
}
@Test
public void test2() {
Person p1 = personRepository.findById(70L).orElse(null);
Person p2 = personRepository.findById(66L).orElse(null);
PersonRelationShip personRelationShip = new PersonRelationShip();
personRelationShip.setParent(p1);
personRelationShip.setChild(p2);
personRelationShip.setRelation("夫妻");
personRelationShipRepository.save(personRelationShip);
}
5.直接通过cql语句创建关系代码
package com.xkj.org.dao;
import com.xkj.org.entity.Person;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long>{
@Query("match (n:person{name:{0}}), (m:person{name:{2}})" +
"create (n)-[:人际关系{relation:{1}}]->(m)")
void createRelation(String from, String relation, String to);
}
@Test
public void test3() {
personRepository.createRelation("小姐姐", "暗恋", "小哥哥");
}