首页>代码>springboot+vue实现CRUD、分页及条件查询>/springboot_crud/src/main/java/com/gs/controller/BookController.java
package com.gs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gs.controller.util.R;
import com.gs.domain.Book;
import com.gs.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;


@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService bookService;

    @GetMapping
    public R getAll(){
        return new R(true,bookService.list());
    }

    @PostMapping
    public R save(@RequestBody Book book) throws IOException {

        if(book.getName().equals("123")){
            throw new IOException();
        }

        boolean flag = bookService.save(book);

        return new R(flag,flag ? "添加成功^_^" : "添加失败-_-!");
    }

    @PutMapping
    public R update(@RequestBody Book book){
        return new R(bookService.modify(book));
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(bookService.delete(id));
    }

    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        return new R(true,bookService.getById(id));
    }

    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Book book){
        System.out.println("参数==>"+book);
        IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
        //如果当前页码值大于总页码值,那么重新执行操作,使最大页码值为当前页码
        if (currentPage > page.getPages()){
            page = bookService.getPage((int)page.getPages(), pageSize,book);
        }
        return new R(true,page);
    }


}
最近下载更多
15719908287  LV9 2024年11月6日
sgm123456  LV13 2024年10月23日
币圈老大  LV17 2024年7月24日
ZthuaL  LV2 2024年6月21日
不想起  LV10 2024年4月22日
welcome丶  LV8 2024年3月7日
微信网友_6829521472425984  LV5 2024年1月20日
oulingqiao  LV13 2024年1月11日
廖业贵  LV18 2023年11月16日
微信网友_6580000030609408  LV2 2023年7月27日
最近浏览更多
qinzhifang  LV1 3月15日
13188140406 3月12日
暂无贡献等级
sgm123456  LV13 2024年10月23日
15719908287  LV9 2024年9月11日
币圈老大  LV17 2024年7月24日
ZthuaL  LV2 2024年6月21日
2775645521  LV7 2024年6月16日
chirsbey2 2024年5月30日
暂无贡献等级
做自己的太阳  LV11 2024年4月19日
welcome丶  LV8 2024年3月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友