Neo4j开发

原生API

待补充。。。

SpringBoot整合Neo4j

依赖


    org.springframework.boot
    spring-boot-starter-data-Neo4j

配置

spring:
  data:
    neo4j:
      uri: bolt://127.0.0.1:7687
      username: neo4j
      password: neo4j

类似于jpa模式,同样是构建 model 层、repository 层,然后在此基础上操作自定义或模板方法。

实体类

节点

@Data
@NodeEntity(label = "Person")
public class Node {
    @Id
    @GeneratedValue
    private Long id;
 
    @Property(name = "name")
    private String name;
 
    @Property(name = "title")
    private String title;
}

关联关系

@Data
@RelationshipEntity(type = "DOWNSTREAM_OF")
public class DownstreamOfRelationShip {
    @Id
    @GeneratedValue
    private Long id;

    @StartNode
    private Person startNode;

    @EndNode
    private Person endNode;

    @Property("relation_ship_id")
    private String relationShipId;
}

 持久层

@Repository
public interface NodeRepository extends Neo4jRepository {
    @Query("MATCH p=(n:Person) RETURN p")
    List selectAll();
 
    @Query("MATCH(p:Person{name:{name}}) return p")
    Node findByName(String name);
}

使用

@Slf4j
@Service
public class NodeServiceImpl implements NodeService {

    @Autowired
    private NodeRepository nodeRepository;


    @Override
    public  Node addNode(Node node) {
        Node node = nodeRepository.save(node);
        return node ;
    }

    public Node findNode(String name) {
        Node node = nodeRepository.findByName(name);
        return node;
    }
}

你可能感兴趣的:(数据库,spring,boot,neo4j,java)