springboot增删改查项目(二)配置Swagger2接口文档

引入依赖

<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>

创建一个config/SwaggerConfig

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author swagger
 */
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.df"))//修改包名
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Xpt API文档")
                .description("power by swagger")
                .termsOfServiceUrl("https://www.zhai78.com")
                .version("1.0")
                .build();
    }
}

在项目的启动程序上加上注释@EnableSwagger2

@SpringBootApplication
@MapperScan("com.df.dao")
@EnableSwagger2
public class TestdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestdemoApplication.class, args);
    }
}

启动程序,访问swagger-ui

http://localhost:8803/swagger-ui.html

默认是有一个Customers Controller,之前用EasyCode自动生成的

之前插入过一条的,可以测试一下id=1的情况,接着会报错,问题出在mapper/CustomesDao.xml表名那里修改一下加反单引号

<!--查询单个-->
<select id="queryById" resultMap="BaseResultMap">
        select
          id, name, sex, tell, addr
        from `customers`
        where id = #{id}
</select>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容