Skip to content

Commit ee513b2

Browse files
committed
✨ spring-boot-demo-neo4j 完成
1 parent c4b5e0c commit ee513b2

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 TeacherStudent {
25+
/**
26+
* 教师姓名
27+
*/
28+
private String teacherName;
29+
30+
/**
31+
* 学生信息
32+
*/
33+
private List<Student> students;
34+
}

spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/repository/StudentRepository.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.xkcoding.neo4j.model.Student;
44
import com.xkcoding.neo4j.payload.ClassmateInfoGroupByLesson;
5+
import com.xkcoding.neo4j.payload.TeacherStudent;
56
import org.springframework.data.neo4j.annotation.Depth;
67
import org.springframework.data.neo4j.annotation.Query;
78
import org.springframework.data.neo4j.repository.Neo4jRepository;
@@ -43,6 +44,27 @@ public interface StudentRepository extends Neo4jRepository<Student, String> {
4344
Long countByClassName(@Param("className") String className);
4445

4546

46-
@Query("match (s:Student)-[:R_LESSON_OF_STUDENT]->(l:Lesson),(l:Lesson)<-[:R_LESSON_OF_STUDENT]-(:Student) with l.name as lessonName,collect(distinct s) as students return lessonName,students")
47+
/**
48+
* 查询满足 (学生)-[选课关系]-(课程)-[选课关系]-(学生) 关系的 同学
49+
*
50+
* @return 返回同学关系
51+
*/
52+
@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")
4753
List<ClassmateInfoGroupByLesson> findByClassmateGroupByLesson();
54+
55+
/**
56+
* 查询师生关系,(学生)-[班级学生关系]-(班级)-[班主任关系]-(教师)
57+
*
58+
* @return 返回师生关系
59+
*/
60+
@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")
61+
List<TeacherStudent> findTeacherStudentByClass();
62+
63+
/**
64+
* 查询师生关系,(学生)-[选课关系]-(课程)-[任教老师关系]-(教师)
65+
*
66+
* @return 返回师生关系
67+
*/
68+
@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")
69+
List<TeacherStudent> findTeacherStudentByLesson();
4870
}

spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/service/NeoService.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import cn.hutool.core.util.StrUtil;
44
import com.google.common.collect.Lists;
55
import com.google.common.collect.Maps;
6+
import com.google.common.collect.Sets;
67
import com.xkcoding.neo4j.model.Class;
78
import com.xkcoding.neo4j.model.Lesson;
89
import com.xkcoding.neo4j.model.Student;
910
import com.xkcoding.neo4j.model.Teacher;
1011
import com.xkcoding.neo4j.payload.ClassmateInfoGroupByLesson;
12+
import com.xkcoding.neo4j.payload.TeacherStudent;
1113
import com.xkcoding.neo4j.repository.ClassRepository;
1214
import com.xkcoding.neo4j.repository.LessonRepository;
1315
import com.xkcoding.neo4j.repository.StudentRepository;
@@ -21,7 +23,7 @@
2123

2224
import java.util.List;
2325
import java.util.Map;
24-
import java.util.stream.Collectors;
26+
import java.util.Set;
2527

2628
/**
2729
* <p>
@@ -163,4 +165,23 @@ public Map<String, List<Student>> findClassmatesGroupByLesson() {
163165

164166
return result;
165167
}
168+
169+
/**
170+
* 查询所有师生关系,包括班主任/学生,任课老师/学生
171+
*
172+
* @return 师生关系
173+
*/
174+
public Map<String, Set<Student>> findTeacherStudent() {
175+
List<TeacherStudent> teacherStudentByClass = studentRepo.findTeacherStudentByClass();
176+
List<TeacherStudent> teacherStudentByLesson = studentRepo.findTeacherStudentByLesson();
177+
Map<String, Set<Student>> result = Maps.newHashMap();
178+
179+
teacherStudentByClass.forEach(teacherStudent -> result.put(teacherStudent.getTeacherName(), Sets.newHashSet(teacherStudent
180+
.getStudents())));
181+
182+
teacherStudentByLesson.forEach(teacherStudent -> result.put(teacherStudent.getTeacherName(), Sets.newHashSet(teacherStudent
183+
.getStudents())));
184+
185+
return result;
186+
}
166187
}

spring-boot-demo-neo4j/src/test/java/com/xkcoding/neo4j/Neo4jTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.List;
1212
import java.util.Map;
13+
import java.util.Set;
1314
import java.util.stream.Collectors;
1415

1516
/**
@@ -79,4 +80,15 @@ public void testFindClassmates() {
7980
.map(Student::getName)
8081
.collect(Collectors.toList()))));
8182
}
83+
84+
/**
85+
* 查询所有师生关系,包括班主任/学生,任课老师/学生
86+
*/
87+
@Test
88+
public void testFindTeacherStudent() {
89+
Map<String, Set<Student>> teacherStudent = neoService.findTeacherStudent();
90+
teacherStudent.forEach((k, v) -> log.info("【{}】教的学生有 {}", k, JSONUtil.toJsonStr(v.stream()
91+
.map(Student::getName)
92+
.collect(Collectors.toList()))));
93+
}
8294
}

0 commit comments

Comments
 (0)