|
12 | 12 | 4. 启动容器:`docker start neo4j-3.5.0`
|
13 | 13 | 5. 浏览器 http://localhost:7474/ 访问 neo4j 管理后台,初始账号/密码 neo4j/neo4j,会要求修改初始化密码,我们修改为 neo4j/admin
|
14 | 14 |
|
| 15 | +## pom.xml |
15 | 16 |
|
| 17 | +```xml |
| 18 | +<?xml version="1.0" encoding="UTF-8"?> |
| 19 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 21 | + <modelVersion>4.0.0</modelVersion> |
16 | 22 |
|
| 23 | + <artifactId>spring-boot-demo-neo4j</artifactId> |
| 24 | + <version>1.0.0-SNAPSHOT</version> |
| 25 | + <packaging>jar</packaging> |
| 26 | + |
| 27 | + <name>spring-boot-demo-neo4j</name> |
| 28 | + <description>Demo project for Spring Boot</description> |
| 29 | + |
| 30 | + <parent> |
| 31 | + <groupId>com.xkcoding</groupId> |
| 32 | + <artifactId>spring-boot-demo</artifactId> |
| 33 | + <version>1.0.0-SNAPSHOT</version> |
| 34 | + </parent> |
| 35 | + |
| 36 | + <properties> |
| 37 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 38 | + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
| 39 | + <java.version>1.8</java.version> |
| 40 | + </properties> |
| 41 | + |
| 42 | + <dependencies> |
| 43 | + <dependency> |
| 44 | + <groupId>org.springframework.boot</groupId> |
| 45 | + <artifactId>spring-boot-starter-data-neo4j</artifactId> |
| 46 | + </dependency> |
| 47 | + |
| 48 | + <dependency> |
| 49 | + <groupId>org.springframework.boot</groupId> |
| 50 | + <artifactId>spring-boot-starter-web</artifactId> |
| 51 | + </dependency> |
| 52 | + |
| 53 | + <dependency> |
| 54 | + <groupId>org.springframework.boot</groupId> |
| 55 | + <artifactId>spring-boot-starter-test</artifactId> |
| 56 | + <scope>test</scope> |
| 57 | + </dependency> |
| 58 | + |
| 59 | + <dependency> |
| 60 | + <groupId>org.projectlombok</groupId> |
| 61 | + <artifactId>lombok</artifactId> |
| 62 | + <optional>true</optional> |
| 63 | + </dependency> |
| 64 | + |
| 65 | + <dependency> |
| 66 | + <groupId>cn.hutool</groupId> |
| 67 | + <artifactId>hutool-all</artifactId> |
| 68 | + </dependency> |
| 69 | + |
| 70 | + <dependency> |
| 71 | + <groupId>com.google.guava</groupId> |
| 72 | + <artifactId>guava</artifactId> |
| 73 | + </dependency> |
| 74 | + </dependencies> |
| 75 | + |
| 76 | + <build> |
| 77 | + <finalName>spring-boot-demo-neo4j</finalName> |
| 78 | + <plugins> |
| 79 | + <plugin> |
| 80 | + <groupId>org.springframework.boot</groupId> |
| 81 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 82 | + </plugin> |
| 83 | + </plugins> |
| 84 | + </build> |
| 85 | + |
| 86 | +</project> |
| 87 | +``` |
| 88 | + |
| 89 | +## application.yml |
| 90 | + |
| 91 | +```yaml |
| 92 | +spring: |
| 93 | + data: |
| 94 | + neo4j: |
| 95 | + uri: bolt://localhost |
| 96 | + username: neo4j |
| 97 | + password: admin |
| 98 | + open-in-view: false |
| 99 | +``` |
| 100 | +
|
| 101 | +## CustomIdStrategy.java |
| 102 | +
|
| 103 | +```java |
| 104 | +/** |
| 105 | + * <p> |
| 106 | + * 自定义主键策略 |
| 107 | + * </p> |
| 108 | + * |
| 109 | + * @package: com.xkcoding.neo4j.config |
| 110 | + * @description: 自定义主键策略 |
| 111 | + * @author: yangkai.shen |
| 112 | + * @date: Created in 2018-12-24 14:40 |
| 113 | + * @copyright: Copyright (c) 2018 |
| 114 | + * @version: V1.0 |
| 115 | + * @modified: yangkai.shen |
| 116 | + */ |
| 117 | +public class CustomIdStrategy implements IdStrategy { |
| 118 | + @Override |
| 119 | + public Object generateId(Object o) { |
| 120 | + return IdUtil.fastUUID(); |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## 部分Model代码 |
| 126 | + |
| 127 | +### Student.java |
| 128 | + |
| 129 | +```java |
| 130 | +/** |
| 131 | + * <p> |
| 132 | + * 学生节点 |
| 133 | + * </p> |
| 134 | + * |
| 135 | + * @package: com.xkcoding.neo4j.model |
| 136 | + * @description: 学生节点 |
| 137 | + * @author: yangkai.shen |
| 138 | + * @date: Created in 2018-12-24 14:38 |
| 139 | + * @copyright: Copyright (c) 2018 |
| 140 | + * @version: V1.0 |
| 141 | + * @modified: yangkai.shen |
| 142 | + */ |
| 143 | +@Data |
| 144 | +@NoArgsConstructor |
| 145 | +@RequiredArgsConstructor(staticName = "of") |
| 146 | +@AllArgsConstructor |
| 147 | +@Builder |
| 148 | +@NodeEntity |
| 149 | +public class Student { |
| 150 | + /** |
| 151 | + * 主键,自定义主键策略,使用UUID生成 |
| 152 | + */ |
| 153 | + @Id |
| 154 | + @GeneratedValue(strategy = CustomIdStrategy.class) |
| 155 | + private String id; |
| 156 | + |
| 157 | + /** |
| 158 | + * 学生姓名 |
| 159 | + */ |
| 160 | + @NonNull |
| 161 | + private String name; |
| 162 | + |
| 163 | + /** |
| 164 | + * 学生选的所有课程 |
| 165 | + */ |
| 166 | + @Relationship(NeoConsts.R_LESSON_OF_STUDENT) |
| 167 | + @NonNull |
| 168 | + private List<Lesson> lessons; |
| 169 | + |
| 170 | + /** |
| 171 | + * 学生所在班级 |
| 172 | + */ |
| 173 | + @Relationship(NeoConsts.R_STUDENT_OF_CLASS) |
| 174 | + @NonNull |
| 175 | + private Class clazz; |
| 176 | + |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +## 部分Repository代码 |
| 181 | + |
| 182 | +### StudentRepository.java |
| 183 | + |
| 184 | +```java |
| 185 | +/** |
| 186 | + * <p> |
| 187 | + * 学生节点Repository |
| 188 | + * </p> |
| 189 | + * |
| 190 | + * @package: com.xkcoding.neo4j.repository |
| 191 | + * @description: 学生节点Repository |
| 192 | + * @author: yangkai.shen |
| 193 | + * @date: Created in 2018-12-24 15:05 |
| 194 | + * @copyright: Copyright (c) 2018 |
| 195 | + * @version: V1.0 |
| 196 | + * @modified: yangkai.shen |
| 197 | + */ |
| 198 | +public interface StudentRepository extends Neo4jRepository<Student, String> { |
| 199 | + /** |
| 200 | + * 根据名称查找学生 |
| 201 | + * |
| 202 | + * @param name 姓名 |
| 203 | + * @param depth 深度 |
| 204 | + * @return 学生信息 |
| 205 | + */ |
| 206 | + Optional<Student> findByName(String name, @Depth int depth); |
| 207 | + |
| 208 | + /** |
| 209 | + * 根据班级查询班级人数 |
| 210 | + * |
| 211 | + * @param className 班级名称 |
| 212 | + * @return 班级人数 |
| 213 | + */ |
| 214 | + @Query("MATCH (s:Student)-[r:R_STUDENT_OF_CLASS]->(c:Class{name:{className}}) return count(s)") |
| 215 | + Long countByClassName(@Param("className") String className); |
| 216 | + |
| 217 | + |
| 218 | + /** |
| 219 | + * 查询满足 (学生)-[选课关系]-(课程)-[选课关系]-(学生) 关系的 同学 |
| 220 | + * |
| 221 | + * @return 返回同学关系 |
| 222 | + */ |
| 223 | + @Query("match (s:Student)-[:R_LESSON_OF_STUDENT]->(l:Lesson)<-[:R_LESSON_OF_STUDENT]-(:Student) with l.name as lessonName,collect(distinct s) as students return lessonName,students") |
| 224 | + List<ClassmateInfoGroupByLesson> findByClassmateGroupByLesson(); |
| 225 | + |
| 226 | + /** |
| 227 | + * 查询师生关系,(学生)-[班级学生关系]-(班级)-[班主任关系]-(教师) |
| 228 | + * |
| 229 | + * @return 返回师生关系 |
| 230 | + */ |
| 231 | + @Query("match (s:Student)-[:R_STUDENT_OF_CLASS]->(:Class)-[:R_BOSS_OF_CLASS]->(t:Teacher) with t.name as teacherName,collect(distinct s) as students return teacherName,students") |
| 232 | + List<TeacherStudent> findTeacherStudentByClass(); |
| 233 | + |
| 234 | + /** |
| 235 | + * 查询师生关系,(学生)-[选课关系]-(课程)-[任教老师关系]-(教师) |
| 236 | + * |
| 237 | + * @return 返回师生关系 |
| 238 | + */ |
| 239 | + @Query("match ((s:Student)-[:R_LESSON_OF_STUDENT]->(:Lesson)-[:R_TEACHER_OF_LESSON]->(t:Teacher))with t.name as teacherName,collect(distinct s) as students return teacherName,students") |
| 240 | + List<TeacherStudent> findTeacherStudentByLesson(); |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +## Neo4jTest.java |
| 245 | + |
| 246 | +```java |
| 247 | +/** |
| 248 | + * <p> |
| 249 | + * 测试Neo4j |
| 250 | + * </p> |
| 251 | + * |
| 252 | + * @package: com.xkcoding.neo4j |
| 253 | + * @description: 测试Neo4j |
| 254 | + * @author: yangkai.shen |
| 255 | + * @date: Created in 2018-12-24 15:17 |
| 256 | + * @copyright: Copyright (c) 2018 |
| 257 | + * @version: V1.0 |
| 258 | + * @modified: yangkai.shen |
| 259 | + */ |
| 260 | +@Slf4j |
| 261 | +public class Neo4jTest extends SpringBootDemoNeo4jApplicationTests { |
| 262 | + @Autowired |
| 263 | + private NeoService neoService; |
| 264 | + |
| 265 | + /** |
| 266 | + * 测试保存 |
| 267 | + */ |
| 268 | + @Test |
| 269 | + public void testSave() { |
| 270 | + neoService.initData(); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * 测试删除 |
| 275 | + */ |
| 276 | + @Test |
| 277 | + public void testDelete() { |
| 278 | + neoService.delete(); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * 测试查询 鸣人 学了哪些课程 |
| 283 | + */ |
| 284 | + @Test |
| 285 | + public void testFindLessonsByStudent() { |
| 286 | + // 深度为1,则课程的任教老师的属性为null |
| 287 | + // 深度为2,则会把课程的任教老师的属性赋值 |
| 288 | + List<Lesson> lessons = neoService.findLessonsFromStudent("漩涡鸣人", 2); |
| 289 | + |
| 290 | + lessons.forEach(lesson -> log.info("【lesson】= {}", JSONUtil.toJsonStr(lesson))); |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * 测试查询班级人数 |
| 295 | + */ |
| 296 | + @Test |
| 297 | + public void testCountStudent() { |
| 298 | + Long all = neoService.studentCount(null); |
| 299 | + log.info("【全校人数】= {}", all); |
| 300 | + Long seven = neoService.studentCount("第七班"); |
| 301 | + log.info("【第七班人数】= {}", seven); |
| 302 | + } |
| 303 | + |
| 304 | + /** |
| 305 | + * 测试根据课程查询同学关系 |
| 306 | + */ |
| 307 | + @Test |
| 308 | + public void testFindClassmates() { |
| 309 | + Map<String, List<Student>> classmates = neoService.findClassmatesGroupByLesson(); |
| 310 | + classmates.forEach((k, v) -> log.info("因为一起上了【{}】这门课,成为同学关系的有:{}", k, JSONUtil.toJsonStr(v.stream() |
| 311 | + .map(Student::getName) |
| 312 | + .collect(Collectors.toList())))); |
| 313 | + } |
| 314 | + |
| 315 | + /** |
| 316 | + * 查询所有师生关系,包括班主任/学生,任课老师/学生 |
| 317 | + */ |
| 318 | + @Test |
| 319 | + public void testFindTeacherStudent() { |
| 320 | + Map<String, Set<Student>> teacherStudent = neoService.findTeacherStudent(); |
| 321 | + teacherStudent.forEach((k, v) -> log.info("【{}】教的学生有 {}", k, JSONUtil.toJsonStr(v.stream() |
| 322 | + .map(Student::getName) |
| 323 | + .collect(Collectors.toList())))); |
| 324 | + } |
| 325 | +} |
| 326 | +``` |
| 327 | + |
| 328 | +## 截图 |
| 329 | + |
| 330 | +运行测试类之后,可以通过访问 http://localhost:7474 ,查看neo里所有节点和关系 |
| 331 | + |
| 332 | + |
| 333 | + |
| 334 | + |
| 335 | + |
| 336 | +## 参考 |
| 337 | + |
| 338 | +- spring-data-neo4j 官方文档:https://docs.spring.io/spring-data/neo4j/docs/5.1.2.RELEASE/reference/html/ |
| 339 | +- neo4j 官方文档:https://neo4j.com/docs/getting-started/3.5/ |
0 commit comments