目录

①勾上需要使用的技术
②需要手动导入的依赖
<dependency> <groupId>com.baomidougroupId> <artifactId>mybatis-plus-boot-starterartifactId> <version>3.5.1version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druid-spring-boot-starterartifactId> <version>1.2.11version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> dependency>③修改配置文件为yml格式,并且设置端口号
server: port: 80
package com.learn.domain; import lombok.Data; /** * @author 咕咕猫 * @version 1.0 */ @Data public class Book { private Integer id; private String type; private String name; private String description; }
@Test void testGetPage(){ IPage page = new Page(1,5); bookDao.selectPage(page,null); System.out.println(page.getCurrent()); System.out.println(page.getSize()); System.out.println(page.getTotal()); System.out.println(page.getPages()); System.out.println(page.getRecords()); }
package com.learn.config; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MPConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ //1.定义Mp拦截器 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //2.添加具体的拦截器 interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } }
package com.learn.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.learn.domain.Book; import com.learn.service.IBookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * @author 咕咕猫 * @version 1.0 */ //@RestController @RequestMapping("/books") public class BookController2 { @Autowired private IBookService bookService; @GetMapping public ListgetAll() { return bookService.list(); } @PostMapping public Boolean save(@RequestBody Book book) { return bookService.save(book); } @PutMapping public Boolean update(@RequestBody Book book) { return bookService.modify(book); } @DeleteMapping("{id}") public Boolean delete(@PathVariable Integer id) { return bookService.delete(id); } @GetMapping("{id}") public Book getById(@PathVariable Integer id) { return bookService.getById(id); } @GetMapping("{currentPage}/{pageSize}") public IPagegetPage(@PathVariable int currentPage, int pageSize) { return bookService.getPage(currentPage,pageSize); } }
package com.learn.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.learn.controller.utils.R; import com.learn.domain.Book; import com.learn.service.IBookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * @author 咕咕猫 * @version 1.0 */ @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) { // R r = new R(); // boolean flag = bookService.save(book); // r.setFlag(flag); return new R(bookService.save(book)); } @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, int pageSize) { return new R(true, bookService.getPage(currentPage, pageSize)); } }