Neo4j系列导航:
neo4j安装及简单实践
cypher语法基础
cypher插入语法
cypher插入语法
cypher查询语法
cypher通用语法
cypher函数语法
neo4j索引及调优
neo4j java Driver等更多
1. 简介
本文主要是java使用neo4j driver操作neo4j的模板项目及非常有用的工具类,主要包括:
- 图谱操作的http请求参数及响应参数标准数据结构定义(node,edge,graph等)
- 一次性操作多个node,多个edge或者path(graph)的模板代码工具类
- 使用
事务
一次提交多个cql的使用模板 - 可以支持
连接多个neo4j实例
的方法 - 使用时保证
sql注入
的实现
2.项目架构
3.实现
3.1.pom文件
因为此项目使用的是jdk8
,所以neo4j使用的是3.x
版本,更多版本适配请参照: neo4j安装及简单实践
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/></parent><groupId>com.test</groupId><artifactId>neo4j-template</artifactId><version>1.1.0</version><name>neo4j-template</name><description>neo4j模板服务</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId></dependency><!--neo4j的driver依赖 --><dependency><groupId>org.neo4j.driver</groupId><artifactId>neo4j-java-driver</artifactId><version>4.4.9</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>org.javatuples</groupId><artifactId>javatuples</artifactId><version>1.2</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>
</project>
3.2.controller层
controller怎么写不重要,主要为了保持项目的完整性,使用时可以根据业务需求
开发。
@Api(tags = "图数据库接口")
@Validated
@RestController
public class GraphController {@Autowiredprivate GraphService service;@ApiOperation("添加图谱")@PostMapping("/v1/graph")public Response<String> put(@RequestBody @Valid GraphReqDto dto) {service.post(dto);return BdResponse.success();}@ApiOperation("删除图谱")@PostMapping("/v1/graph/{label}")public Response<String> delete(@PathVariable @NotBlank String label, @RequestBody Map<String, Object> dto) {service.delete(label, dto);return BdResponse.success();}@ApiOperation("查询图谱根据label")@GetMapping("/v1/graph/{label}")public Response<GraphDbMd> queryByLabel(@PathVariable @NotBlank String label) {return Response.success(service.queryByLabel(label));}@ApiOperation("查询节点的入度节点")@GetMapping("/v1/graph-in-vertex/{label}/{id}")public Response<GraphDbMd> queryInVertexById(@PathVariable @NotBlank String label, @PathVariable @NotBlank String id) {return Response.success(service.queryAllInVertexById(label, Long.parseLong(id)));}@ApiOperation("查询节点的出度节点")@GetMapping("/v1/graph-out-vertex/{label}/{id}")public Response<GraphDbMd> queryOutVertexById(@PathVariable @NotBlank String label, @PathVariable @NotBlank String id) {return Response.success(service.