引入依赖
<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>