From 493605c56cf55e4ca9ad761f63a0f24127db87e8 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Fri, 31 Jul 2020 04:57:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BA=E6=96=87=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/db_table.md | 1 + .../bupt/note/Controller/PaperController.java | 49 ++++++++++++- src/main/java/com/bupt/note/Model/Paper.java | 10 +++ .../java/com/bupt/note/NoteApplication.java | 5 -- .../bupt/note/Properties/FileProperties.java | 17 ----- .../note/Repository/PapaerRepository.java | 3 +- src/main/java/com/bupt/note/dto/Page.java | 25 +++++++ .../java/com/bupt/note/dto/QueryPaper.java | 70 +++++++++++++++++++ .../java/com/bupt/note/dto/UploadPaper.java | 10 +++ 9 files changed, 166 insertions(+), 24 deletions(-) delete mode 100644 src/main/java/com/bupt/note/Properties/FileProperties.java create mode 100644 src/main/java/com/bupt/note/dto/Page.java create mode 100644 src/main/java/com/bupt/note/dto/QueryPaper.java diff --git a/doc/db_table.md b/doc/db_table.md index e583820..d5af503 100644 --- a/doc/db_table.md +++ b/doc/db_table.md @@ -18,6 +18,7 @@ | 列名 | 数据类型 | 约束条件 | 含义 | | ---- | -------- | -------- | ---- | | id | int | primary key; identity | 论文id | +| title | varchar | not null | 论文标题 | | type | varchar | not null | 论文类型 | | author | varchar | not null | 论文作者 | | profession | varchar | not null | 学科专业 | diff --git a/src/main/java/com/bupt/note/Controller/PaperController.java b/src/main/java/com/bupt/note/Controller/PaperController.java index c1f2832..b5ac721 100644 --- a/src/main/java/com/bupt/note/Controller/PaperController.java +++ b/src/main/java/com/bupt/note/Controller/PaperController.java @@ -4,11 +4,19 @@ import com.bupt.note.Model.Paper; import com.bupt.note.Repository.PapaerRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; +import com.bupt.note.dto.Page; +import com.bupt.note.dto.QueryPaper; import com.bupt.note.dto.UploadPaper; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.persistence.criteria.Predicate; +import java.util.ArrayList; +import java.util.List; + /** * 论文管理接口 */ @@ -16,12 +24,14 @@ import org.springframework.web.bind.annotation.*; @RequestMapping(value = "/v1/api/paper") public class PaperController { + private Logger logger= LoggerFactory.getLogger(PaperController.class); + @Autowired private PapaerRepository papaerRepository; @PostMapping("upload") public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { - if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getType(), + if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(),uploadPaper.getType(), uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){ Paper paper = uploadPaper.toPaper(); paper.setUserName(username); @@ -36,5 +46,42 @@ public class PaperController { } } + @GetMapping("list") + public ResponseData list(QueryPaper queryPaper){ + + try { + Page list=new Page<>(); + list.setTotal(papaerRepository.count()); + List papers = papaerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { + List predicates = new ArrayList<>(); + if(StringUtils.isNotEmpty(queryPaper.getTitle())){ + predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%")); + } + if(StringUtils.isNotEmpty(queryPaper.getAuthor())){ + predicates.add(criteriaBuilder.like(root.get("author"),"%"+queryPaper.getAuthor()+"%")); + } + if(StringUtils.isNotEmpty(queryPaper.getTag())){ + predicates.add(criteriaBuilder.like(root.get("tag"),"%"+queryPaper.getTag()+"%")); + } + if(StringUtils.isNotEmpty(queryPaper.getProfession())){ + predicates.add(criteriaBuilder.like(root.get("profession"),"%"+queryPaper.getTag()+"%")); + } + if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()==null){ + predicates.add(criteriaBuilder.ge(root.get("year"),queryPaper.getStartYear())); + }else if(queryPaper.getStartYear()==null&&queryPaper.getEndYear()!=null){ + predicates.add(criteriaBuilder.le(root.get("year"),queryPaper.getEndYear())); + }else if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()!=null){ + predicates.add(criteriaBuilder.between(root.get("year"),queryPaper.getStartYear(),queryPaper.getEndYear())); + } + return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); + }); + list.setData(papers); + return ResponseDataUtil.buildSuccess(list); + } catch (Exception e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + return ResponseDataUtil.buildError(); + } + } } diff --git a/src/main/java/com/bupt/note/Model/Paper.java b/src/main/java/com/bupt/note/Model/Paper.java index 1a2fc53..6e1765d 100644 --- a/src/main/java/com/bupt/note/Model/Paper.java +++ b/src/main/java/com/bupt/note/Model/Paper.java @@ -10,6 +10,7 @@ import javax.persistence.*; public class Paper { private Long id; + private String title; private String type; private String author; private String profession; @@ -30,6 +31,15 @@ public class Paper { this.id = id; } + @Column(nullable = false) + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + @Column(nullable = false) public String getType() { return type; diff --git a/src/main/java/com/bupt/note/NoteApplication.java b/src/main/java/com/bupt/note/NoteApplication.java index 78b6d04..4305a90 100644 --- a/src/main/java/com/bupt/note/NoteApplication.java +++ b/src/main/java/com/bupt/note/NoteApplication.java @@ -1,14 +1,9 @@ package com.bupt.note; -import com.bupt.note.Properties.FileProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication -@EnableConfigurationProperties({ - FileProperties.class -}) public class NoteApplication { public static void main(String[] args) { diff --git a/src/main/java/com/bupt/note/Properties/FileProperties.java b/src/main/java/com/bupt/note/Properties/FileProperties.java deleted file mode 100644 index 8ff33c4..0000000 --- a/src/main/java/com/bupt/note/Properties/FileProperties.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.bupt.note.Properties; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties(prefix = "file") -public class FileProperties { - - private String uploadDir; - - public String getUploadDir() { - return uploadDir; - } - - public void setUploadDir(String uploadDir) { - this.uploadDir = uploadDir; - } -} diff --git a/src/main/java/com/bupt/note/Repository/PapaerRepository.java b/src/main/java/com/bupt/note/Repository/PapaerRepository.java index 98b252b..dc7ac91 100644 --- a/src/main/java/com/bupt/note/Repository/PapaerRepository.java +++ b/src/main/java/com/bupt/note/Repository/PapaerRepository.java @@ -2,6 +2,7 @@ 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; -public interface PapaerRepository extends JpaRepository { +public interface PapaerRepository extends JpaRepository, JpaSpecificationExecutor { } diff --git a/src/main/java/com/bupt/note/dto/Page.java b/src/main/java/com/bupt/note/dto/Page.java new file mode 100644 index 0000000..750611d --- /dev/null +++ b/src/main/java/com/bupt/note/dto/Page.java @@ -0,0 +1,25 @@ +package com.bupt.note.dto; + +import java.util.List; + +public class Page { + private List data; + + private Long total; + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public Long getTotal() { + return total; + } + + public void setTotal(Long total) { + this.total = total; + } +} diff --git a/src/main/java/com/bupt/note/dto/QueryPaper.java b/src/main/java/com/bupt/note/dto/QueryPaper.java new file mode 100644 index 0000000..ff5e26d --- /dev/null +++ b/src/main/java/com/bupt/note/dto/QueryPaper.java @@ -0,0 +1,70 @@ +package com.bupt.note.dto; + +/** + * 论文查询 + */ +public class QueryPaper { + private String title; + private String type; + private String author; + private String profession; + private String tag; + private Integer startYear; + private Integer endYear; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getProfession() { + return profession; + } + + public void setProfession(String profession) { + this.profession = profession; + } + + public String getTag() { + return tag; + } + + public void setTag(String tag) { + this.tag = tag; + } + + public Integer getStartYear() { + return startYear; + } + + public void setStartYear(Integer startYear) { + this.startYear = startYear; + } + + public Integer getEndYear() { + return endYear; + } + + public void setEndYear(Integer endYear) { + this.endYear = endYear; + } +} diff --git a/src/main/java/com/bupt/note/dto/UploadPaper.java b/src/main/java/com/bupt/note/dto/UploadPaper.java index cb248b9..56d2e0e 100644 --- a/src/main/java/com/bupt/note/dto/UploadPaper.java +++ b/src/main/java/com/bupt/note/dto/UploadPaper.java @@ -3,6 +3,7 @@ package com.bupt.note.dto; import com.bupt.note.Model.Paper; public class UploadPaper { + private String title; private String type; private String author; private String profession; @@ -12,6 +13,14 @@ public class UploadPaper { private String tag; private Long fileId; + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + public String getType() { return type; } @@ -78,6 +87,7 @@ public class UploadPaper { public Paper toPaper(){ Paper paper=new Paper(); + paper.setTitle(this.title); paper.setType(this.type); paper.setAuthor(this.author); paper.setProfession(this.profession);