package com.example.demo07.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo07.controller.util.R;
import com.example.demo07.dto.Book;
import com.example.demo07.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
IBookService bookService;
@GetMapping
public R getAll(){
//该方法继承自IService
List<Book> bookList = bookService.list();
R r = new R();
r.setData(bookList);
r.setFlag(true);
return r;
}
@PostMapping
//@RequestBody 接收的是请求体里面的数据(post方式参数在请求体中),@RequestBody以简单对象接收前端传过来的json数据
public R save(@RequestBody Book book){
//该方法继承自IService
Boolean result = bookService.save(book);
R r = new R(result);
return r;
}
@PutMapping
public R update(@RequestBody Book book){
//该方法继承自IService
Boolean result = bookService.updateById(book);
R r = new R(result);
return r;
}
@DeleteMapping("/{id}")
public R delete(@PathVariable Integer id){
//该方法继承自IService
Boolean result = bookService.removeById(id);
return new R(result);
}
@GetMapping("/{id}")
public R getById(@PathVariable Integer id){
//该方法继承自IService
Book book = bookService.getById(id);
return new R( true,book);
}
@GetMapping("/{currentPage}/{pageSize}")
public R getPage( @PathVariable int currentPage, @PathVariable int pageSize ){
IPage<Book> page = bookService.getPage(currentPage,pageSize);
return new R(true,page);
}
}
service接口
package com.example.demo07.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo07.dto.Book;
public interface IBookService extends IService<Book> {
IPage<Book> getPage(int currentPage, int pageSize);
}
service实现
package com.example.demo07.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo07.dao.BookDao;
import com.example.demo07.dto.Book;
import com.example.demo07.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
测试
结果
{
"flag": true,
"data": [
{
"id": 1,
"type": null,
"name": "毛选",
"description": null
},
{
"id": 2,
"type": "哲学",
"name": "局外人",
"description": null
},
{
"id": 3,
"type": "文学",
"name": "葡萄牙的高山",
"description": "苦难"
},
{
"id": 4,
"type": "文学",
"name": "活着",
"description": "苦难"
},
{
"id": 5,
"type": "文学",
"name": "平凡的世界",
"description": null
},
{
"id": 6,
"type": "文学",
"name": "孩子王",
"description": null
},
{
"id": 7,
"type": "文学",
"name": "孔乙己",
"description": null
}
]
}
测试
结果
{
"flag": true,
"data": {
"records": [
{
"id": 1,
"type": null,
"name": "毛选",
"description": null
},
{
"id": 2,
"type": "哲学",
"name": "局外人",
"description": null
},
{
"id": 3,
"type": "文学",
"name": "葡萄牙的高山",
"description": "苦难"
}
],
"total": 7,
"size": 3,
"current": 1,
"orders": [],
"optimizeCountSql": true,
"searchCount": true,
"countId": null,
"maxLimit": null,
"pages": 3
}
}