springboot整合swagger2接口文档和mybatis-plus
使用mybatis-plus对员工表的增删改查
项目结构
jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
注意:
springboot的版本号最新版本swagger会出现问题
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
mabatis最新版本加上
@Configuration
public class MybatisPlusConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
swagger
@Configuration
public class Swagger {
//获取Swagger2的实例对象Docket。
@Bean
public Docket getDocket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("143").apiInfo(apiInfo())
.select()
//为指定的包生成api文档
.apis(RequestHandlerSelectors.basePackage("com.ccr.controller"))
//为指定的路径生成api文档
//.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo apiInfo(){
Contact DEFAULT_CONTACT = new Contact("程哈哈", "http://www.test.com", "2665488005@qq.com");
ApiInfo apiInfo = new ApiInfo("员工管理", "员工管理", "2.0",
"http://www.test.com",
DEFAULT_CONTACT, "test", "http://www.test.com", new ArrayList<VendorExtension>());
return apiInfo;
}
}
启动类
@SpringBootApplication
@MapperScan(basePackages = "com.ccr.dao")
@EnableSwagger2 //开启swagger
public class SpringbootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
}
}
CommonResult
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "状态码")
public class CommonResult {
@ApiModelProperty(value = "返回的状态码200成功")
private Integer code;
@ApiModelProperty(value = "提示信息")
private String msg;
@ApiModelProperty(value = "返回的数据")
private Object data;
}
EmpService
public interface EmpService {
// 根据id查询信息
public CommonResult selectById(Long empId );
// 查询所有分页
public CommonResult selectByPage(Integer page,Integer limit);
// 添加信息
public CommonResult addEmp(Emp emp);
// 修改
public CommonResult upEmp(Emp emp);
// 删除
public CommonResult delEmp(Long empId);
}
EmpServiceImpl
@Service(value = "empService")
public class EmpServiceImpl implements EmpService {
@Resource
public EmpDao empDao;
@Override
public CommonResult selectById(Long empId) {
Emp emp = empDao.selectById(empId);
return new CommonResult(200,"查询成功",emp);
}
@Override
public CommonResult selectByPage(Integer page, Integer limit) {
final Page<Emp> empPage = new Page<>(page,limit);
final Page<Emp> empPage1 = empDao.selectPage(empPage, null);
return new CommonResult(200,"查询成功",empPage1);
}
// 添加
@Override
public CommonResult addEmp(Emp emp) {
int insert = empDao.insert(emp);
return new CommonResult(200,"添加成功",insert);
}
@Override
public CommonResult upEmp(Emp emp) {
final int i = empDao.updateById(emp);
return new CommonResult(200,"更新",i);
}
@Override
public CommonResult delEmp(Long empId) {
final int i = empDao.deleteById(empId);
return new CommonResult(200,"删除成功",i);
}
}
Emp
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tbl_emp") //表名和实体类不同时
@ApiModel(value = "员工表")//实体类说明
public class Emp {
// 实体类中的属性
@ApiModelProperty(value = "员工id编号")
@TableId(value = "empId", type = IdType.ASSIGN_ID)
private Long empId;
@ApiModelProperty(value = "员工姓名")
@TableField(value = "empName")
private String empName;
@ApiModelProperty(value = "员工性别")
private char gender;
@ApiModelProperty("员工邮箱")
private String email;
@ApiModelProperty("员工所在的部门编号")
@TableField(value = "d_Id")
private Integer dId;
}
EmpDao
public interface EmpDao extends BaseMapper<Emp> {
}
EmpController
@RestController
@RequestMapping("emp")
@Api(tags = "员工表的增删改")
public class EmpController {
@Autowired
private EmpService empService;
// 根据id查询
@GetMapping("/getById")
@ApiOperation(value = "根据id查询员工信息")
@ApiImplicitParams(
@ApiImplicitParam (name = "empId",value = "员工id号",dataType = "Integer" ,required = true)
)
public CommonResult getById(Long empId){
return empService.selectById(empId);
}
// 查询所有信息分页
@GetMapping("/getAll")
@ApiOperation(value = "查询所有分页")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", required = true),
@ApiImplicitParam(name = "limit", value = "一页条数", dataType = "Integer", required = true)
})
public CommonResult getAll(Integer page,Integer limit){
return empService.selectByPage(page,limit);
}
// 添加
@PostMapping("/addEmp")
@ApiOperation(value = "添加员工")
@ApiImplicitParams({
@ApiImplicitParam(name = "empId", value = "员工id",required = false,paramType = "添加",dataType="Long"),
@ApiImplicitParam(name = "empName", value = "员工姓名",required = true,paramType = "添加",dataType="String"),
@ApiImplicitParam(name = "gender", value = "员工性别",required = true,paramType = "添加",dataType = "char"),
@ApiImplicitParam(name = "email", value = "邮箱",required = true,paramType = "添加",dataType = "String"),
@ApiImplicitParam(name = "dId", value = "部门",required = true,paramType = "添加",dataType = "Integer"),
})
public CommonResult addEmp(Long empId,String empName,char gender,String email,Integer dId){
Emp emp = new Emp(empId,empName,gender,email,dId);
// System.out.println(emp);
return empService.addEmp(emp);
}
// 修改
@PutMapping("upEmp")
@ApiOperation(value = "修改员工")
@ApiImplicitParams({
@ApiImplicitParam(name = "empId", value = "员工id",required = true,paramType = "修改",dataType="Long"),
@ApiImplicitParam(name = "empName", value = "员工姓名",required = true,paramType = "修改",dataType="String"),
@ApiImplicitParam(name = "gender", value = "员工性别",required = true,paramType = "修改",dataType = "char"),
@ApiImplicitParam(name = "email", value = "邮箱",required = true,paramType = "修改",dataType = "String"),
@ApiImplicitParam(name = "dId", value = "部门",required = true,paramType = "修改",dataType = "Integer"),
})
public CommonResult upEmp(Long empId,String empName,char gender,String email,Integer dId){
Emp emp = new Emp(empId, empName, gender, email, dId);
return empService.upEmp(emp);
}
// 删除
@DeleteMapping("delEmp")
@ApiOperation(value = "删除员工")
@ApiImplicitParams(
@ApiImplicitParam(name = "empId", value = "员工id",required = true,paramType = "删除",dataType = "Long"))
public CommonResult delEmp(Long empId){
return empService.delEmp(empId);
}
}
applicatication.yml
#配置端口号
server:
port: 8080
# 配置druid的信息
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/emp?serverTimezone=Asia/Shanghai
username: root
password: root
max-active: 20
initial-size: 5
min-idle: 5
max-wait: 3000
logging:
level:
com.ccr.dao: debug
pagehelper:
reasonable: true
运行结果