第四次作业后端代码(未完成)

This commit is contained in:
adan 2024-11-11 01:32:03 +08:00
parent 879eb9a12a
commit 9a244f8efd
17 changed files with 309 additions and 11 deletions

View File

@ -0,0 +1,56 @@
package com.c202201020417.task1.controller;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Books202201020417;
import com.c202201020417.task1.model.Result;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.IBooks202201020417Service;
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;
/**
* <p>
* 前端控制器
* </p>
*
* @author 张三
* @since 2024-10-13
*/
@CrossOrigin
@Controller
@RequestMapping("/books202201020417")
public class Books202201020417Controller {
@Autowired
IBooks202201020417Service books202201020417Service;
@ResponseBody
@RequestMapping("/getBooks")
public Result getBooks(){
List<Books202201020417> books202201020417List = books202201020417Service.list();
return Result.ok().put("data", books202201020417List);
}
@ResponseBody
@RequestMapping("/addBooks")
public Result addBooks(Books202201020417 book202201120417 ){
/*iStudentService.list();*/
boolean result = books202201020417Service.saveOrUpdate(book202201120417);
if(result){
return Result.ok();
}
return Result.error();
}
@ResponseBody
@RequestMapping("/deleteBooks")
public Result deleteBook(int id){
/*iStudentService.list();*/
boolean result = books202201020417Service.removeById(id);
if(result){
return Result.ok();
}
return Result.error();
}
}

View File

