1、pom檔案
springboot2.0.5,jdk1.8
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
</dependency>
</dependencies>
2、application.yml
spring:
data:
neo4j:
compiler : org.neo4j.ogm.compiler.MultiStatementCypherCompiler
driver : org.neo4j.ogm.drivers.http.driver.HttpDriver
uri : bolt://10.182.xxx.xx:xxxxx
username : neo4japp
password : xxxxxxxx
server:
port: 31020
3、 實體類
目錄結構如下。constant存放需要的全局變量實體,node存放圖資料庫節點實體類,relationship存放節點間的關系實體。
3.1、節點類
節點基類AuditabledNode:
import java.util.Date;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
@NodeEntity(label = "")
public class AuditabledNode {
@Id
@Property(name = "ID")
protected String id;
@Property(name = "CREATED_TIME")
protected Date createdTime;
@Property(name = "CREATED_BY")
protected String createdBy;
@Property(name = "LAST_MODIFIED_TIME")
protected Date lastModifiedTime;
@Property(name = "LAST_MODIFIED_BY")
protected String lastModifiedBy;
//省略getter/setter
}
節點類BusinessSystem、Application:
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
@NodeEntity(label = "T_BUSINESS_SYSTEM")
public class BusinessSystem extends AuditabledNode {
@Property(name = "BUSINESS_CODE")
private String businessCode;
@Property(name = "BUSINESS_NAME")
private String businessName;
@Property(name = "DEV_CONTACTER")
private String devContacter;
@Property(name = "DEV_MOBILE")
private String devMobile;
@Property(name = "TEST_CONTACTER")
private String testContacter;
@Property(name = "TEST_MOBILE")
private String testMobile;
@Property(name = "MAINTAIN_CONTACTER")
private String maintainContacter;
@Property(name = "MAINTAIN_MOBILE")
private String maintainMobile;
@Property(name = "status")
private String status;
@Property(name = "ISTOPSYSTEM")
private Integer istopsystem;
@Property(name = "ISLOGSPIDERED")
private Integer islogspidered;
//省略getter/setter
}
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
@NodeEntity(label = "T_APPLACTION")
public class Application extends AuditabledNode {
@Property(name = "APP_NAME")
private String appName;
@Property(name = "ENAME_SHORT")
private String enameShort;
@Property(name = "BUSINESS_ID")
private String businessId;
@Property(name = "DEV_FTP_PATH")
private String devFtpPath;
@Property(name = "DEV_FTP_TYPE")
private String devFtpType;
@Property(name = "PDR_FTP_PATH")
private String pdrFtpPath;
@Property(name = "MEMO")
private String memo;
@Property(name = "status")
private String status;
//省略getter/setter
}
3.2、關系類
關系基類BaseRelationship:
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.RelationshipEntity;
@RelationshipEntity
public class BaseRelationship {
@Id
@GeneratedValue
private Long id;
@Property(name = "RELATION_NAME")
private String relationName;
//省略getter/setter
}
關系類HaveRelationship:
import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;
import com.cpic.automation.opsgraph.model.node.AuditabledNode;
@RelationshipEntity(type = "have" , value = "have")
public class HaveRelationship <S extends AuditabledNode, E extends AuditabledNode> extends BaseRelationship {
@StartNode
private S startNode;
@EndNode
private E endNode;
@Property(name = "BUSINESSSYSTEM_ID")
private String businesssystemId;
//省略getter/setter
}
常量類記錄關系名稱RelationName:
public interface RelationName {
String BUSINESSSYSTEM_TO_APPLICATION = "BUSINESSSYSTEM_TO_APPLICATION";
String APPLICATION_TO_CLUSTER = "APPLICATION_TO_CLUSTER";
//其他視需添加
}
4、Repository、Service、controller
目錄結構如下:
4.1、BusinessSystem、Application實體Repository
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
import com.cpic.automation.opsgraph.model.node.BusinessSystem;
@Repository
public interface BusinessSystemRepository extends Neo4jRepository<BusinessSystem, String> {
}
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
import com.cpic.automation.opsgraph.model.node.Application;
@Repository
public interface ApplicationRepository extends Neo4jRepository<Application, String> {
}
4.2、HaveRelationship實體Repository
import java.util.List;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
@Repository
public interface HaveRelationshipRepository extends Neo4jRepository<HaveRelationship, Long> {
List<HaveRelationship> findByBusinesssystemId(String businesssystemId);
//查詢一個業務系統下所有have關系及開始、結束節點
@Query("MATCH p=()-[r:have]->() WHERE r.BUSINESSSYSTEM_ID={businesssystemId} RETURN p")
List<HaveRelationship> findAllCascadeByBusinesssystemId(@Param("businesssystemId") String businesssystemId);
//删除一個業務系統下所有have關系及開始、結束節點
@Query("MATCH p=()-[r:have]->() DELETE p")
List<HaveRelationship> removeAllCascadeByBusinesssystemId(@Param("businesssystemId") String businesssystemId);
}
4.3 HaveRelationshipService
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
import com.cpic.automation.opsgraph.repository.relationrepository.HaveRelationshipRepository;
@Service
public class HaveRelationshipService {
private static final Logger logger = LoggerFactory.getLogger(HaveRelationshipService.class);
@Autowired
HaveRelationshipRepository haveRelationshipRepository;
public List<HaveRelationship> findAllCascadeByBusinesssystemId(String businesssystemId) {
return haveRelationshipRepository.findAllCascadeByBusinesssystemId(businesssystemId);
}
public List<HaveRelationship> removeAllCascadeByBusinesssystemId(String businesssystemId) {
return haveRelationshipRepository.removeAllCascadeByBusinesssystemId(businesssystemId);
}
}
4.4 HaveRelationshipController
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
import com.cpic.automation.opsgraph.service.HaveRelationshipService;
@RestController
@RequestMapping("/opsgraph/haveRelationship")
public class HaveRelationshipController {
@Autowired
HaveRelationshipService service;
@GetMapping("/findAllCascadeByBusinesssystemId/{businesssystemId}")
public ResponseEntity<ModelMap> findAllCascadeByBusinesssystemId(@PathVariable String businesssystemId) {
List<HaveRelationship> haveRelationships = service.findAllCascadeByBusinesssystemId(businesssystemId);
return null;
}
@DeleteMapping("/removeAllCascadeByBusinesssystemId/{businesssystemId}") //僅删除了relationship,未删除節點
public ResponseEntity<ModelMap> removeAllCascadeByBusinesssystemId(@PathVariable String businesssystemId) {
List<HaveRelationship> haveRelationships = service.removeAllCascadeByBusinesssystemId(businesssystemId);
return null;
}
}
5、儲存節點及關聯關系測試
首先建立實體類BusinessSystem、Application,用save方法儲存,再建立兩者關聯關系
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.cpic.automation.opsgraph.OpsGraphApplication;
import com.cpic.automation.opsgraph.model.node.Application;
import com.cpic.automation.opsgraph.model.node.BusinessSystem;
import com.cpic.automation.opsgraph.model.relationship.HaveRelationship;
import com.cpic.automation.opsgraph.repository.noderepository.ApplicationRepository;
import com.cpic.automation.opsgraph.repository.noderepository.BusinessSystemRepository;
import com.cpic.automation.opsgraph.repository.relationrepository.HaveRelationshipRepository;
@SpringBootTest(classes = OpsGraphApplication.class)
@RunWith(SpringRunner.class)
public class ResTest {
@Autowired
HaveRelationshipRepository haveRelationshipRepository;
@Autowired
BusinessSystemRepository businessSystemRepository;
@Autowired
ApplicationRepository applicationRepository;
@Test
public void resTest() throws Exception {
BusinessSystem businessSystem = new BusinessSystem();
businessSystem.setId("12345678901234567890");
businessSystem.setBusinessName("自動化運維管理平台");
businessSystemRepository.save(businessSystem);
Application application = new Application();
application.setId("zdh234567");
application.setAppName("自動化主叢集");
applicationRepository.save(application);
Application application2 = new Application();
application2.setId("zdh235678");
application2.setAppName("自動化ci叢集");
applicationRepository.save(application2);
applicationRepository.save(application);
HaveRelationship<BusinessSystem, Application> haveRelationship = new HaveRelationship<>();
// haveRelationship.setId((long) 1234);
haveRelationship.setBusinesssystemId("12345678901234567890");
haveRelationship.setStartNode(businessSystem);
haveRelationship.setEndNode(application);
HaveRelationship<BusinessSystem, Application> haveRelationship2 = new HaveRelationship<>();
// haveRelationship2.setId((long) 1235);
haveRelationship2.setBusinesssystemId("12345678901234567890");
haveRelationship2.setStartNode(businessSystem);
haveRelationship2.setEndNode(application2);
System.out.println(haveRelationship);
System.out.println(haveRelationship2);
haveRelationshipRepository.save(haveRelationship);
haveRelationshipRepository.save(haveRelationship2);
}
}