From ccb1cb36820e43677007a3c6f1b7d019c2a764fc Mon Sep 17 00:00:00 2001 From: chuyan <1047381936@qq.com> Date: Tue, 8 Sep 2020 21:36:22 +0800 Subject: [PATCH] =?UTF-8?q?student=E7=9B=B8=E5=85=B3repository=E5=88=9D?= =?UTF-8?q?=E6=AD=A5=E6=9E=84=E5=BB=BA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ase/controller/TestController.java | 37 +++++++++++++++++++ .../codesdream/ase/model/student/Comment.java | 4 ++ .../codesdream/ase/model/student/Honor.java | 2 + .../codesdream/ase/model/student/Student.java | 3 -- .../repository/student/CommentRepository.java | 15 ++++++++ .../repository/student/CourseRepository.java | 9 +++++ .../repository/student/HonorRepository.java | 13 +++++++ .../student/NotificationRepository.java | 12 ++++++ .../repository/student/StudentRepository.java | 18 +++++++++ .../java/com/codesdream/ase/test/JpaTest.java | 35 ++++++++++++++++++ 10 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/codesdream/ase/controller/TestController.java create mode 100644 src/main/java/com/codesdream/ase/repository/student/CommentRepository.java create mode 100644 src/main/java/com/codesdream/ase/repository/student/CourseRepository.java create mode 100644 src/main/java/com/codesdream/ase/repository/student/HonorRepository.java create mode 100644 src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java create mode 100644 src/main/java/com/codesdream/ase/repository/student/StudentRepository.java create mode 100644 src/test/java/com/codesdream/ase/test/JpaTest.java diff --git a/src/main/java/com/codesdream/ase/controller/TestController.java b/src/main/java/com/codesdream/ase/controller/TestController.java new file mode 100644 index 0000000..de39213 --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/TestController.java @@ -0,0 +1,37 @@ +package com.codesdream.ase.controller; + +import com.codesdream.ase.model.student.Comment; +import com.codesdream.ase.repository.student.CommentRepository; +import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.List; + +@RestController("test") +public class TestController { + + @Resource + CommentRepository commentRepository; + + @RequestMapping + @ResponseStatus(HttpStatus.OK) + public boolean test() throws InterruptedException { + + int userId = 1; + for (int i = 0;i < 10; i++){ + Comment comment = new Comment(); + comment.setUserId(userId); + comment.setContext(new Integer(i).toString()); + commentRepository.save(comment); + Thread.sleep(20); + } + Thread.sleep(1000); + List comments = commentRepository.findByUserId(new Integer(userId).toString(), + Sort.by(Sort.Direction.ASC, "date")); + return true; + } +} diff --git a/src/main/java/com/codesdream/ase/model/student/Comment.java b/src/main/java/com/codesdream/ase/model/student/Comment.java index 44d446e..ee0bfc3 100644 --- a/src/main/java/com/codesdream/ase/model/student/Comment.java +++ b/src/main/java/com/codesdream/ase/model/student/Comment.java @@ -3,6 +3,7 @@ package com.codesdream.ase.model.student; import lombok.Data; import javax.persistence.*; +import java.util.Date; @Entity @Table @@ -17,4 +18,7 @@ public class Comment { // 评论内容 String context; + + // 评论时间 + Date date = new Date(); } diff --git a/src/main/java/com/codesdream/ase/model/student/Honor.java b/src/main/java/com/codesdream/ase/model/student/Honor.java index 0e03cd5..fbf1fd5 100644 --- a/src/main/java/com/codesdream/ase/model/student/Honor.java +++ b/src/main/java/com/codesdream/ase/model/student/Honor.java @@ -15,6 +15,8 @@ public class Honor { @GeneratedValue(strategy = GenerationType.AUTO) int id; + String studentId; + // 荣誉描述 String description; diff --git a/src/main/java/com/codesdream/ase/model/student/Student.java b/src/main/java/com/codesdream/ase/model/student/Student.java index 770792a..ddf5385 100644 --- a/src/main/java/com/codesdream/ase/model/student/Student.java +++ b/src/main/java/com/codesdream/ase/model/student/Student.java @@ -5,12 +5,9 @@ import com.codesdream.ase.model.permission.User; import lombok.Data; import lombok.EqualsAndHashCode; -import javax.annotation.Generated; import javax.persistence.*; -import java.util.List; @Entity -@Table @Data @EqualsAndHashCode(callSuper = false) public class Student extends User { diff --git a/src/main/java/com/codesdream/ase/repository/student/CommentRepository.java b/src/main/java/com/codesdream/ase/repository/student/CommentRepository.java new file mode 100644 index 0000000..88f45c6 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/student/CommentRepository.java @@ -0,0 +1,15 @@ +package com.codesdream.ase.repository.student; + +import com.codesdream.ase.model.student.Comment; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface CommentRepository extends JpaRepository { + List findByUserId(String id, Sort sort); + + List findByUserId(String id); +} diff --git a/src/main/java/com/codesdream/ase/repository/student/CourseRepository.java b/src/main/java/com/codesdream/ase/repository/student/CourseRepository.java new file mode 100644 index 0000000..763f372 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/student/CourseRepository.java @@ -0,0 +1,9 @@ +package com.codesdream.ase.repository.student; + +import com.codesdream.ase.model.student.Course; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CourseRepository extends JpaRepository { +} diff --git a/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java b/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java new file mode 100644 index 0000000..68dfa79 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java @@ -0,0 +1,13 @@ +package com.codesdream.ase.repository.student; + +import com.codesdream.ase.model.student.Honor; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface HonorRepository extends JpaRepository { + List findByStudentId(String studentId, Sort sort); +} diff --git a/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java b/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java new file mode 100644 index 0000000..780ed96 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java @@ -0,0 +1,12 @@ +package com.codesdream.ase.repository.student; + +import com.codesdream.ase.model.student.Notification; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface NotificationRepository extends JpaRepository { + Optional findByTitle(String title); +} diff --git a/src/main/java/com/codesdream/ase/repository/student/StudentRepository.java b/src/main/java/com/codesdream/ase/repository/student/StudentRepository.java new file mode 100644 index 0000000..9d7d568 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/student/StudentRepository.java @@ -0,0 +1,18 @@ +package com.codesdream.ase.repository.student; + +import com.codesdream.ase.model.student.Student; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface StudentRepository extends JpaRepository { + Optional findByStudentId(String studentId); + + List findByName(String name, Sort sort); + + List findByName(String name); +} diff --git a/src/test/java/com/codesdream/ase/test/JpaTest.java b/src/test/java/com/codesdream/ase/test/JpaTest.java new file mode 100644 index 0000000..aeb6edd --- /dev/null +++ b/src/test/java/com/codesdream/ase/test/JpaTest.java @@ -0,0 +1,35 @@ +package com.codesdream.ase.test; + +import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.model.student.Comment; +import com.codesdream.ase.repository.student.CommentRepository; +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Sort; + +import javax.annotation.Resource; +import java.util.List; + +@SpringBootTest +public class JpaTest { + + @Resource + ASESpringUtil aseSpringUtil; + + @Test + public void test() throws InterruptedException { + + CommentRepository commentRepository = aseSpringUtil.getBean(CommentRepository.class); + int userId = 1; + for (int i = 0;i < 10; i++){ + Comment comment = new Comment(); + comment.setUserId(userId); + comment.setContext(new Integer(i).toString()); + commentRepository.save(comment); + Thread.sleep(20); + } + Thread.sleep(1000); + List comments = commentRepository.findByUserId(new Integer(userId).toString(), + Sort.by(Sort.Direction.ASC, "date")); + } +}