第一个Springboot Neo4j代码

2020-10-28 14:19:55 浏览数 (1)

pom

代码语言:javascript复制
<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>

配置文件

代码语言:javascript复制
spring:
  data:
    neo4j:
      uri: bolt://XXX.XXX.XXX.XXX:7687
      username: neo4j
      password: neo4j

Springboot主程序

代码语言:javascript复制
@SpringBootApplication
@EnableNeo4jRepositories
public class Neo4jtestApplication {

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

}

实体类

代码语言:javascript复制
@NodeEntity
@Builder
@Data
public class CompanyGraph {
    @Id
    @GeneratedValue
    private Long id;
    private String fullName;
    private String name;
}
代码语言:javascript复制
@NodeEntity
@Builder
@Data
public class SupplyGraph {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
}

关系类

代码语言:javascript复制
@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

代码语言:javascript复制
public interface CompanyGraphRepository extends Neo4jRepository<CompanyGraph,Long> {
}
代码语言:javascript复制
public interface SupplyGraphRepository extends Neo4jRepository<SupplyGraph,Long> {
}
代码语言:javascript复制
public interface SupplyRelationshipRepository extends Neo4jRepository<SupplyRelationship,Long> {
}

service

代码语言:javascript复制
@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);
    }
}

运行结果

0 人点赞