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