1.前后端分离的一个常用的文档接口swaggerui越来越受欢迎,方便了前端以及后端人员的测试
2.如下为springboot整合swagger和mybatispus案例的github地址:https://github.com/baisul/springbootswaggermybatisplus.git
3.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<!--代码生成器依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<!--velocity模板引擎依赖-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<!--freemarker模板引擎依赖-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.application.properties
spring.application.name=demo
#配置端口号以及访问路径
server.port=8080
server.servlet.context-path=/demo
#数据源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatisplus配置
#开启驼峰命名法
mybatis-plus.configuration.map-underscore-to-camel-case=true
#扫描xml文件
mybatis-plus.mapper-locations=classpath:mapper/*.xml
5.项目结构
6.entity
package com.yl.demo.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author wfj
* @since 2020-11-12
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Student implements Serializable {
private static final long serialVersionUID=1L;
/**
* id
*/
@TableId(value="id",type= IdType.AUTO)
private Integer id;
/**
* 姓名
*/
private String sname;
/**
* 学号
*/
private String sno;
/**
* 性别,0男,1女
*/
private Integer sex;
/**
* 爱好
*/
private String hobby;
/**
* 邮箱
*/
private String email;
}
package com.yl.demo.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author wfj
* @since 2020-11-12
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Teacher implements Serializable {
private static final long serialVersionUID=1L;
/**
* id
*/
@TableId(value="id",type= IdType.AUTO)
private Integer id;
/**
* 姓名
*/
private String tname;
/**
* 工号
*/
private String tno;
/**
* 性别
*/
private Integer sex;
/**
* 教的课程
*/
private String cource;
}
7.service
package com.yl.demo.service;
import com.yl.demo.entity.Student;
import io.swagger.models.auth.In;
public interface StudentServie {
Student getStudentById(Integer id);
Integer addStudent(Student student);
Integer deleteStudentById(Integer id);
Integer updateStudent(Student student);
}
package com.yl.demo.service;
import com.yl.demo.dto.QueryDto;
import com.yl.demo.dto.TeacherDto;
import com.yl.demo.entity.Teacher;
import java.util.List;
public interface TeacherService {
List<Teacher> getTeaches(QueryDto queryDto);
}
8.service.impl
package com.yl.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yl.demo.dao.StudentMapper;
import com.yl.demo.dao.TeacherMapper;
import com.yl.demo.entity.Student;
import com.yl.demo.entity.Teacher;
import com.yl.demo.service.StudentServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentServie {
@Autowired
private StudentMapper studentMapper;
@Override
public Student getStudentById(Integer id) {
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.eq("id",id);
return studentMapper.selectList(wrapper).get(0);
}
@Override
public Integer addStudent(Student student) {
return studentMapper.insert(student);
}
@Override
public Integer deleteStudentById(Integer id) {
return studentMapper.deleteById(id);
}
@Override
public Integer updateStudent(Student student) {
return studentMapper.updateById(student);
}
}
package com.yl.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yl.demo.dao.TeacherMapper;
import com.yl.demo.dto.QueryDto;
import com.yl.demo.dto.TeacherDto;
import com.yl.demo.entity.Teacher;
import com.yl.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TeacherServiceImpl implements TeacherService {
@Autowired
private TeacherMapper teacherMapper;
@Override
public List<Teacher> getTeaches(QueryDto queryDto) {
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.like("tname","小");
wrapper.eq("sex",0);
return teacherMapper.selectList(wrapper);
}
}
9.dao
package com.yl.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yl.demo.entity.Student;
public interface StudentMapper extends BaseMapper<Student> {
}
package com.yl.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yl.demo.entity.Teacher;
public interface TeacherMapper extends BaseMapper<Teacher> {
}
10.dto
package com.yl.demo.dto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class StudentDto implements Serializable {
/**
* id
*/
@ApiModelProperty(value = "主键id")
private Integer id;
/**
* 姓名
*/
@ApiModelProperty(value = "姓名")
private String sname;
/**
* 学号
*/
@ApiModelProperty(value = "学号")
private String sno;
/**
* 性别,0男,1女
*/
@ApiModelProperty(value = "性别,0男,1女")
private Integer sex;
/**
* 爱好
*/
@ApiModelProperty(value = "爱好")
private String hobby;
/**
* 邮箱
*/
@ApiModelProperty(value = "邮箱")
private String email;
}
package com.yl.demo.dto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class TeacherDto implements Serializable {
/**
* id
*/
@ApiModelProperty(value = "主键id")
private Integer id;
/**
* 姓名
*/
@ApiModelProperty(value = "姓名")
private String tname;
/**
* 工号
*/
@ApiModelProperty(value = "工号")
private String tno;
/**
* 性别
*/
@ApiModelProperty(value = "性别")
private Integer sex;
/**
* 教的课程
*/
@ApiModelProperty(value = "教的课程")
private String cource;
}
package com.yl.demo.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class QueryDto implements Serializable {
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "性别")
private Integer sex;
}
11.util
package com.yl.demo.util;
import com.baomidou.mybatisplus.extension.api.R;
import lombok.Data;
import java.io.Serializable;
/**
* 通用结果返回类
*/
@Data
public class Result implements Serializable {
public int code;
public String msg;
public Object data;
public static Result success() {
Result result = new Result();
result.setCode(200);
result.setMsg("操作成功");
return result;
}
public static Result success(int code,String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
public static Result success(int code,String msg,Object data) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
result.setData(data);
return result;
}
public static Result success(Object data) {
Result result = new Result();
result.setCode(200);
result.setMsg("操作成功");
result.setData(data);
return result;
}
public static Result fail() {
Result result = new Result();
result.setCode(0);
result.setMsg("操作失败");
return result;
}
public static Result fail(int code,String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
public static Result fail(int code,String msg,Object data) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
result.setData(data);
return result;
}
}
12.controller
package com.yl.demo.controller;
import com.yl.demo.dto.StudentDto;
import com.yl.demo.entity.Student;
import com.yl.demo.service.StudentServie;
import com.yl.demo.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <p>
* 学生控制器
* </p>
*
* @author wfj
* @since 2020-11-12
*/
@RestController
@RequestMapping("/student")
@Api(value = "学生控制器")
public class StudentController {
@Autowired
private StudentServie studentServie;
/**
* 根据id获取学生
* @return
*/
@GetMapping("/getStudentById")
public Result getStudentById(@RequestParam Integer id) {
Student student = studentServie.getStudentById(id);
if (student != null) {
return Result.success(200,"查找成功",student);
} else {
return Result.fail();
}
}
/**
* 添加学生
* @return
*/
@PostMapping("/addStudent")
public Result addStudent(@RequestBody StudentDto studentDto) {
Student student = new Student();
BeanUtils.copyProperties(studentDto,student);
Integer result = studentServie.addStudent(student);
if (result > 0) {
return Result.success();
} else {
return Result.fail();
}
}
/**
* 根据学生ID删除学生
* @param id
* @return
*/
@DeleteMapping("/deleteStudentById")
public Result deleteStudentById(@RequestParam Integer id) {
Integer result = studentServie.deleteStudentById(id);
if (result > 0) {
return Result.success();
} else {
return Result.fail();
}
}
/**
* 修改学生
* @return
*/
@PutMapping("/updateStudent")
public Result updateStudent(@RequestBody StudentDto studentDto) {
Student student = new Student();
BeanUtils.copyProperties(studentDto,student);
Integer result = studentServie.updateStudent(student);
if (result > 0) {
return Result.success();
} else {
return Result.fail();
}
}
}
package com.yl.demo.controller;
import com.yl.demo.dto.QueryDto;
import com.yl.demo.entity.Teacher;
import com.yl.demo.service.TeacherService;
import com.yl.demo.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* <p>
* 老师控制器
* </p>
*
* @author wfj
* @since 2020-11-12
*/
@RestController
@RequestMapping("/teacher")
@Api(value = "老师控制器")
public class TeacherController {
@Autowired
private TeacherService teacherService;
/**
* 模糊查询姓名带有“小”字并且sex为0的老师列表
*/
@PostMapping("/queryTeachers")
public Result queyTeachers(@RequestBody QueryDto queryDto) {
List<Teacher> teaches = teacherService.getTeaches(queryDto);
if (teaches != null) {
return Result.success(200,"查找成功",teaches);
} else {
return Result.fail(0,"暂无数据",null);
}
}
}
13.config,swagger的配置类
package com.yl.demo.config;
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.yl.demo.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot整合Swagger+mybatispus")
.description("SpringBoot整合Swagger+mybatisplus,信息")
.version("1.0")
.contact(new Contact("mycsdn","blog.csdn.net","baisul@gmail.com"))
.license("baidu")
.licenseUrl("http://www.baidu.com")
.build());
}
}
14.启动主程序
15.打开浏览器,输入http://localhost:8080/demo/swagger-ui.html
16.post请求
17.get请求
18.delete请求
19.put请求
20.模糊查询
21.至此,一个springboot整合swagger+mybatisplus的入门案例已完成。