使用SpirngBoot+mybatisplus快速搭建接口架构
一:创建一个SpringBoot工程,引用lombok,mysql,web服务
二:准备数据库表:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tbl_book
-- ----------------------------
DROP TABLE IF EXISTS `tbl_book`;
CREATE TABLE `tbl_book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tbl_book
-- ----------------------------
INSERT INTO `tbl_book` VALUES (1, '计算机理论', 'Spring实战 第5版', 'Spring入门经典教程,深入理解Spring原理技术内幕');
INSERT INTO `tbl_book` VALUES (2, '计算机理论', 'Spring 5核心原理与30个类手写实战', '十年沉淀之作,手写Spring精华思想');
INSERT INTO `tbl_book` VALUES (3, '计算机理论', 'Spring 5设计模式', '全方位解析面向Web应用的轻量级框架,带你成为Spring MVC开发高手');
INSERT INTO `tbl_book` VALUES (4, '计算机理论', 'Spring MVC+MyBatis开发从入门到项目实战', '源码级剖析Spring框架,适合已掌握Java基础的读者');
INSERT INTO `tbl_book` VALUES (5, '计算机理论', '轻量级Java Web企业应用实战', '源码级剖析Spring框架,适合已掌握Java基础的读者');
INSERT INTO `tbl_book` VALUES (6, '计算机理论', 'Java核心技术卷I基础知识(原书第11版)', 'Core Java第11版,Jolt大奖获奖作品,针对Java SE9、10、11全面更新');
INSERT INTO `tbl_book` VALUES (7, '计算机理论', '深入理解Java虚拟机', '5个维度全面剖析JVM,大厂面试知识点全覆盖');
INSERT INTO `tbl_book` VALUES (8, '计算机理论', 'Java编程思想(第4版)', 'Java学习必读经典,殿堂级著作!赢得了全球程序员的广泛赞誉');
INSERT INTO `tbl_book` VALUES (9, '计算机理论', '零基础学Java(全彩版)', '零基础自学编程的入门图书,由浅入深,详解Java语言的编程思想和核心技术');
INSERT INTO `tbl_book` VALUES (10, '市场营销', '直播就该这么做:主播高效沟通实战指南', '李子染、李佳琦、薇娅成长为网红的秘密都在书中');
INSERT INTO `tbl_book` VALUES (11, '市场营销', '直播销讲实战—本通', '和秋叶—起学系列网络营销书籍');
INSERT INTO `tbl_book` VALUES (12, '市场营销', '直播带货:淘宝、天猫直播从新手到高手', '—本教你如何玩转直播的书,10堂课轻松实现带货月入3W+');
SET FOREIGN_KEY_CHECKS = 1;
三:引入mybatis-plus依赖与druid依赖
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
四:编写配置文件application.yml:
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: root
password: XXX #数据库密码
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_ #全局设置表前缀
id-type: auto #全局设置id自增
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启SQL日志
五:新建pojo包,编写实体类,字段名称与表名称对应
package com.lrc.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
/**
* @param
* @author lrc
* @create 2022/4/5
* @return
* @description
**/
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
六:新建mapper包,编写BookMapper接口继承mybatisplus的BaseMapper
package com.lrc.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lrc.pojo.Book;
import org.apache.ibatis.annotations.Mapper;
/**
* @param
* @author lrc
* @create 2022/4/5
* @return
* @description
**/
@Mapper
public interface BookMapper extends BaseMapper<Book> {
}
七:新建config包,编写mybatisplus的分页拦截器。
package com.lrc.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @param
* @author lrc
* @create 2022/4/5
* @return
* @description
**/
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//创建乐观锁拦截器 OptimisticLockerInnerInterceptor
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); //插件分页拦截器,我的是mysql
return mybatisPlusInterceptor;
}
}
八、新建service包,编写service接口继承mybatispuls的IService
package com.lrc.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lrc.pojo.Book;
/**
* @param
* @author lrc
* @create 2022/4/5
* @return
* @description
**/
public interface IBookService extends IService<Book> {
//在mybatisplus提供的默认方法之外,定义自己的方法
IPage<Book> getPageBook(int currentPage,int size);
}
九、在service包下再新建一个impl实现类包,编写我们IBookServiceImpl实现类,并继承mybatisplus的ServiceImpl类
package com.lrc.service.impl;
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.lrc.pojo.Book;
import com.lrc.mapper.BookMapper;
import com.lrc.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @param
* @author lrc
* @create 2022/4/5
* @return
* @description
**/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
@Autowired
private BookMapper bookMapper;
//增强自定义的方法实现
@Override
public IPage<Book> getPageBook(int currentPage, int size) {
IPage page=new Page(currentPage,size);
bookMapper.selectPage(page,null);
return page;
}
}
十:新建一个controller包,编写接口请求控制代码
package com.lrc.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lrc.pojo.Book;
import com.lrc.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @param
* @author lrc
* @create 2022/4/5
* @return
* @description
**/
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
@GetMapping("/getById")
public Book getById(@RequestParam Integer id){
return bookService.getById(id);
}
@GetMapping(value = "/getPageList")
public IPage<Book> getByPage(@RequestParam int currentPage, @RequestParam int size){
return bookService.getPageBook(currentPage,size);
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@DeleteMapping("/deleteById")
public Boolean delete(@RequestParam Integer id){
return bookService.removeById(id);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.updateById(book);
}
}
自此,接口编写练习的模型已经编写完成,框架目录结构:
下面启动项目后在postman进行测试:
1、查询全量数据:
2、根据ID查数据
3、根据分页查数据
4、添加数据
5、更新数据
6、删除数据