1.Neo4j簡(jiǎn)介
Neo4j是一個(gè)高性能的,NOSQL圖形數(shù)據(jù)庫(kù),它將結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)在網(wǎng)絡(luò)上而不是表中。它是一個(gè)嵌入式的、基于磁盤的、具備完全的事務(wù)特性的Java持久化引擎,但是它將結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)在網(wǎng)絡(luò)(從數(shù)學(xué)角度叫做圖)上而不是表中。Neo4j也可以被看作是一個(gè)高性能的圖引擎,該引擎具有成熟數(shù)據(jù)庫(kù)的所有特性。程序員工作在一個(gè)面向?qū)ο蟮?、靈活的網(wǎng)絡(luò)結(jié)構(gòu)下而不是嚴(yán)格、靜態(tài)的表中——但是他們可以享受到具備完全的事務(wù)特性、企業(yè)級(jí)的數(shù)據(jù)庫(kù)的所有好處。
Neo4j的官方網(wǎng)站:http://www.neo4j.org
2.安裝Neo4j
網(wǎng)上安裝教程很多,本文是在mac電腦下使用docker+Kitematic安裝的,步驟大致如下:
1.啟動(dòng)docker
2.在Kitematic中搜索Neo4j鏡像并安裝,這里安利一下這個(gè)軟件,安裝一些鏡像非常方便,如下圖:

3.安裝完成后,訪問對(duì)應(yīng)web地址,如下:

3.SpringBoot整合
接下來介紹SpringBoot中如何視同Neo4j。
3.1 添加Neo4j依賴
創(chuàng)建項(xiàng)目,pom文件中引入依賴,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
3.2 配置文件
在配置文件中配置Neo4j相關(guān)配置,如下:
# neo4j配置
spring.data.neo4j.uri= bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=neo4j
3.3 創(chuàng)建對(duì)應(yīng)entity
這里以部門為例,要?jiǎng)?chuàng)建一個(gè)如下的圖:
* CEO
* -設(shè)計(jì)部
* - 設(shè)計(jì)1組
* - 設(shè)計(jì)2組
* -技術(shù)部
* - 前端技術(shù)部
* - 后端技術(shù)部
* - 測(cè)試技術(shù)部
那么這里簡(jiǎn)單創(chuàng)建一個(gè)部門實(shí)體和一個(gè)關(guān)系實(shí)體。
其中部門實(shí)體,如下:
@NodeEntity(label = "dept")
@Data
@Builder
public class Dept {
@Id
@GeneratedValue
private Long id;
@Property(name = "deptName")
private String deptName;
}
關(guān)系實(shí)體如下:
@RelationshipEntity(type = "relationShip")
@Data
@Builder
public class RelationShip {
@Id
@GeneratedValue
private Long id;
@StartNode
private Dept parent;
@EndNode
private Dept child;
}
這里說明一下幾個(gè)注解的意思:
- @NodeEntity:標(biāo)明是一個(gè)節(jié)點(diǎn)實(shí)體
- @RelationshipEntity:標(biāo)明是一個(gè)關(guān)系實(shí)體
- @Id:實(shí)體主鍵
- @Property:實(shí)體屬性
- @GeneratedValue:實(shí)體屬性值自增
- @StartNode:開始節(jié)點(diǎn)(可以理解為父節(jié)點(diǎn))
- @EndNode:結(jié)束節(jié)點(diǎn)(可以理解為子節(jié)點(diǎn))
3.4 repository
由于使用的spring-data操作neo4j,所以實(shí)現(xiàn)邏輯類似,創(chuàng)建接口繼承Neo4jRepository。
DeptRepository如下:
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DeptRepository extends Neo4jRepository<Dept,Long> {
}
RelationShipRepository如下:
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RelationShipRepository extends Neo4jRepository<RelationShip, Long> {
}
3.5 基本使用
這里創(chuàng)建了一些基礎(chǔ)方法,使用方式和spring-data-jpa類似,由于需要構(gòu)建一個(gè)本文3.1所描述的圖,所以創(chuàng)建了一個(gè)create方法來初始化數(shù)據(jù),完整代碼如下:
@RestController
public class TestController {
@Resource
private DeptRepository deptRepository;
@Resource
private RelationShipRepository relationShipRepository;
/**
* CEO
* -設(shè)計(jì)部
* - 設(shè)計(jì)1組
* - 設(shè)計(jì)2組
* -技術(shù)部
* - 前端技術(shù)部
* - 后端技術(shù)部
* - 測(cè)試技術(shù)部
*/
@GetMapping("create")
public void create(){
Dept CEO = Dept.builder().deptName("CEO").build();
Dept dept1 = Dept.builder().deptName("設(shè)計(jì)部").build();
Dept dept11 = Dept.builder().deptName("設(shè)計(jì)1組").build();
Dept dept12 = Dept.builder().deptName("設(shè)計(jì)2組").build();
Dept dept2 = Dept.builder().deptName("技術(shù)部").build();
Dept dept21 = Dept.builder().deptName("前端技術(shù)部").build();
Dept dept22 = Dept.builder().deptName("后端技術(shù)部").build();
Dept dept23 = Dept.builder().deptName("測(cè)試技術(shù)部").build();
List<Dept> depts = new ArrayList<>(Arrays.asList(CEO,dept1,dept11,dept12,dept2,dept21,dept22,dept23));
deptRepository.saveAll(depts);
RelationShip relationShip1 = RelationShip.builder().parent(CEO).child(dept1).build();
RelationShip relationShip2 = RelationShip.builder().parent(CEO).child(dept2).build();
RelationShip relationShip3 = RelationShip.builder().parent(dept1).child(dept11).build();
RelationShip relationShip4 = RelationShip.builder().parent(dept1).child(dept12).build();
RelationShip relationShip5 = RelationShip.builder().parent(dept2).child(dept21).build();
RelationShip relationShip6 = RelationShip.builder().parent(dept2).child(dept22).build();
RelationShip relationShip7 = RelationShip.builder().parent(dept2).child(dept23).build();
List<RelationShip> relationShips = new ArrayList<>(Arrays.asList(relationShip1,relationShip2,relationShip3,relationShip4,relationShip5
,relationShip6,relationShip7));
relationShipRepository.saveAll(relationShips);
}
@GetMapping("get")
public RelationShip get(Long id){
Optional<RelationShip> byId = relationShipRepository.findById(id);
return byId.orElse(null);
}
@GetMapping("deleteRelationShip")
public void deleteRelationShip(Long id){
relationShipRepository.deleteById(id);
}
@GetMapping("deleteDept")
public void deleteDept(Long id){
deptRepository.deleteById(id);
}
@GetMapping("deleteAll")
public void deleteAll(){
deptRepository.deleteAll();
relationShipRepository.deleteAll();
}
}
執(zhí)行create方法初始化數(shù)據(jù),結(jié)果如下圖所示:

