当前位置:   article > 正文

neo4j图数据库在springboot中使用_no dependency satisfies type class org.neo4j

no dependency satisfies type class org.neo4j

1、pom文件

springboot2.0.5,jdk1.8

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-neo4j</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.neo4j</groupId>
  8. <artifactId>neo4j-ogm-embedded-driver</artifactId>
  9. <version>3.0.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.alibaba</groupId>
  13. <artifactId>fastjson</artifactId>
  14. <version>1.2.36</version>
  15. </dependency>
  16. </dependencies>

2、application.yml 

  1. spring:
  2. data:
  3. neo4j:
  4. compiler : org.neo4j.ogm.compiler.MultiStatementCypherCompiler
  5. driver : org.neo4j.ogm.drivers.http.driver.HttpDriver
  6. uri : bolt://10.182.xxx.xx:xxxxx
  7. username : neo4japp
  8. password : xxxxxxxx
  9. server:
  10. port: 31020

3、 实体类

目录结构如下。constant存放需要的全局变量实体,node存放图数据库节点实体类,relationship存放节点间的关系实体。

3.1、节点类

节点基类AuditabledNode:

  1. import java.util.Date;
  2. import org.neo4j.ogm.annotation.Id;
  3. import org.neo4j.ogm.annotation.NodeEntity;
  4. import org.neo4j.ogm.annotation.Property;
  5. @NodeEntity(label = "")
  6. public class AuditabledNode {
  7. @Id
  8. @Property(name = "ID")
  9. protected String id;
  10. @Property(name = "CREATED_TIME")
  11. protected Date createdTime;
  12. @Property(name = "CREATED_BY")
  13. protected String createdBy;
  14. @Property(name = "LAST_MODIFIED_TIME")
  15. protected Date lastModifiedTime;
  16. @Property(name = "LAST_MODIFIED_BY")
  17. protected String lastModifiedBy;
  18. //省略getter/setter
  19. }

节点类BusinessSystem、Application:

  1. import org.neo4j.ogm.annotation.NodeEntity;
  2. import org.neo4j.ogm.annotation.Property;
  3. @NodeEntity(label = "T_BUSINESS_SYSTEM")
  4. public class BusinessSystem extends AuditabledNode {
  5. @Property(name = "BUSINESS_CODE")
  6. private String businessCode;
  7. @Property(name = "BUSINESS_NAME")
  8. private String businessName;
  9. @Property(name = "DEV_CONTACTER")
  10. private String devContacter;
  11. @Property(name = "DEV_MOBILE")
  12. private String devMobile;
  13. @Property(name = "TEST_CONTACTER")
  14. private String testContacter;
  15. @Property(name = "TEST_MOBILE")
  16. private String testMobile;
  17. @Property(name = "MAINTAIN_CONTACTER")
  18. private String maintainContacter;
  19. @Property(name = "MAINTAIN_MOBILE")
  20. private String maintainMobile;
  21. @Property(name = "status")
  22. private String status;
  23. @Property(name = "ISTOPSYSTEM")
  24. private Integer istopsystem;
  25. @Property(name = "ISLOGSPIDERED")
  26. private Integer islogspidered;
  27. //省略getter/setter
  28. }
  1. import org.neo4j.ogm.annotation.NodeEntity;
  2. import org.neo4j.ogm.annotation.Property;
  3. @NodeEntity(label = "T_APPLACTION")
  4. public class Application extends AuditabledNode {
  5. @Property(name = "APP_NAME")
  6. private String appName;
  7. @Property(name = "ENAME_SHORT")
  8. private String enameShort;
  9. @Property(name = "BUSINESS_ID")
  10. private String businessId;
  11. @Property(name = "DEV_FTP_PATH")
  12. private String devFtpPath;
  13. @Property(name = "DEV_FTP_TYPE")
  14. private String devFtpType;
  15. @Property(name = "PDR_FTP_PATH")
  16. private String pdrFtpPath;
  17. @Property(name = "MEMO")
  18. private String memo;
  19. @Property(name = "status")
  20. private String status;
  21. //省略getter/setter
  22. }

3.2、关系类

