依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
dao:
继承JpaRepository
swagger
依赖
<!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency>
配置
#修改tomcat启动端口号 server.port=8081 #修改项目的部署路径 server.context-path=/bookstore #修改日志级别 logging.level.root=ERROR #热部署触发文件 spring.devtools.restart.trigger-file=.trigger #mysql配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/bookstore1716?useUnicode=true&characterEncoding=UTF-8 #jp配置 spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
swagger的配置类
package com.qfedu.bootdemo.config; import com.google.common.base.Predicates; 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.service.ApiInfo; 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 apiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("myapi") .apiInfo(apiInfo()) .useDefaultResponseMessages(false) .select() .paths(Predicates.or(PathSelectors.regex("/api2/.*"))) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("BookStore Platform API")//大标题 .description("BookStore Platform's REST API, all the applications could access the Object model data via JSON.")//详细描述 .version("2.0")//版本 .contact(new Contact("java", "http://itqf.com", "123456@qq.com"))//作者 .license("The Apache License, Version 2.0")//许可证信息 .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")//许可证地址 .build(); } }
访问
http://localhost:8088/savestore/swagger-ui.html