@ -1,13 +1,17 @@
package com.c202201020417.task1.controller;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Result;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.BooksService;
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;
@CrossOrigin
@Controller
public class BooksController {
@Autowired
@ -17,8 +21,13 @@ public class BooksController {
model.addAttribute("booksList",booksService.getAllBook());
return "bookpage.html";
}
@ResponseBody
@RequestMapping("/getbooks1")
public Result getbooks1(){
return Result.ok().put("data",booksService.getAllBook());
}
@RequestMapping("/addbook")
public String addbook(Books books){
public String addbook(){
return "addbookpage.html";
}
@RequestMapping("/addbookcommit")
@ -26,11 +35,31 @@ public class BooksController {
booksService.addBook(books);
return "redirect:/bookpage";
}
@ResponseBody
@RequestMapping("/addbookcommit1")
public Result addbookcommit1(Books book){
int result = booksService.addBook(book);
if (result>=1){
return Result.ok();
}else {
return Result.error();
}
}
@RequestMapping("/deletebook")
public String deletebook(int id){
booksService.deleteBook(id);
return "redirect:/bookpage";
}
@ResponseBody
@RequestMapping("/deletebook1")
public Result deletebook1(int id){
int result = booksService.deleteBook(id);
if (result>=1){
return Result.ok();
}else {
return Result.error();
}
}
@RequestMapping("/updatebook")
public String updatebook(Books books,Model model){
model.addAttribute("books",books);
@ -41,5 +70,25 @@ public class BooksController {
booksService.updateBook(books);
return "redirect:/bookpage";
}
@ResponseBody
@RequestMapping("/updatebookcommit1")
public Result updatebookcommit1(Books books){
int result = booksService.updateBook(books);
if (result>=1){
return Result.ok();
}else {
return Result.error();
}
}
@ResponseBody
@RequestMapping("/addreview1")
public Result addreview1(Reviews reviews){
int result = booksService.addReview(reviews);
if (result>=1){
return Result.ok();
}else {
return Result.error();
}
}
}

View File

@ -2,12 +2,14 @@ package com.c202201020417.task1.controller;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Borrow;
import com.c202201020417.task1.model.Result;
import com.c202201020417.task1.service.BooksService;
import com.c202201020417.task1.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BorrowController {
@ -24,6 +26,16 @@ public class BorrowController {
borrowService.addBorrow(borrow);
return "redirect:/bookpage";
}
@ResponseBody
@RequestMapping("/addborrowcommmit1")
public Result addborrowcommmit1(Borrow borrow)
{ int result = borrowService.addBorrow(borrow);
if (result>=1){
return Result.ok();
}else {
return Result.error();
}
}
@RequestMapping("/updateborrow")
public String updateborrow(Borrow borrow,Model model)
{

View File

@ -2,6 +2,7 @@ package com.c202201020417.task1.controller;
import com.c202201020417.task1.dao.ReviewsMapper;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Result;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.BooksService;
import com.c202201020417.task1.service.ReviewsService;
@ -9,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ReviewsController {
@ -16,7 +18,7 @@ public class ReviewsController {
private ReviewsService reviewsService;
@RequestMapping("/addreview")
public String addreviews(Reviews reviews){
return "addreview.html";
return "addreview.html";
}
@RequestMapping("/addreviewcommit")
public String addreviewcommit(Reviews reviews){

View File

@ -0,0 +1,19 @@
package com.c202201020417.task1.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.c202201020417.task1.model.Books202201020417;
import org.apache.ibatis.annotations.Mapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author 张三
* @since 2024-10-13
*/
@Mapper
public interface Books202201020417Mapper extends BaseMapper<Books202201020417> {
}

View File

@ -1,5 +1,6 @@
package com.c202201020417.task1.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;

View File

@ -9,5 +9,5 @@ import java.util.List;
@Mapper
public interface ReviewsMapper {
public List<Reviews> findReviewsByBooksId(int booksid);
public int addReviews(Reviews reviews);
public int addReviews(Reviews reviews);
}

View File

@ -3,6 +3,7 @@ package com.c202201020417.task1.model;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@ -11,10 +12,9 @@ public class Books {
private int id;
private String title;
private String author;
private String publisher;
private String isbn;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date published_date;
private String publisher;
private String published_date;
private List<Reviews> reviewsList;
private List<User> usersList;
}

View File

@ -0,0 +1,39 @@
package com.c202201020417.task1.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;
/**
* <p>
*
* </p>
*
* @author 张三
* @since 2024-10-13
*/
@Getter
@Setter
public class Books202201020417 implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String title;
private String author;
private String isbn;
private String publisher;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDateTime publishedDate;
}

View File

@ -0,0 +1,58 @@
package com.c202201020417.task1.model;
import java.util.HashMap;
import java.util.Map;
/**
* 返回数据封装类
*/
public class Result extends HashMap<String, Object> {
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<String, Object> 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;
}
}

View File

@ -1,6 +1,8 @@
package com.c202201020417.task1.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;
import java.util.List;
@ -10,4 +12,6 @@ public interface BooksService {
public int addBook(Books book);
public int deleteBook(int id);
public int updateBook(Books book);
public int addReview(Reviews reviews);
}

View File

@ -0,0 +1,19 @@
package com.c202201020417.task1.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.c202201020417.task1.model.Books202201020417;
import com.c202201020417.task1.model.Reviews;
/**
* <p>
* 服务类
* </p>
*
* @author 张三
* @since 2024-10-13
*/
public interface IBooks202201020417Service extends IService<Books202201020417> {
int addReview(Reviews review);
}

View File

@ -0,0 +1,30 @@
package com.c202201020417.task1.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c202201020417.task1.dao.Books202201020417Mapper;
import com.c202201020417.task1.dao.ReviewsMapper;
import com.c202201020417.task1.model.Books202201020417;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.IBooks202201020417Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author 张三
* @since 2024-10-13
*/
@Service
public class Books202201020417ServiceImpl extends ServiceImpl<Books202201020417Mapper, Books202201020417> implements IBooks202201020417Service {
@Autowired
private ReviewsMapper reviewsMapper;
@Override
public int addReview(Reviews review) {
return reviewsMapper.addReviews(review);
}
}

View File

@ -1,6 +1,7 @@
package com.c202201020417.task1.service.impl;
import com.c202201020417.task1.dao.BooksMapper;
import com.c202201020417.task1.dao.ReviewsMapper;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.BooksService;
@ -13,6 +14,8 @@ import java.util.List;
public class BooksServiceImpl implements BooksService {
@Autowired
private BooksMapper booksMapper;
@Autowired
private ReviewsMapper reviewsMapper;
@Override
public List<Books> getAllBook() {
return booksMapper.getAllBook();
@ -33,7 +36,7 @@ public class BooksServiceImpl implements BooksService {
return booksMapper.updateBook(book);
}
@Override
public int addReview(Reviews reviews) {return reviewsMapper.addReviews(reviews);}
}

View File

@ -17,10 +17,10 @@ public class ReviewsServiceImpl implements ReviewsService {
return List.of();
}
@Override
public int addReviews(Reviews reviews)
{
return reviewsMapper.addReviews(reviews);
public int addReviews(Reviews reviews) {
return reviewsMapper.addReviews(reviews); // 通过 reviewsMapper 实例调用方法
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.c202201020417.task1.dao.Books202201020417Mapper">
</mapper>

View File

@ -7,6 +7,7 @@
<insert id="addBook" parameterType="com.c202201020417.task1.model.Books">
insert into books202201020417(title,author,isbn,publisher,published_date) values(#{title},#{author},#{isbn},#{publisher},#{published_date})
</insert>
<delete id="deleteBook" parameterType="Integer">
delete from books202201020417 where id=#{id}
</delete>