关系基类BaseRelationship:

  1. import org.neo4j.ogm.annotation.GeneratedValue;
  2. import org.neo4j.ogm.annotation.Id;
  3. import org.neo4j.ogm.annotation.Property;
  4. import org.neo4j.ogm.annotation.RelationshipEntity;
  5. @RelationshipEntity
  6. public class BaseRelationship {
  7. @Id
  8. @GeneratedValue
  9. private Long id;
  10. @Property(name = "RELATION_NAME")
  11. private String relationName;
  12. //省略getter/setter
  13. }

关系类HaveRelationship:

  1. import org.neo4j.ogm.annotation.EndNode;
  2. import org.neo4j.ogm.annotation.Property;
  3. import org.neo4j.ogm.annotation.RelationshipEntity;
  4. import org.neo4j.ogm.annotation.StartNode;
  5. import com.cpic.automation.opsgraph.model.node.AuditabledNode;
  6. @RelationshipEntity(type = "have" , value = "have")
  7. public class HaveRelationship <S extends AuditabledNode, E extends AuditabledNode> extends BaseRelationship {
  8. @StartNode
  9. private S startNode;
  10. @EndNode
  11. private E endNode;
  12. @Property(name = "BUSINESSSYSTEM_ID")
  13. private String businesssystemId;
  14. //省略getter/setter
  15. }

常量类记录关系名称RelationName:

  1. public interface RelationName {
  2. String BUSINESSSYSTEM_TO_APPLICATION = "BUSINESSSYSTEM_TO_APPLICATION";
  3. String APPLICATION_TO_CLUSTER = "APPLICATION_TO_CLUSTER";
  4. //其他视需添加
  5. }

4、Repository、Service、controller

目录结构如下:

4.1、BusinessSystem、Application实体Repository

  1. import org.springframework.data.neo4j.repository.Neo4jRepository;
  2. import org.springframework.stereotype.Repository;
  3. import com.cpic.automation.opsgraph.model.node.BusinessSystem;
  4. @Repository
  5. public interface BusinessSystemRepository extends Neo4jRepository<BusinessSystem, String> {
  6. }

 

  1. import org.springframework.data.neo4j.repository.Neo4jRepository;
  2. import org.springframework.stereotype.Repository;
  3. import com.cpic.automation.opsgraph.model.node.Application;
  4. @Repository
  5. public interface ApplicationRepository extends Neo4jRepository<Application, String> {
  6. }

4.2、HaveRelationship实体Repository

  1. import java.util.List;
  2. import org.springframework.data.neo4j.annotation.Query;
  3. import org.springframework.data.neo4j.repository.Neo4jRepository;
  4. import org.springframework.data.repository.query.Param;
  5. import org.springframework.stereotype.Repository;
  6. import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
  7. @Repository
  8. public interface HaveRelationshipRepository extends Neo4jRepository<HaveRelationship, Long> {
  9. List<HaveRelationship> findByBusinesssystemId(String businesssystemId);
  10. //查询一个业务系统下所有have关系及开始、结束节点
  11. @Query("MATCH p=()-[r:have]->() WHERE r.BUSINESSSYSTEM_ID={businesssystemId} RETURN p")
  12. List<HaveRelationship> findAllCascadeByBusinesssystemId(@Param("businesssystemId") String businesssystemId);
  13. //删除一个业务系统下所有have关系及开始、结束节点
  14. @Query("MATCH p=()-[r:have]->() DELETE p")
  15. List<HaveRelationship> removeAllCascadeByBusinesssystemId(@Param("businesssystemId") String businesssystemId);
  16. }

4.3 HaveRelationshipService

  1. import java.util.List;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
  7. import com.cpic.automation.opsgraph.repository.relationrepository.HaveRelationshipRepository;
  8. @Service
  9. public class HaveRelationshipService {
  10. private static final Logger logger = LoggerFactory.getLogger(HaveRelationshipService.class);
  11. @Autowired
  12. HaveRelationshipRepository haveRelationshipRepository;
  13. public List<HaveRelationship> findAllCascadeByBusinesssystemId(String businesssystemId) {
  14. return haveRelationshipRepository.findAllCascadeByBusinesssystemId(businesssystemId);
  15. }
  16. public List<HaveRelationship> removeAllCascadeByBusinesssystemId(String businesssystemId) {
  17. return haveRelationshipRepository.removeAllCascadeByBusinesssystemId(businesssystemId);
  18. }
  19. }

