在SpringBoot项目中快速使用Neo4j
本文是在看了https://blog.csdn.net/zt15732625878/article/details/98797467这篇文章后,发现其中有一些内容有遗漏,而对其中缺少的部分进行补充并记录。
1、Pom文件中导入依赖并在yaml文件中配置Neo4j地址
pom文件:
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.12.RELEASE</version>
</parent>
<dependencies>
<!--这里使用spring-data的neo4j-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
配置文件配置:
spring:
data:
neo4j:
uri: bolt://localhost:7687
username: neo4j
password: 123456
2、创建与Neo4j中的节点与关系所对应的实体类
类似于ORM架构,实体类要与Neo4j中的节点、关系一一对应。
这里以公司—供应商的关联关系为例,进行demo书写。
1)节点
// 公司节点
@Data
@Builder
@NodeEntity
public class CompanyGraph {
/**
* id
*/
@Id
@GeneratedValue
private Long id;
/**
* 公司名称
*/
private String name;
}
// 供应商节点
@Data
@Builder
@NodeEntity
public class SupplyGraph {
/**
* id
*/
@Id
@GeneratedValue
private Long id;
/**
* 供应商名称
*/
private String name;
}
2)关联关系
// 供应商与公司的关系
@Data
@Builder
@RelationshipEntity(type = "supply") // 这里的type属性对应关系的“标签”
public class SupplyRelationship {
@Id
@GeneratedValue
private Long id;
/**
* 关系名称
*/
private String indexName;
/**
* company
* @StartNode:关系的开端
*/
@StartNode
private CompanyGraph company;
/**
* supply
* @EndNode:关系的指向
*/
@EndNode
private SupplyGraph supply;
/**
* 采购占比
*/
private String scale;
/**
* 采购金额
*/
private String amount;
}
3、编写用于操作Neo4j的Repository
每一个Repository与实体类相关联
@Component
public interface CompanyGraphRepository extends Neo4jRepository<CompanyGraph, Long> {
}
@Component
public interface SupplyGraphRepository extends Neo4jRepository<SupplyGraph, Long> {
}
@Component
public interface SupplyRelationshipRepository extends Neo4jRepository<SupplyRelationship, Long> {
}
4、编写Service层进行具体操作
@Service
public class NeoService {
@Resource
private SupplyGraphRepository supplyGraphRepository;
@Resource
private CompanyGraphRepository companyGraphRepository;
@Resource
private SupplyRelationshipRepository supplyRelationshipRepository;
public void testSave() {
// supply
String scale = "48%";
String amount = "180000";
String supplyName = "供应商-A";
String companyName = "公司-C";
String indexName = "SUPPLY 关系";
// 构建supplyGraph
SupplyGraph supplyGraph = SupplyGraph.builder().name(supplyName).build();
// 保存操作
supplyGraphRepository.save(supplyGraph);
// company
CompanyGraph companyGraph = CompanyGraph.builder().name(companyName).build();
companyGraphRepository.save(companyGraph);
// relation
SupplyRelationship supplyRelationship = SupplyRelationship.builder()
.indexName(indexName).amount(amount).scale(scale)
.company(companyGraph).supply(supplyGraph).build();
supplyRelationshipRepository.save(supplyRelationship);
System.out.println("全部完成");
}
}
5、在测试类中进行测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class NeoTest {
@Resource
private NeoService neoService;
@Test
public void test01() {
System.out.println("开始测试");
neoService.testSave();
System.out.println("测试完成");
}
}
6、检查图形界面:
通过命令match (a)-[n:supply]-(b) return a,n,b
,可以看到当前图数据库中已经有了对应的数据。