From 78aa53ebec7a56229120316ebbe6dd628dbc430d Mon Sep 17 00:00:00 2001 From: JUZIZZZ <2352856138@qq.com> Date: Tue, 5 Nov 2024 22:35:16 +0800 Subject: [PATCH] =?UTF-8?q?24.11.05=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=EF=BC=8C=E6=9C=89=E4=BA=9B=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 25 ++-- .../homework/controller/BooksController.java | 91 +++++++++------ .../controller/Borrow_recordsController.java | 109 +++++++++++++----- .../Reviews202201020121Controller.java | 68 +++++++++++ .../dao/Reviews202201020121Mapper.java | 19 +++ .../task1/homework/model/Result.java | 60 ++++++++++ .../homework/model/Reviews202201020121.java | 31 +++++ .../service/IReviews202201020121Service.java | 17 +++ .../impl/Reviews202201020121ServiceImpl.java | 23 ++++ .../c202201020121/task1/homework/test.java | 33 ++++++ src/main/resources/application.properties | 4 +- .../mapper/Reviews202201020121Mapper.xml | 5 + 12 files changed, 415 insertions(+), 70 deletions(-) create mode 100644 src/main/java/com/c202201020121/task1/homework/controller/Reviews202201020121Controller.java create mode 100644 src/main/java/com/c202201020121/task1/homework/dao/Reviews202201020121Mapper.java create mode 100644 src/main/java/com/c202201020121/task1/homework/model/Result.java create mode 100644 src/main/java/com/c202201020121/task1/homework/model/Reviews202201020121.java create mode 100644 src/main/java/com/c202201020121/task1/homework/service/IReviews202201020121Service.java create mode 100644 src/main/java/com/c202201020121/task1/homework/service/impl/Reviews202201020121ServiceImpl.java create mode 100644 src/main/java/com/c202201020121/task1/homework/test.java create mode 100644 src/main/resources/mapper/Reviews202201020121Mapper.xml diff --git a/pom.xml b/pom.xml index b737f4b..68b715e 100644 --- a/pom.xml +++ b/pom.xml @@ -60,17 +60,28 @@ spring-boot-starter-test test + + + + + + + - org.mybatis - mybatis - 3.5.14 - compile + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.7 - org.mybatis.spring.boot - mybatis-spring-boot-starter - 3.0.3 + com.baomidou + mybatis-plus-generator + 3.5.7 + + + org.freemarker + freemarker + 2.3.31 diff --git a/src/main/java/com/c202201020121/task1/homework/controller/BooksController.java b/src/main/java/com/c202201020121/task1/homework/controller/BooksController.java index 39d40a8..12846b0 100644 --- a/src/main/java/com/c202201020121/task1/homework/controller/BooksController.java +++ b/src/main/java/com/c202201020121/task1/homework/controller/BooksController.java @@ -2,16 +2,20 @@ package com.c202201020121.task1.homework.controller; import com.c202201020121.task1.homework.model.Books; import com.c202201020121.task1.homework.model.Evaluations; +import com.c202201020121.task1.homework.model.Result; import com.c202201020121.task1.homework.service.BooksService; import com.c202201020121.task1.homework.service.EvaluationsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; +@CrossOrigin @Controller public class BooksController { @@ -21,60 +25,79 @@ public class BooksController { @Autowired private EvaluationsService evaluationsService; -// @RequestMapping("/getbooks") -// public String getBooks(String username, String password,Model model) { -//// if (username.equals("admin")&&password.equals("123456")){ -// model.addAttribute("booksList", booksService.getBooks()); -// return "main.html"; -//// } -//// return "login.html"; -// } + @ResponseBody @RequestMapping("/getbooks") - public String getBooks(Model model) { - model.addAttribute("booksList", booksService.getBooks()); -// model.addAttribute("evaluationsList", evaluationsService.getEvaluations()); - return "main.html"; + public Result getBooks() { + List booksList = booksService.getBooks(); + if (booksList!= null &&!booksList.isEmpty()) { + // 如果获取到书籍列表成功,返回包含数据的Result,状态码为默认的0(成功),消息为"success" + return Result.ok().put("data", booksList); + } else { + // 如果获取书籍列表失败,返回错误信息的Result,这里假设状态码为500表示获取数据失败,消息可自定义 + return Result.error(500, "获取书籍列表失败"); + } } - + @ResponseBody @RequestMapping("/addbookspage") - public String addBooks() { - return "addbookspage.html"; + public Result addBooksPage() { + return Result.ok().put("page", "addbookspage.html"); } + @ResponseBody @RequestMapping("/addbookscommit") - public String addBooksCommit(Books books) { - booksService.addBooks(books); - return "redirect:/getbooks"; + public Result addBooksCommit(Books books) { + try { + booksService.addBooks(books); + return Result.ok(); + } catch (Exception e) { + return Result.error(); + } } + @ResponseBody @RequestMapping("/deletebooks") - public String deleteBooks(int id) { - booksService.deleteBooks(id); - return "redirect:/getbooks"; + public Result deleteBooks(int id) { + try { + booksService.deleteBooks(id); + return Result.ok(); + } catch (Exception e) { + return Result.error(); + } } + @ResponseBody @RequestMapping("/updatebookspage") - public String updateBooks(Books books,Model model) { - model.addAttribute("books",books); - return "updatebooks.html"; + public Result updateBooksPage(Books books, Model model) { + model.addAttribute("books", books); + return Result.ok().put("page", "updatebooks.html"); } + + @ResponseBody @RequestMapping("/updatebookscommit") - public String updateBooksCommit(Books books) { - booksService.updateBooks(books); - return "redirect:/getbooks"; + public Result updateBooksCommit(Books books) { + try { + booksService.updateBooks(books); + return Result.ok(); + } catch (Exception e) { + return Result.error(); + } } - + @ResponseBody @RequestMapping("/addbooksevaluations") - public String addBooksEvaluations() { - return "addbooksevaluations.html"; + public Result addBooksEvaluations() { + return Result.ok().put("page", "addbooksevaluations.html"); } + @ResponseBody @RequestMapping("/addbooksevaluationscommit") - public String addBooksEvaluationsCommit(Evaluations evaluations) { - evaluationsService.addEvaluations(evaluations); - return "redirect:/getbooks"; + public Result addBooksEvaluationsCommit(Evaluations evaluations) { + try { + evaluationsService.addEvaluations(evaluations); + return Result.ok(); + } catch (Exception e) { + return Result.error(); + } } - } diff --git a/src/main/java/com/c202201020121/task1/homework/controller/Borrow_recordsController.java b/src/main/java/com/c202201020121/task1/homework/controller/Borrow_recordsController.java index e1ea5eb..f1a395f 100644 --- a/src/main/java/com/c202201020121/task1/homework/controller/Borrow_recordsController.java +++ b/src/main/java/com/c202201020121/task1/homework/controller/Borrow_recordsController.java @@ -3,63 +3,118 @@ package com.c202201020121.task1.homework.controller; //import ch.qos.logback.core.model.Model; //import com.c202201020121.task1.homework.model.Borrow_records; import com.c202201020121.task1.homework.model.Borrow_records; +import com.c202201020121.task1.homework.model.Result; import com.c202201020121.task1.homework.service.Borrow_recordsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import java.util.Collections; import java.util.List; - +@CrossOrigin @Controller public class Borrow_recordsController { @Autowired public Borrow_recordsService borrow_recordsService; - + @ResponseBody @RequestMapping("/getborrow_records") - public String getBorrow_records(Model model) - { - model.addAttribute("borrow_recordsList",borrow_recordsService.getBorrow_records()); - return "borrow_records.html"; + public Result getBorrow_records() { + List borrowRecordsList = borrow_recordsService.getBorrow_records(); + return Result.ok().put("data", borrowRecordsList); } - + @ResponseBody @RequestMapping("/addborrow_recordspage") - public String addBorrow_recordsPage() - { - return "addborrow_records.html"; + public Result addBorrow_recordsPage() { + return Result.ok().put("page", "addborrow_records.html"); } + @ResponseBody @RequestMapping("/addborrow_recordscommit") - public String addBorrow_recordsCommit(Borrow_records borrow_records) - { - borrow_recordsService.addBorrow_records(borrow_records); - return "redirect:/getborrow_records"; + public Result addBorrow_recordsCommit(Borrow_records borrow_records) { + try { + borrow_recordsService.addBorrow_records(borrow_records); + return Result.ok(); + } catch (Exception e) { + return Result.error(); + } } + @ResponseBody @RequestMapping("/deleteborrow_records") - public String deleteBorrow_records(int id) - { - borrow_recordsService.deleteBorrow_records(id); - return "redirect:/getborrow_records"; + public Result deleteBorrow_records(int id) { + try { + borrow_recordsService.deleteBorrow_records(id); + return Result.ok(); + } catch (Exception e) { + return Result.error(); + } } + @ResponseBody @RequestMapping("/updateborrow_recordspage") - public String updateBorrow_records(Borrow_records borrow_records,Model model) - { - model.addAttribute("borrow_records",borrow_records); - return "updateborrow_records.html"; + public Result updateBorrow_recordsPage(Borrow_records borrow_records, Model model) { + model.addAttribute("borrow_records", borrow_records); + return Result.ok().put("page", "updateborrow_records.html"); } - + @ResponseBody @RequestMapping("/updateborrow_recordscommit") - public String updateBorrow_recordsCommit(Borrow_records borrow_records) - { - borrow_recordsService.updateBorrow_records(borrow_records); - return "redirect:/getborrow_records"; + public Result updateBorrow_recordsCommit(Borrow_records borrow_records) { + try { + borrow_recordsService.updateBorrow_records(borrow_records); + return Result.ok(); + } catch (Exception e) { + return Result.error(); + } } +// +// @RequestMapping("/getborrow_records") +// public String getBorrow_records(Model model) +// { +// model.addAttribute("borrow_recordsList",borrow_recordsService.getBorrow_records()); +// return "borrow_records.html"; +// } +// +// +// @RequestMapping("/addborrow_recordspage") +// public String addBorrow_recordsPage() +// { +// return "addborrow_records.html"; +// } +// +// @RequestMapping("/addborrow_recordscommit") +// public String addBorrow_recordsCommit(Borrow_records borrow_records) +// { +// borrow_recordsService.addBorrow_records(borrow_records); +// return "redirect:/getborrow_records"; +// } +// +// @RequestMapping("/deleteborrow_records") +// public String deleteBorrow_records(int id) +// { +// borrow_recordsService.deleteBorrow_records(id); +// return "redirect:/getborrow_records"; +// } +// +// @RequestMapping("/updateborrow_recordspage") +// public String updateBorrow_records(Borrow_records borrow_records,Model model) +// { +// model.addAttribute("borrow_records",borrow_records); +// return "updateborrow_records.html"; +// } +// +// +// @RequestMapping("/updateborrow_recordscommit") +// public String updateBorrow_recordsCommit(Borrow_records borrow_records) +// { +// borrow_recordsService.updateBorrow_records(borrow_records); +// return "redirect:/getborrow_records"; +// } } diff --git a/src/main/java/com/c202201020121/task1/homework/controller/Reviews202201020121Controller.java b/src/main/java/com/c202201020121/task1/homework/controller/Reviews202201020121Controller.java new file mode 100644 index 0000000..5105844 --- /dev/null +++ b/src/main/java/com/c202201020121/task1/homework/controller/Reviews202201020121Controller.java @@ -0,0 +1,68 @@ +package com.c202201020121.task1.homework.controller; + +import com.c202201020121.task1.homework.model.Result; +import com.c202201020121.task1.homework.model.Reviews202201020121; +import com.c202201020121.task1.homework.service.IReviews202201020121Service; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.List; + +/** + *

+ * 前端控制器 + *

+ * + * @author 作者是橘子大王 + * @since 2024-11-03 + */ +@CrossOrigin +@Controller +@RequestMapping("/reviews202201020121") +public class Reviews202201020121Controller { + + @Autowired + IReviews202201020121Service iReviews202201020121Service; + + @ResponseBody + @RequestMapping("reviewslist") + public Result getReviewsList(){ + List list = iReviews202201020121Service.list(); + return Result.ok().put("data",list); + } + + @ResponseBody + @RequestMapping("addReviews") + public Result addReviews(Reviews202201020121 reviews){ + Boolean result = iReviews202201020121Service.save(reviews); + if (result){ + return Result.ok(); + } + return Result.error(); + } + + @ResponseBody + @RequestMapping("updateReviews") + public Result updateReviews(Reviews202201020121 reviews){ + Boolean result = iReviews202201020121Service.updateById(reviews); + if (result){ + return Result.ok(); + } + return Result.error(); + } + + @ResponseBody + @RequestMapping("deleteReviews") + public Result deleteReviews(int id){ + Boolean result = iReviews202201020121Service.removeById(id); + if (result){ + return Result.ok(); + } + return Result.error(); + } + + +} diff --git a/src/main/java/com/c202201020121/task1/homework/dao/Reviews202201020121Mapper.java b/src/main/java/com/c202201020121/task1/homework/dao/Reviews202201020121Mapper.java new file mode 100644 index 0000000..8d1b1f0 --- /dev/null +++ b/src/main/java/com/c202201020121/task1/homework/dao/Reviews202201020121Mapper.java @@ -0,0 +1,19 @@ +package com.c202201020121.task1.homework.dao; + + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.c202201020121.task1.homework.model.Reviews202201020121; +import org.apache.ibatis.annotations.Mapper; + +/** + *

+ * Mapper 接口 + *

+ * + * @author 作者是橘子大王 + * @since 2024-11-03 + */ +@Mapper +public interface Reviews202201020121Mapper extends BaseMapper { + +} diff --git a/src/main/java/com/c202201020121/task1/homework/model/Result.java b/src/main/java/com/c202201020121/task1/homework/model/Result.java new file mode 100644 index 0000000..1022aa7 --- /dev/null +++ b/src/main/java/com/c202201020121/task1/homework/model/Result.java @@ -0,0 +1,60 @@ +package com.c202201020121.task1.homework.model; + +import java.util.HashMap; +import java.util.Map; + +/** + * 返回数据封装类 + */ +public class Result extends HashMap { + private static final long serialVersionUID = 1L; + + public Result() { + put("code", 0); + put("msg", "success"); + } + + public static Result error() { + return error(500, "未知异常,请联系管理员"); + } + + public static Result error(String msg) { + return error(500, msg); + } + + public static Result error(int code, String msg) { + Result r = new Result(); + r.put("code", code); + r.put("msg", msg); + return r; + } + + public static Result ok(String msg) { + Result r = new Result(); + r.put("msg", msg); + return r; + } + + + public static Result ok(Object obj) { + Result r = new Result(); + r.put("data", obj); + return r; + } + + public static Result ok(Map map) { + Result r = new Result(); + r.putAll(map); + return r; + } + + public static Result ok() { + return new Result(); + } + + public Result put(String key, Object value) { + super.put(key, value); + return this; + } +} + diff --git a/src/main/java/com/c202201020121/task1/homework/model/Reviews202201020121.java b/src/main/java/com/c202201020121/task1/homework/model/Reviews202201020121.java new file mode 100644 index 0000000..ca17819 --- /dev/null +++ b/src/main/java/com/c202201020121/task1/homework/model/Reviews202201020121.java @@ -0,0 +1,31 @@ +package com.c202201020121.task1.homework.model; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import java.io.Serializable; +import lombok.Getter; +import lombok.Setter; + +/** + *

+ * + *

+ * + * @author 作者是橘子大王 + * @since 2024-11-03 + */ +@Getter +@Setter +public class Reviews202201020121 implements Serializable { + + private static final long serialVersionUID = 1L; + + @TableId(value = "id", type = IdType.AUTO) + private Integer id; + + private String evaluation; + + private Integer userid; + + private Integer bookid; +} diff --git a/src/main/java/com/c202201020121/task1/homework/service/IReviews202201020121Service.java b/src/main/java/com/c202201020121/task1/homework/service/IReviews202201020121Service.java new file mode 100644 index 0000000..98eac5e --- /dev/null +++ b/src/main/java/com/c202201020121/task1/homework/service/IReviews202201020121Service.java @@ -0,0 +1,17 @@ +package com.c202201020121.task1.homework.service; + +//import com.baomidou.mybatisplus.model.Reviews202201020121; +import com.baomidou.mybatisplus.extension.service.IService; +import com.c202201020121.task1.homework.model.Reviews202201020121; + +/** + *

+ * 服务类 + *

+ * + * @author 作者是橘子大王 + * @since 2024-11-03 + */ +public interface IReviews202201020121Service extends IService { + +} diff --git a/src/main/java/com/c202201020121/task1/homework/service/impl/Reviews202201020121ServiceImpl.java b/src/main/java/com/c202201020121/task1/homework/service/impl/Reviews202201020121ServiceImpl.java new file mode 100644 index 0000000..af716bc --- /dev/null +++ b/src/main/java/com/c202201020121/task1/homework/service/impl/Reviews202201020121ServiceImpl.java @@ -0,0 +1,23 @@ +package com.c202201020121.task1.homework.service.impl; + +//import com.baomidou.mybatisplus.model.Reviews202201020121; +//import com.baomidou.mybatisplus.mapper.Reviews202201020121Mapper; +//import com.baomidou.mybatisplus.service.IReviews202201020121Service; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.c202201020121.task1.homework.dao.Reviews202201020121Mapper; +import com.c202201020121.task1.homework.model.Reviews202201020121; +import com.c202201020121.task1.homework.service.IReviews202201020121Service; +import org.springframework.stereotype.Service; + +/** + *

+ * 服务实现类 + *

+ * + * @author 作者是橘子大王 + * @since 2024-11-03 + */ +@Service +public class Reviews202201020121ServiceImpl extends ServiceImpl implements IReviews202201020121Service { + +} diff --git a/src/main/java/com/c202201020121/task1/homework/test.java b/src/main/java/com/c202201020121/task1/homework/test.java new file mode 100644 index 0000000..98adba4 --- /dev/null +++ b/src/main/java/com/c202201020121/task1/homework/test.java @@ -0,0 +1,33 @@ +package com.c202201020121.task1.homework; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; + +import java.nio.file.Paths; + +public class test { + public static void main(String[] args) { + FastAutoGenerator.create("jdbc:mysql://10.33.66.120:3306/task202201020121?serverTimezone=UTC", + "202201020121", + "@hnucm1254") + .globalConfig(builder -> builder + .author("作者是橘子大王") + .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java") + .commentDate("yyyy-MM-dd") + ) + .packageConfig(builder -> builder + .parent("com.baomidou.mybatisplus") + .entity("model") + .mapper("mapper") + .service("service") + .serviceImpl("service.impl") + .xml("mapper.xml") + ) + .strategyConfig(builder -> builder + .entityBuilder() + .enableLombok() + ) + .templateEngine(new FreemarkerTemplateEngine()) + .execute(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 784e2e7..355f0e0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,8 +11,8 @@ spring.datasource.username=202201020121 spring.datasource.password=@hnucm1254 #??????????MyBatis?? #??Mybatis?Mapper?? -mybatis.mapper-locations=classpath:mapper/*.xml +mybatis-plus.mapper-locations=classpath:mapper/*.xml #??Mybatis????? -mybatis.type-aliases-package=com.c202201020121.task1.homework.model +mybatis-plus.type-aliases-package=com.c202201020121.task1.homework.model #???? logging.level.com.liulingzhi.springboot.springboot = debug \ No newline at end of file diff --git a/src/main/resources/mapper/Reviews202201020121Mapper.xml b/src/main/resources/mapper/Reviews202201020121Mapper.xml new file mode 100644 index 0000000..98181a7 --- /dev/null +++ b/src/main/resources/mapper/Reviews202201020121Mapper.xml @@ -0,0 +1,5 @@ + + + + +