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()); + } }