From 0f5767069a05ee57a9c63392c1d9290f3e7291ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B5=B7=E7=91=9E?= <3148024859@qq.com> Date: Wed, 10 Jan 2024 13:25:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230109 \345\244\215\344\271\240.md" | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 "50 \345\274\240\350\265\267\347\221\236/20230109 \345\244\215\344\271\240.md" diff --git "a/50 \345\274\240\350\265\267\347\221\236/20230109 \345\244\215\344\271\240.md" "b/50 \345\274\240\350\265\267\347\221\236/20230109 \345\244\215\344\271\240.md" new file mode 100644 index 0000000..0f875fc --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20230109 \345\244\215\344\271\240.md" @@ -0,0 +1,153 @@ +```java +package com.mdd.controller; + +import com.mdd.domain.Book; +import com.mdd.domain.R; +import com.mdd.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/books") +public class BookController { + // 分别写五个方法,添加,修改,查全部,按ID删除,,按ID查单个 + + // controller是表现层,要实现具体的业务,就要调用业务层service + @Autowired + private BookService bookService; + + //添加, + @PostMapping + public R addBook(@RequestBody Book book) { + Integer i = bookService.addBook(book); + if (i > 0) { + return R.success(200,"添加成功!"); + } else { + return R.failure(404,"添加失败"); + } + } + + // 修改 + @PutMapping + public R updateBook(@RequestBody Book book) { + Integer i = bookService.updateBook(book); + if (i > 0) { + return R.success(200,"更新成功!"); + } else { + return R.failure(404,"更新失败"); + } + + } + + // 查全部 + @GetMapping + public R getAllBook() { + List allBook = bookService.getAllBook(); + if (allBook != null) + return R.success(200, allBook); + else + return R.failure(404, "查询失败,没数据!"); + + } + + // 按ID查单个 + @GetMapping("/{id}") + public R getBook(@PathVariable Integer id) { + Book book = bookService.getBook(id); + if (book != null) + return R.success(200, book); + else + return R.failure(404, "查询失败,没有该数据!"); + } + + // 按ID查单个 + @DeleteMapping("/{id}") + public R delBook(@PathVariable Integer id) { + Integer i = bookService.delBook(id); + if (i > 0) { + return R.success(200,"删除成功!"); + } else { + return R.failure(404,"删除失败"); + } + } +} + +``` + +```java +package com.mock.mapper; + +import com.mock.pojo.Book; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +import java.util.List; + +public interface BookMapper { + // 查询所有 + @Select("select id, book_name bookName, author, publisher from book order by id desc;") + List findAll(); + + // 新增 + @Insert("insert into book (book_name, author, publisher) values (#{bookName}, #{author}, #{publisher})") + Integer insertBook(Book book); + + // 修改 + @Update("update book set book_name = #{bookName} , author = #{author} , publisher = #{publisher} where id = #{id} ;") + Integer editBook(Book book); + + // 根据id查一个 + @Select("select id, book_name bookName, author, publisher from book where id = #{id};") + Book findById(Integer id); + + // 根据id删除 + @Delete("delete from book where id = #{id};") + Integer deleteBook(Integer id); +} +``` + +```java +package com.mock.service.impl; + +import com.mock.mapper.BookMapper; +import com.mock.pojo.Book; +import com.mock.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +@Service +public class BookServiceImpl implements BookService { + @Autowired + private BookMapper bookMapper; + @Override + public List findAll() { + return bookMapper.findAll(); + } + + @Override + public Integer insertBook(Book book) { + return bookMapper.insertBook(book); + } + + @Override + public Integer editBook(Book book) { + return bookMapper.editBook(book); + } + + @Override + public Book findById(Integer id) { + return bookMapper.findById(id); + } + + @Override + public Integer deleteBook(Integer id) { + return bookMapper.deleteBook(id); + } +} +``` + -- Gitee