4.4 HaveRelationshipController

  1. import java.util.List;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.DeleteMapping;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
  11. import com.cpic.automation.opsgraph.service.HaveRelationshipService;
  12. @RestController
  13. @RequestMapping("/opsgraph/haveRelationship")
  14. public class HaveRelationshipController {
  15. @Autowired
  16. HaveRelationshipService service;
  17. @GetMapping("/findAllCascadeByBusinesssystemId/{businesssystemId}")
  18. public ResponseEntity<ModelMap> findAllCascadeByBusinesssystemId(@PathVariable String businesssystemId) {
  19. List<HaveRelationship> haveRelationships = service.findAllCascadeByBusinesssystemId(businesssystemId);
  20. return null;
  21. }
  22. @DeleteMapping("/removeAllCascadeByBusinesssystemId/{businesssystemId}") //仅删除了relationship,未删除节点
  23. public ResponseEntity<ModelMap> removeAllCascadeByBusinesssystemId(@PathVariable String businesssystemId) {
  24. List<HaveRelationship> haveRelationships = service.removeAllCascadeByBusinesssystemId(businesssystemId);
  25. return null;
  26. }
  27. }

5、保存节点及关联关系测试

首先新建实体类BusinessSystem、Application,用save方法保存,再新建两者关联关系

  1. import org.junit.Test;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.test.context.junit4.SpringRunner;
  6. import com.cpic.automation.opsgraph.OpsGraphApplication;
  7. import com.cpic.automation.opsgraph.model.node.Application;
  8. import com.cpic.automation.opsgraph.model.node.BusinessSystem;
  9. import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
  10. import com.cpic.automation.opsgraph.repository.noderepository.ApplicationRepository;
  11. import com.cpic.automation.opsgraph.repository.noderepository.BusinessSystemRepository;
  12. import com.cpic.automation.opsgraph.repository.relationrepository.HaveRelationshipRepository;
  13. @SpringBootTest(classes = OpsGraphApplication.class)
  14. @RunWith(SpringRunner.class)
  15. public class ResTest {
  16. @Autowired
  17. HaveRelationshipRepository haveRelationshipRepository;
  18. @Autowired
  19. BusinessSystemRepository businessSystemRepository;
  20. @Autowired
  21. ApplicationRepository applicationRepository;
  22. @Test
  23. public void resTest() throws Exception {
  24. BusinessSystem businessSystem = new BusinessSystem();
  25. businessSystem.setId("12345678901234567890");
  26. businessSystem.setBusinessName("自动化运维管理平台");
  27. businessSystemRepository.save(businessSystem);
  28. Application application = new Application();
  29. application.setId("zdh234567");
  30. application.setAppName("自动化主集群");
  31. applicationRepository.save(application);
  32. Application application2 = new Application();
  33. application2.setId("zdh235678");
  34. application2.setAppName("自动化ci集群");
  35. applicationRepository.save(application2);
  36. applicationRepository.save(application);
  37. HaveRelationship<BusinessSystem, Application> haveRelationship = new HaveRelationship<>();
  38. // haveRelationship.setId((long) 1234);
  39. haveRelationship.setBusinesssystemId("12345678901234567890");
  40. haveRelationship.setStartNode(businessSystem);
  41. haveRelationship.setEndNode(application);
  42. HaveRelationship<BusinessSystem, Application> haveRelationship2 = new HaveRelationship<>();
  43. // haveRelationship2.setId((long) 1235);
  44. haveRelationship2.setBusinesssystemId("12345678901234567890");
  45. haveRelationship2.setStartNode(businessSystem);
  46. haveRelationship2.setEndNode(application2);
  47. System.out.println(haveRelationship);
  48. System.out.println(haveRelationship2);
  49. haveRelationshipRepository.save(haveRelationship);
  50. haveRelationshipRepository.save(haveRelationship2);
  51. }
  52. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/727636
推荐阅读
相关标签
  

闽ICP备14008679号