• 128719

    文章

  • 810

    评论

  • 12

    友链

  • 最近新加了换肤功能,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

第一个Springboot Neo4j代码

服了这份高薪指南,涨多少你说了算>>

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);
    }
}

运行结果


695856371Web网页设计师②群 | 喜欢本站的朋友可以收藏本站,或者加入我们大家一起来交流技术!

0条评论

Loading...


发表评论

电子邮件地址不会被公开。 必填项已用*标注

自定义皮肤 主体内容背景
打开支付宝扫码付款购买视频教程
遇到问题联系客服QQ:419400980
注册梁钟霖个人博客