目录
一、SSMP整合案例
1.1 项目搭建
1.1.1 模块搭建
①勾上需要使用的技术
②需要手动导入的依赖
<!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.11</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
③修改配置文件为yml格式,并且设置端口号
server: port: 80
1.2 实体类开发
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; }
1.3 数据层开发
1.3.1 数据层开发——分页功能
@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; } }
1.3.2 数据层开发——条件查询功能
1.4 业务层开发
1.4.1 业务层快速开发
1.5 表现层开发
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 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.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 IPage<Book> getPage(@PathVariable int currentPage, int pageSize) { return bookService.getPage(currentPage,pageSize); } }
1.5.1 表现层消息一致性处理
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)); } }