File tree 16 files changed +699
-0
lines changed
test/java/com/xkcoding/neo4j 16 files changed +699
-0
lines changed Original file line number Diff line number Diff line change 1
1
# spring-boot-demo-neo4j
2
+
3
+ > 此 demo 主要演示了 Spring Boot 如何集成Neo4j操作图数据库,实现一个校园人物关系网。
4
+
5
+ ## 注意
6
+
7
+ 作者编写本demo时,Neo4j 版本为 ` 3.5.0 ` ,使用 docker 运行,下面是所有步骤:
8
+
9
+ 1 . 下载镜像:` docker pull neo4j:3.5.0 `
10
+ 2 . 运行容器:` docker run -d -p 7474:7474 -p 7687:7687 --name neo4j-3.5.0 neo4j:3.5.0 `
11
+ 3 . 停止容器:` docker stop neo4j-3.5.0 `
12
+ 4 . 启动容器:` docker start neo4j-3.5.0 `
13
+ 5 . 浏览器 http://localhost:7474/ 访问 neo4j 管理后台,初始账号/密码 neo4j/neo4j,会要求修改初始化密码,我们修改为 neo4j/admin
14
+
15
+
16
+
Original file line number Diff line number Diff line change 38
38
<artifactId >spring-boot-starter-test</artifactId >
39
39
<scope >test</scope >
40
40
</dependency >
41
+
42
+ <dependency >
43
+ <groupId >org.projectlombok</groupId >
44
+ <artifactId >lombok</artifactId >
45
+ <optional >true</optional >
46
+ </dependency >
47
+
48
+ <dependency >
49
+ <groupId >cn.hutool</groupId >
50
+ <artifactId >hutool-all</artifactId >
51
+ </dependency >
52
+
53
+ <dependency >
54
+ <groupId >com.google.guava</groupId >
55
+ <artifactId >guava</artifactId >
56
+ </dependency >
41
57
</dependencies >
42
58
43
59
<build >
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .config ;
2
+
3
+ import cn .hutool .core .util .IdUtil ;
4
+ import org .neo4j .ogm .id .IdStrategy ;
5
+
6
+ /**
7
+ * <p>
8
+ * 自定义主键策略
9
+ * </p>
10
+ *
11
+ * @package: com.xkcoding.neo4j.config
12
+ * @description: 自定义主键策略
13
+ * @author: yangkai.shen
14
+ * @date: Created in 2018-12-24 14:40
15
+ * @copyright: Copyright (c) 2018
16
+ * @version: V1.0
17
+ * @modified: yangkai.shen
18
+ */
19
+ public class CustomIdStrategy implements IdStrategy {
20
+ @ Override
21
+ public Object generateId (Object o ) {
22
+ return IdUtil .fastUUID ();
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .constants ;
2
+
3
+ /**
4
+ * <p>
5
+ * 常量池
6
+ * </p>
7
+ *
8
+ * @package: com.xkcoding.neo4j.constants
9
+ * @description: 常量池
10
+ * @author: yangkai.shen
11
+ * @date: Created in 2018-12-24 14:45
12
+ * @copyright: Copyright (c) 2018
13
+ * @version: V1.0
14
+ * @modified: yangkai.shen
15
+ */
16
+ public interface NeoConsts {
17
+ /**
18
+ * 关系:班级拥有的学生
19
+ */
20
+ String R_STUDENT_OF_CLASS = "R_STUDENT_OF_CLASS" ;
21
+
22
+ /**
23
+ * 关系:班级的班主任
24
+ */
25
+ String R_BOSS_OF_CLASS = "R_BOSS_OF_CLASS" ;
26
+
27
+ /**
28
+ * 关系:课程的老师
29
+ */
30
+ String R_TEACHER_OF_LESSON = "R_TEACHER_OF_LESSON" ;
31
+
32
+ /**
33
+ * 关系:学生选的课
34
+ */
35
+ String R_LESSON_OF_STUDENT = "R_LESSON_OF_STUDENT" ;
36
+
37
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .model ;
2
+
3
+ import com .xkcoding .neo4j .config .CustomIdStrategy ;
4
+ import com .xkcoding .neo4j .constants .NeoConsts ;
5
+ import lombok .*;
6
+ import org .neo4j .ogm .annotation .GeneratedValue ;
7
+ import org .neo4j .ogm .annotation .Id ;
8
+ import org .neo4j .ogm .annotation .NodeEntity ;
9
+ import org .neo4j .ogm .annotation .Relationship ;
10
+
11
+ /**
12
+ * <p>
13
+ * 班级节点
14
+ * </p>
15
+ *
16
+ * @package: com.xkcoding.neo4j.model
17
+ * @description: 班级节点
18
+ * @author: yangkai.shen
19
+ * @date: Created in 2018-12-24 14:44
20
+ * @copyright: Copyright (c) 2018
21
+ * @version: V1.0
22
+ * @modified: yangkai.shen
23
+ */
24
+ @ Data
25
+ @ NoArgsConstructor
26
+ @ RequiredArgsConstructor (staticName = "of" )
27
+ @ AllArgsConstructor
28
+ @ Builder
29
+ @ NodeEntity
30
+ public class Class {
31
+ /**
32
+ * 主键
33
+ */
34
+ @ Id
35
+ @ GeneratedValue (strategy = CustomIdStrategy .class )
36
+ private String id ;
37
+
38
+ /**
39
+ * 班级名称
40
+ */
41
+ @ NonNull
42
+ private String name ;
43
+
44
+ /**
45
+ * 班级的班主任
46
+ */
47
+ @ Relationship (NeoConsts .R_BOSS_OF_CLASS )
48
+ @ NonNull
49
+ private Teacher boss ;
50
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .model ;
2
+
3
+ import com .xkcoding .neo4j .config .CustomIdStrategy ;
4
+ import com .xkcoding .neo4j .constants .NeoConsts ;
5
+ import lombok .*;
6
+ import org .neo4j .ogm .annotation .GeneratedValue ;
7
+ import org .neo4j .ogm .annotation .Id ;
8
+ import org .neo4j .ogm .annotation .NodeEntity ;
9
+ import org .neo4j .ogm .annotation .Relationship ;
10
+
11
+ /**
12
+ * <p>
13
+ * 课程节点
14
+ * </p>
15
+ *
16
+ * @package: com.xkcoding.neo4j.model
17
+ * @description: 课程节点
18
+ * @author: yangkai.shen
19
+ * @date: Created in 2018-12-24 14:55
20
+ * @copyright: Copyright (c) 2018
21
+ * @version: V1.0
22
+ * @modified: yangkai.shen
23
+ */
24
+ @ Data
25
+ @ NoArgsConstructor
26
+ @ RequiredArgsConstructor (staticName = "of" )
27
+ @ AllArgsConstructor
28
+ @ Builder
29
+ @ NodeEntity
30
+ public class Lesson {
31
+ /**
32
+ * 主键,自定义主键策略,使用UUID生成
33
+ */
34
+ @ Id
35
+ @ GeneratedValue (strategy = CustomIdStrategy .class )
36
+ private String id ;
37
+
38
+ /**
39
+ * 课程名称
40
+ */
41
+ @ NonNull
42
+ private String name ;
43
+
44
+ /**
45
+ * 任教老师
46
+ */
47
+ @ Relationship (NeoConsts .R_TEACHER_OF_LESSON )
48
+ @ NonNull
49
+ private Teacher teacher ;
50
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .model ;
2
+
3
+ import com .xkcoding .neo4j .config .CustomIdStrategy ;
4
+ import com .xkcoding .neo4j .constants .NeoConsts ;
5
+ import lombok .*;
6
+ import org .neo4j .ogm .annotation .GeneratedValue ;
7
+ import org .neo4j .ogm .annotation .Id ;
8
+ import org .neo4j .ogm .annotation .NodeEntity ;
9
+ import org .neo4j .ogm .annotation .Relationship ;
10
+
11
+ import java .util .List ;
12
+
13
+ /**
14
+ * <p>
15
+ * 学生节点
16
+ * </p>
17
+ *
18
+ * @package: com.xkcoding.neo4j.model
19
+ * @description: 学生节点
20
+ * @author: yangkai.shen
21
+ * @date: Created in 2018-12-24 14:38
22
+ * @copyright: Copyright (c) 2018
23
+ * @version: V1.0
24
+ * @modified: yangkai.shen
25
+ */
26
+ @ Data
27
+ @ NoArgsConstructor
28
+ @ RequiredArgsConstructor (staticName = "of" )
29
+ @ AllArgsConstructor
30
+ @ Builder
31
+ @ NodeEntity
32
+ public class Student {
33
+ /**
34
+ * 主键,自定义主键策略,使用UUID生成
35
+ */
36
+ @ Id
37
+ @ GeneratedValue (strategy = CustomIdStrategy .class )
38
+ private String id ;
39
+
40
+ /**
41
+ * 学生姓名
42
+ */
43
+ @ NonNull
44
+ private String name ;
45
+
46
+ /**
47
+ * 学生选的所有课程
48
+ */
49
+ @ Relationship (NeoConsts .R_LESSON_OF_STUDENT )
50
+ @ NonNull
51
+ private List <Lesson > lessons ;
52
+
53
+ /**
54
+ * 学生所在班级
55
+ */
56
+ @ Relationship (NeoConsts .R_STUDENT_OF_CLASS )
57
+ @ NonNull
58
+ private Class clazz ;
59
+
60
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .model ;
2
+
3
+ import com .xkcoding .neo4j .config .CustomIdStrategy ;
4
+ import lombok .*;
5
+ import org .neo4j .ogm .annotation .GeneratedValue ;
6
+ import org .neo4j .ogm .annotation .Id ;
7
+ import org .neo4j .ogm .annotation .NodeEntity ;
8
+
9
+ /**
10
+ * <p>
11
+ * 教师节点
12
+ * </p>
13
+ *
14
+ * @package: com.xkcoding.neo4j.model
15
+ * @description: 教师节点
16
+ * @author: yangkai.shen
17
+ * @date: Created in 2018-12-24 14:54
18
+ * @copyright: Copyright (c) 2018
19
+ * @version: V1.0
20
+ * @modified: yangkai.shen
21
+ */
22
+ @ Data
23
+ @ NoArgsConstructor
24
+ @ RequiredArgsConstructor (staticName = "of" )
25
+ @ AllArgsConstructor
26
+ @ Builder
27
+ @ NodeEntity
28
+ public class Teacher {
29
+ /**
30
+ * 主键,自定义主键策略,使用UUID生成
31
+ */
32
+ @ Id
33
+ @ GeneratedValue (strategy = CustomIdStrategy .class )
34
+ private String id ;
35
+
36
+ /**
37
+ * 教师姓名
38
+ */
39
+ @ NonNull
40
+ private String name ;
41
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .payload ;
2
+
3
+ import com .xkcoding .neo4j .model .Student ;
4
+ import lombok .Data ;
5
+ import org .springframework .data .neo4j .annotation .QueryResult ;
6
+
7
+ import java .util .List ;
8
+
9
+ /**
10
+ * <p>
11
+ * 按照课程分组的同学关系
12
+ * </p>
13
+ *
14
+ * @package: com.xkcoding.neo4j.payload
15
+ * @description: 按照课程分组的同学关系
16
+ * @author: yangkai.shen
17
+ * @date: Created in 2018-12-24 19:18
18
+ * @copyright: Copyright (c) 2018
19
+ * @version: V1.0
20
+ * @modified: yangkai.shen
21
+ */
22
+ @ Data
23
+ @ QueryResult
24
+ public class ClassmateInfoGroupByLesson {
25
+ /**
26
+ * 课程名称
27
+ */
28
+ private String lessonName ;
29
+
30
+ /**
31
+ * 学生信息
32
+ */
33
+ private List <Student > students ;
34
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .repository ;
2
+
3
+ import com .xkcoding .neo4j .model .Class ;
4
+ import org .springframework .data .neo4j .repository .Neo4jRepository ;
5
+
6
+ import java .util .Optional ;
7
+
8
+ /**
9
+ * <p>
10
+ * 班级节点Repository
11
+ * </p>
12
+ *
13
+ * @package: com.xkcoding.neo4j.repository
14
+ * @description: 班级节点Repository
15
+ * @author: yangkai.shen
16
+ * @date: Created in 2018-12-24 15:05
17
+ * @copyright: Copyright (c) 2018
18
+ * @version: V1.0
19
+ * @modified: yangkai.shen
20
+ */
21
+ public interface ClassRepository extends Neo4jRepository <Class , String > {
22
+ /**
23
+ * 根据班级名称查询班级信息
24
+ *
25
+ * @param name 班级名称
26
+ * @return 班级信息
27
+ */
28
+ Optional <Class > findByName (String name );
29
+ }
Original file line number Diff line number Diff line change
1
+ package com .xkcoding .neo4j .repository ;
2
+
3
+ import com .xkcoding .neo4j .model .Lesson ;
4
+ import org .springframework .data .neo4j .repository .Neo4jRepository ;
5
+
6
+ /**
7
+ * <p>
8
+ * 课程节点Repository
9
+ * </p>
10
+ *
11
+ * @package: com.xkcoding.neo4j.repository
12
+ * @description: 课程节点Repository
13
+ * @author: yangkai.shen
14
+ * @date: Created in 2018-12-24 15:05
15
+ * @copyright: Copyright (c) 2018
16
+ * @version: V1.0
17
+ * @modified: yangkai.shen
18
+ */
19
+ public interface LessonRepository extends Neo4jRepository <Lesson , String > {
20
+ }
You can’t perform that action at this time.
0 commit comments