【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】
直接开干
创建Controller
package com.dingjiaxiong.controller;
import com.dingjiaxiong.domain.Book;
import com.dingjiaxiong.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* ClassName: BookController
* date: 2022/10/17 20:12
*
* @author DingJiaxiong
*/
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
}
这样其实就算完事儿了
直接启动程序
使用Postman 测试 http://localhost/books
没毛病
加入所有方法接口
package com.dingjiaxiong.controller;
import com.dingjiaxiong.domain.Book;
import com.dingjiaxiong.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* ClassName: BookController
* date: 2022/10/17 20:12
*
* @author DingJiaxiong
*/
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.updateById(book);
}
@DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id){
return bookService.removeById(id);
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
}
/ 可写可不写,会自动加的
重启服务器,测试
按ID查询
新增
修改
按ID删除
OK,删除成功
小结一下
【分页】
新增一个接口方法
IPage getPage(int currentPage , int pageSize);
实现类
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
return bookDao.selectPage(page , null);
}
OK,表现层
@GetMapping("/{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage ,@PathVariable int pageSize){
return bookService.getPage(currentPage, pageSize);
}
OK,重启服务器测试
没毛病