实战:一
关于使用MP service及其实现类的规范
- 我们一般将service声明为接口,然后编写一个实现类来实现接口。
- MP对service接口及其实现类提供了支持
//这是一个service接口
@Service
public interface HospitalSetService extends IService<HospitalSet> {
//让service接口继承Iservice,其中泛型为相对应的实体类
}
//这是一个接口的实现类
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet>
implements HospitalSetService {
//在实现原接口的基础上,继承了ServiceImpl这个类,其中泛型1为相对应的mapper接口,泛型2为对应的实体类。
//在ServiceImpl中已经将相对应的Mapper注入,因此不需要使用@Autowired注入。
}
Tips:@Mapper和MapperScan只要有一个就行了。
其中MapperScan只要指定到包,不得指定到类。
swagger2介绍
编写和维护接口文档是每个程序员的职责,根据Swagger2可以快速帮助我们编写最新的API接口文档,再也不用担心开会前仍忙于整理各种资料了,间接提升了团队开发的沟通效率。
-
引入依赖
<!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency>
-
编写一个配置类
@Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket webApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("webApi") .apiInfo(webApiInfo()) .select() //只显示api路径下的页面 .paths(Predicates.and(PathSelectors.regex("/api/.*"))) .build(); } @Bean public Docket adminApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("adminApi") .apiInfo(adminApiInfo()) .select() //只显示admin路径下的页面 .paths(Predicates.and(PathSelectors.regex("/admin/.*"))) .build(); } private ApiInfo webApiInfo(){ return new ApiInfoBuilder() .title("网站-API文档") .description("本文档描述了网站微服务接口定义") .version("1.0") .contact(new Contact("atguigu", "http://atguigu.com", "493211102@qq.com")) .build(); } private ApiInfo adminApiInfo(){ return new ApiInfoBuilder() .title("后台管理系统-API文档") .description("本文档描述了后台管理系统微服务接口定义") .version("1.0") .contact(new Contact("atguigu", "http://atguigu.com", "49321112@qq.com")) .build(); } }
-
将模块引入
<dependency> <groupId>com.boerk</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
-
配置扫描包
由于隶属于不同的模块,所以需要按照相同部分进行扫描
@ComponentScan(basePackages = "com.boerk") //扫描以com.boerk开头的包及其子包。已经将common模块引入进此模块了
Swagger的一些注解
注解名 描述 @Api: 修饰整个类,描述Controller的作用 @ApiOperation: 描述一个类的一个方法,或者说一个接口 @ApiParam: 单个参数描述 @ApiModel: 用对象来接收参数 @ApiModelProperty: 用对象接收参数时,描述对象的一个字段 @ApiImplicitParam: 一个请求参数 @ApiImplicitParams: 多个请求参数 -