From e0163cb0c2a058dec7e3c76549d3eedba6d42f00 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Sat, 1 Aug 2020 03:33:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A8=E8=8D=90=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bupt/note/Controller/PaperController.java | 10 ++++++++++ .../com/bupt/note/Repository/PaperRepository.java | 15 ++++++++++++++- .../java/com/bupt/note/NoteApplicationTests.java | 12 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/bupt/note/Controller/PaperController.java b/src/main/java/com/bupt/note/Controller/PaperController.java index 835bacb..897b5e1 100644 --- a/src/main/java/com/bupt/note/Controller/PaperController.java +++ b/src/main/java/com/bupt/note/Controller/PaperController.java @@ -106,6 +106,16 @@ public class PaperController { } + /** + * 论文推荐 + * @return + */ + @GetMapping("recommend") + public ResponseData recommend(){ + List papers=paperRepository.recommend(); + return ResponseDataUtil.buildSuccess(papers); + } + @PostMapping("rating") public ResponseData rating(@RequestBody RatingForm ratingForm,@CookieValue("user") String userName) { if(ratingForm.getPaperId()!=null&&ratingForm.getScore1()!=null&&ratingForm.getScore2()!=null&&ratingForm.getScore3()!=null){ diff --git a/src/main/java/com/bupt/note/Repository/PaperRepository.java b/src/main/java/com/bupt/note/Repository/PaperRepository.java index 58add77..ffab879 100644 --- a/src/main/java/com/bupt/note/Repository/PaperRepository.java +++ b/src/main/java/com/bupt/note/Repository/PaperRepository.java @@ -3,7 +3,20 @@ package com.bupt.note.Repository; import com.bupt.note.Model.Paper; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; -public interface PaperRepository extends JpaRepository, JpaSpecificationExecutor { +import java.util.List; +public interface PaperRepository extends JpaRepository, JpaSpecificationExecutor { + @Query(value = "select *\n" + + "from sys_paper\n" + + "where id in (select a.id\n" + + " from (select p.id, sum(r.score1 + r.score2 + r.score3) as score\n" + + " from sys_paper p,\n" + + " sys_paper_rating r\n" + + " where p.id = r.paper_id\n" + + " group by p.id\n" + + " order by score desc\n" + + " limit 10) a)",nativeQuery = true) + List recommend(); } diff --git a/src/test/java/com/bupt/note/NoteApplicationTests.java b/src/test/java/com/bupt/note/NoteApplicationTests.java index 6512a40..3a48dea 100644 --- a/src/test/java/com/bupt/note/NoteApplicationTests.java +++ b/src/test/java/com/bupt/note/NoteApplicationTests.java @@ -1,5 +1,7 @@ package com.bupt.note; +import com.bupt.note.Model.Paper; +import com.bupt.note.Repository.PaperRepository; import com.bupt.note.service.FileService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,6 +16,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import java.io.IOException; +import java.util.List; @SpringBootTest class NoteApplicationTests { @@ -26,6 +29,9 @@ class NoteApplicationTests { @Autowired private FileService fileService; + @Autowired + private PaperRepository paperRepository; + @BeforeEach public void setUp() { mvc = MockMvcBuilders.webAppContextSetup(context).build(); @@ -45,4 +51,10 @@ class NoteApplicationTests { public void testRead() throws IOException { String a= fileService.getContent("E:\\JetBrains\\IdeaProjects\\back-end code\\target\\classes\\resources\\static\\txt\\1aee7062-58a7-46e0-ba4d-84a9a14f4205.txt"); } + + @Test + public void testPaper(){ + List papers=paperRepository.recommend(); + System.out.println(papers.size()); + } }