其余測(cè)試方法這里就不在演示了,可以自行測(cè)試。
4.Neo4j基本命令
4.1 操作命令簡(jiǎn)介
接下來介紹一下Neo4j的基本操作命令。
- CREATE命令:創(chuàng)建節(jié)點(diǎn)命令
- MATCH命令:查詢命令
- RETURN命令:返回?cái)?shù)據(jù)命令
- DELETE命令:刪除命令,可以用于刪除節(jié)點(diǎn)和關(guān)聯(lián)節(jié)點(diǎn)信息
- REMOVE命令:可以用于刪除標(biāo)簽和屬性
4.2 簡(jiǎn)單練習(xí)
創(chuàng)建命令,可以用來創(chuàng)建節(jié)點(diǎn)和關(guān)系節(jié)點(diǎn),比如我們要在創(chuàng)建一個(gè)部門,秘書部,如下,執(zhí)行如下命令:
CREATE (d:dept {deptName:"秘書部"})
操作后如下圖所示:

目前可以看到,秘書部和其余節(jié)點(diǎn)是沒有關(guān)系的,那么接下來將秘書部與CEO創(chuàng)建關(guān)系,執(zhí)行如下命令:
MATCH (n:dept {deptName:"CEO"}),(m:dept {deptName:"秘書部"}) CREATE (n)-[r:relationShip]->(m) return r;
查看結(jié)果如圖:

可以看到秘書部已經(jīng)掛在了CEO節(jié)點(diǎn)下。
其中從上面就可以看出,CQL語(yǔ)句大致結(jié)構(gòu)如下:
- MATCH RETURN:查詢命中結(jié)果返回;
- MATCH CREATE RETURN:查詢后創(chuàng)建關(guān)系返回;
- MATCH DELETE:查詢命中刪除;
...
5.源碼
源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_neo4j
6.參考
參考地址:
https://baike.baidu.com/item/Neo4j/9952114?fr=aladdin
http://www.neo4j.org