pom

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
</dependency>

配置文件

spring:
  data:
    neo4j:
      uri: bolt://XXX.XXX.XXX.XXX:7687
      username: neo4j
      password: neo4j

Springboot主程序

@SpringBootApplication
@EnableNeo4jRepositories
public class Neo4jtestApplication {

   public static void main(String[] args) {
      SpringApplication.run(Neo4jtestApplication.class, args);
   }

}

实体类

@NodeEntity
@Builder
@Data
public class CompanyGraph {
    @Id
    @GeneratedValue
    private Long id;
    private String fullName;
    private String name;
}
@NodeEntity
@Builder
@Data
public class SupplyGraph {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
}

关系类

@Data
@Builder
@RelationshipEntity(type = "supply")
public class SupplyRelationship {
    @Id
    @GeneratedValue
    private Long id;
    private String indexName;
    @StartNode
    private CompanyGraph company;
    @EndNode
    private SupplyGraph supply;
    private String scale;
    private String amount;
}

dao

public interface CompanyGraphRepository extends Neo4jRepository<CompanyGraph,Long> {
}
public interface SupplyGraphRepository extends Neo4jRepository<SupplyGraph,Long> {
}
public interface SupplyRelationshipRepository extends Neo4jRepository<SupplyRelationship,Long> {
}

service

@Service
public class GraphService {
    @Autowired
    private CompanyGraphRepository companyGraphRepository;
    @Autowired
    private SupplyGraphRepository supplyGraphRepository;
    @Autowired
    private SupplyRelationshipRepository supplyRelationshipRepository;

    @PostConstruct
    public void save() {
        String scale = "47.12";
        String amount = "18474.34";
        String companyName = "ABDC公司";
        String fullName = "大力水手公司";
        CompanyGraph company = CompanyGraph.builder().fullName(fullName).name(companyName).build();
        companyGraphRepository.save(company);
        String supplyName = "RPG公司";
        SupplyGraph supply = SupplyGraph.builder().name(supplyName).build();
        supplyGraphRepository.save(supply);
        SupplyRelationship supplyRelationship = SupplyRelationship.builder().company(company)
                .supply(supply).amount(amount).scale(scale).build();
        supplyRelationshipRepository.save(supplyRelationship);
    }
}

运行结果

第一个Springboot Neo4j代码-LMLPHP

04-25 11:10