SpringBoot+MybatisPlus项目后台接口编写完整流程以及集成swagger
1、创建SpringBoot项目
比较简单,所以不再阐述
2、引入需要的依赖包
有些注释可以在创建SpringBoot项目的时候勾选:如下图
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--mysql-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!--mybatis_plus-->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
3、yml配置文件
4、在idea中连接数据库
选择数据库,这里使用的是mysql:
填写相关参数:
5、生成实体类
借助idea中连接好的数据库生成需要的实体类:
在表名上右键,不然就不会生成实体类,然后根据弹出框选择实体类存放的目录:
6、编写代码
controller层:
package com.example.blog.controller;
import com.example.blog.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "用户管理") // tags:你可以当作是这个组的名字。 //这里不懂可以继续往下看,后文有讲解
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
public UserService userService;
/**
* 获取所有用户列表
*/
@ApiOperation(value = "获取所有用户")//这里不懂可以继续往下看,后文有讲解
@GetMapping("/allUser")
public List<UserDto> allUser(){
List<UserDto> userList = userService.allUser();
System.out.printf("查询到所有用户:"+userList.toString());
return userList;
}
}
service层:
- IService:MyBatis-Plus 通用的接口(IService接口和BaseMapper接口作用差不多,最大的区别就是IService接口有批处理操作)
- service层接口可以继承IService接口,IService的使用(需要另外两个接口baseMapper和ServiceImpl的配合)
使用这些接口可以直接用mapper和service在controller层调用mybatisplus提供的一些处理数据的接口:
情况一:返回给前台要显示的数据和实体类中一样
在controller曾可以直接使用userService或者userMapper调用mybatisplus提供的接口进行查询等操作,这时候service和mapper层不需要编写任何代码
情况二:返回给前台要显示的数据和实体类中不一样,需要多表联查等
比如本文的密码等一些属性不能显示,这时就可以重新定义一个类如文章中出现的 UserDto,然后就是常规操作编写相应的 controller --> service -->mapper)。
具体操作代码如下:
//service接口
package com.example.blog.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.blog.dto.UserDto;
import java.util.List;
public interface UserService extends IService<User> {
List<UserDto> allUser();
}
- service接口的实现类继承ServiceImpl<继承BaseMapper的mapper,实体类>,实现UserService接口:
//service实现类
package com.example.blog.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.dto.UserDto;
import com.example.blog.mapper.UserMapper;
import com.example.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
@Autowired
public UserMapper userMapper;
@Override
public List<UserDto> allUser() {
return userMapper.allUser();
}
}
- mapper层的接口继承BaseMapper接口:
由于sql语句过于简单,所以直接在UserMapper接口中的方法上使用sql注解,无需在xml中写sql。
复杂的SQL语句可以在xml中编写,这时接口方法名必须和xml中sql语句的id值一样。
package com.example.blog.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.blog.dto.UserDto;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("select id,username from user")
List<UserDto> allUser();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.blog.mapper.UserMapper">
</mapper>
💖接下来就可以输入访问地址测试啦!!!
💖由于是前后端分离项目,后端提供接口的时候还需提供接口文档说明,这时候就可以借助 swagger 了。Swagger 是一个用于生成、描述、调用和可视化 RESTful 风格的 Web 服务的接口文档。具体可以百度自行了解,毕竟博主也是小白,哈哈哈ヾ(•ω•`)o
7、集成swagger
引入swagger依赖
旧版和新版
<!--swagger旧版本-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger新版本-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-boot-starter</artifactId>-->
<!-- <version>3.0.0</version>-->
<!-- </dependency>-->
配置swagger
- @Configuration : 标明是配置类
- @EnableSwagger2
开启旧版本swagger功能:http://localhost:自己设定的端口号,默认8080/swagger-ui.html - @EnableOpenApi
开启新版本swagger功能 :http://localhost:自己设定的端口号,默认8080/swagger-ui/ - @Api(tags = “用户管理”)
controller类上的注解,tags:你可以当作是这个接口组的名字。 - @ApiOperation(value = “获取所有用户”,notes = “描述信息”)
controller类中方法上的注解,说明方法名,描述作用等
swagger的其余注解还需小伙伴自行查阅了解,配置和注解的作用可以在页面上会展现出来,可以对比看看。
//Swagger使用的配置文件
package com.example.blog.config;
//import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger使用的配置文件
*/
@Configuration // 标明是配置类
@EnableSwagger2 //开启旧版本swagger功能 :http://localhost:9999/swagger-ui.html
//@EnableOpenApi //开启新版本swagger功能 :http://localhost:9999/swagger-ui/
//@EnableSwaggerBootstrapUI // 开启SwaggerBootstrapUI,访问API:http://localhost:9999/doc.html,即可预览到基于bootstarp的Swagger UI界面
public class SwaggerConfig {
/**
* apiInfo:api基本信息的配置,用于定义API主界面的信息,比如可以声明所有的API的总标题、描述、版本
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("博客系统API文档")//可以用来自定义API的主标题
.description("博客系统相关接口的文档")//可以用来描述整体的API
.termsOfServiceUrl("http://localhost:9999/")//服务条款网址,这儿博主也不明白是干啥用的,可以自行去搜索一下swagger配置类说明
.version("1.0")//版本
.contact(new Contact("作者", "作者URL", "作者Email"))
.build();
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // DocumentationType.SWAGGER_2 固定的,代表swagger2
// .groupName("分布式任务系统") // 如果配置多个文档的时候,那么需要配置groupName来分组标识
.apiInfo(apiInfo()) // 用于生成API信息
.select() // select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档 .apis(RequestHandlerSelectors.basePackage("com.example.blog.controller")) // 用于指定扫描哪个包下的接口
.paths(PathSelectors.any())// 选择所有的API,如果你想只为部分API生成文档,可以配置这里
.build();
}
}
💖配置完成,可以访问了,根据自己是新版还是旧版输入地址访问,前面已给出访问地址
swagger界面操作就自行解决吧ヾ(•ω•`)o
swagger的页面增强版
由于swagger默认ui界面不是特别美观,现在引入SwaggerBootstrapUI,增强版的SwaggerUI界面。
添加依赖
在原有基础上添加swagger-bootstrap-ui依赖
<!-- 引入swagger-bootstrap-ui依赖包-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.7</version>
</dependency>
添加注解
SwaggerConfig类上添加注解@EnableSwaggerBootstrapUI
- @EnableSwaggerBootstrapUI
用来开启SwaggerBootstrapUI,
访问地址:http://localhost:自己设定的端口号,默认8080/doc.html,即可预览到基于bootstarp的SwaggerUI界面
比刚刚美观舒服多了吧,嘻嘻嘻(╹ڡ╹ )
💌 文章到这儿就结束啦!!!( ゚д゚)つBye,如果文章中有错误不当的地方,还请各位多多指正。
💌 后端项目打包以及部署服务器,请移步下篇文章。