From ee513b2f227ba47bfb7eb86322d82d9f3cb75dce Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Tue, 25 Dec 2018 10:07:50 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-neo4j=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neo4j/payload/TeacherStudent.java | 34 +++++++++++++++++++ .../neo4j/repository/StudentRepository.java | 24 ++++++++++++- .../xkcoding/neo4j/service/NeoService.java | 23 ++++++++++++- .../java/com/xkcoding/neo4j/Neo4jTest.java | 12 +++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/payload/TeacherStudent.java diff --git a/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/payload/TeacherStudent.java b/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/payload/TeacherStudent.java new file mode 100644 index 0000000..57eca84 --- /dev/null +++ b/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/payload/TeacherStudent.java @@ -0,0 +1,34 @@ +package com.xkcoding.neo4j.payload; + +import com.xkcoding.neo4j.model.Student; +import lombok.Data; +import org.springframework.data.neo4j.annotation.QueryResult; + +import java.util.List; + +/** + *

+ * 师生关系 + *

+ * + * @package: com.xkcoding.neo4j.payload + * @description: 师生关系 + * @author: yangkai.shen + * @date: Created in 2018-12-24 19:18 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@QueryResult +public class TeacherStudent { + /** + * 教师姓名 + */ + private String teacherName; + + /** + * 学生信息 + */ + private List students; +} diff --git a/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/repository/StudentRepository.java b/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/repository/StudentRepository.java index fb4dd69..00956d7 100644 --- a/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/repository/StudentRepository.java +++ b/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/repository/StudentRepository.java @@ -2,6 +2,7 @@ package com.xkcoding.neo4j.repository; import com.xkcoding.neo4j.model.Student; import com.xkcoding.neo4j.payload.ClassmateInfoGroupByLesson; +import com.xkcoding.neo4j.payload.TeacherStudent; import org.springframework.data.neo4j.annotation.Depth; import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.repository.Neo4jRepository; @@ -43,6 +44,27 @@ public interface StudentRepository extends Neo4jRepository { Long countByClassName(@Param("className") String className); - @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") + /** + * 查询满足 (学生)-[选课关系]-(课程)-[选课关系]-(学生) 关系的 同学 + * + * @return 返回同学关系 + */ + @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") List findByClassmateGroupByLesson(); + + /** + * 查询师生关系,(学生)-[班级学生关系]-(班级)-[班主任关系]-(教师) + * + * @return 返回师生关系 + */ + @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") + List findTeacherStudentByClass(); + + /** + * 查询师生关系,(学生)-[选课关系]-(课程)-[任教老师关系]-(教师) + * + * @return 返回师生关系 + */ + @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") + List findTeacherStudentByLesson(); } diff --git a/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/service/NeoService.java b/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/service/NeoService.java index 057b800..b0a04f3 100644 --- a/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/service/NeoService.java +++ b/spring-boot-demo-neo4j/src/main/java/com/xkcoding/neo4j/service/NeoService.java @@ -3,11 +3,13 @@ package com.xkcoding.neo4j.service; import cn.hutool.core.util.StrUtil; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.xkcoding.neo4j.model.Class; import com.xkcoding.neo4j.model.Lesson; import com.xkcoding.neo4j.model.Student; import com.xkcoding.neo4j.model.Teacher; import com.xkcoding.neo4j.payload.ClassmateInfoGroupByLesson; +import com.xkcoding.neo4j.payload.TeacherStudent; import com.xkcoding.neo4j.repository.ClassRepository; import com.xkcoding.neo4j.repository.LessonRepository; import com.xkcoding.neo4j.repository.StudentRepository; @@ -21,7 +23,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; +import java.util.Set; /** *

@@ -163,4 +165,23 @@ public class NeoService { return result; } + + /** + * 查询所有师生关系,包括班主任/学生,任课老师/学生 + * + * @return 师生关系 + */ + public Map> findTeacherStudent() { + List teacherStudentByClass = studentRepo.findTeacherStudentByClass(); + List teacherStudentByLesson = studentRepo.findTeacherStudentByLesson(); + Map> result = Maps.newHashMap(); + + teacherStudentByClass.forEach(teacherStudent -> result.put(teacherStudent.getTeacherName(), Sets.newHashSet(teacherStudent + .getStudents()))); + + teacherStudentByLesson.forEach(teacherStudent -> result.put(teacherStudent.getTeacherName(), Sets.newHashSet(teacherStudent + .getStudents()))); + + return result; + } } diff --git a/spring-boot-demo-neo4j/src/test/java/com/xkcoding/neo4j/Neo4jTest.java b/spring-boot-demo-neo4j/src/test/java/com/xkcoding/neo4j/Neo4jTest.java index da2af0d..9f4fed2 100644 --- a/spring-boot-demo-neo4j/src/test/java/com/xkcoding/neo4j/Neo4jTest.java +++ b/spring-boot-demo-neo4j/src/test/java/com/xkcoding/neo4j/Neo4jTest.java @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; /** @@ -79,4 +80,15 @@ public class Neo4jTest extends SpringBootDemoNeo4jApplicationTests { .map(Student::getName) .collect(Collectors.toList())))); } + + /** + * 查询所有师生关系,包括班主任/学生,任课老师/学生 + */ + @Test + public void testFindTeacherStudent() { + Map> teacherStudent = neoService.findTeacherStudent(); + teacherStudent.forEach((k, v) -> log.info("【{}】教的学生有 {}", k, JSONUtil.toJsonStr(v.stream() + .map(Student::getName) + .collect(Collectors.toList())))); + } }