@@ -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; | |||||
/** | |||||
* <p> | |||||
* 师生关系 | |||||
* </p> | |||||
* | |||||
* @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<Student> students; | |||||
} |
@@ -2,6 +2,7 @@ package com.xkcoding.neo4j.repository; | |||||
import com.xkcoding.neo4j.model.Student; | import com.xkcoding.neo4j.model.Student; | ||||
import com.xkcoding.neo4j.payload.ClassmateInfoGroupByLesson; | 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.Depth; | ||||
import org.springframework.data.neo4j.annotation.Query; | import org.springframework.data.neo4j.annotation.Query; | ||||
import org.springframework.data.neo4j.repository.Neo4jRepository; | import org.springframework.data.neo4j.repository.Neo4jRepository; | ||||
@@ -43,6 +44,27 @@ public interface StudentRepository extends Neo4jRepository<Student, String> { | |||||
Long countByClassName(@Param("className") String className); | 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<ClassmateInfoGroupByLesson> findByClassmateGroupByLesson(); | List<ClassmateInfoGroupByLesson> 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<TeacherStudent> 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<TeacherStudent> findTeacherStudentByLesson(); | |||||
} | } |
@@ -3,11 +3,13 @@ package com.xkcoding.neo4j.service; | |||||
import cn.hutool.core.util.StrUtil; | import cn.hutool.core.util.StrUtil; | ||||
import com.google.common.collect.Lists; | import com.google.common.collect.Lists; | ||||
import com.google.common.collect.Maps; | import com.google.common.collect.Maps; | ||||
import com.google.common.collect.Sets; | |||||
import com.xkcoding.neo4j.model.Class; | import com.xkcoding.neo4j.model.Class; | ||||
import com.xkcoding.neo4j.model.Lesson; | import com.xkcoding.neo4j.model.Lesson; | ||||
import com.xkcoding.neo4j.model.Student; | import com.xkcoding.neo4j.model.Student; | ||||
import com.xkcoding.neo4j.model.Teacher; | import com.xkcoding.neo4j.model.Teacher; | ||||
import com.xkcoding.neo4j.payload.ClassmateInfoGroupByLesson; | import com.xkcoding.neo4j.payload.ClassmateInfoGroupByLesson; | ||||
import com.xkcoding.neo4j.payload.TeacherStudent; | |||||
import com.xkcoding.neo4j.repository.ClassRepository; | import com.xkcoding.neo4j.repository.ClassRepository; | ||||
import com.xkcoding.neo4j.repository.LessonRepository; | import com.xkcoding.neo4j.repository.LessonRepository; | ||||
import com.xkcoding.neo4j.repository.StudentRepository; | import com.xkcoding.neo4j.repository.StudentRepository; | ||||
@@ -21,7 +23,7 @@ import org.springframework.transaction.annotation.Transactional; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.stream.Collectors; | |||||
import java.util.Set; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
@@ -163,4 +165,23 @@ public class NeoService { | |||||
return result; | return result; | ||||
} | } | ||||
/** | |||||
* 查询所有师生关系,包括班主任/学生,任课老师/学生 | |||||
* | |||||
* @return 师生关系 | |||||
*/ | |||||
public Map<String, Set<Student>> findTeacherStudent() { | |||||
List<TeacherStudent> teacherStudentByClass = studentRepo.findTeacherStudentByClass(); | |||||
List<TeacherStudent> teacherStudentByLesson = studentRepo.findTeacherStudentByLesson(); | |||||
Map<String, Set<Student>> 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; | |||||
} | |||||
} | } |
@@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Set; | |||||
import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||
/** | /** | ||||
@@ -79,4 +80,15 @@ public class Neo4jTest extends SpringBootDemoNeo4jApplicationTests { | |||||
.map(Student::getName) | .map(Student::getName) | ||||
.collect(Collectors.toList())))); | .collect(Collectors.toList())))); | ||||
} | } | ||||
/** | |||||
* 查询所有师生关系,包括班主任/学生,任课老师/学生 | |||||
*/ | |||||
@Test | |||||
public void testFindTeacherStudent() { | |||||
Map<String, Set<Student>> teacherStudent = neoService.findTeacherStudent(); | |||||
teacherStudent.forEach((k, v) -> log.info("【{}】教的学生有 {}", k, JSONUtil.toJsonStr(v.stream() | |||||
.map(Student::getName) | |||||
.collect(Collectors.toList())))); | |||||
} | |||||
} | } |