SpringBoot+Mybatis+swagger2
Straight to the point, directly on the course of it, with the idea of development tools
step
SpringBoot create a project
to add some dependence
After you create a complete look at the structure of the code, and there are some of my own new
Look pom file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cheny.demo</groupId>
<artifactId>crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>crud</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Because more accustomed to using yml this configuration file, so I application.properties configuration file to re-create a deleted
Configuration as follows, including selecting the configuration profile, the data source, like mapping file MyBatis
spring:
profiles:
active: dev
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: ok
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
tomcat:
initialSize: 1
min-idle: 1
max-idle: 20
max-wait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
logging:
file: logs/demo.log
mybatis:
mapper-locations: classpath:mybatis/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.cheny.demo # 注意:对应实体类的路径
Because here are set up multiple development environments and production environments, as specified in application.yml environment for the development environment, so then the development environment configuration files posted, there is nothing to configure, just set up a port
application- dev.yml
server:
port: 8080
Like writing Dao, Mapper and drink SSM Service
Here is not showing
Since we used here so swagger2 also be configured to swagger2
First, the pom have respective dependent
Then create a configuration class Swagger2 under the same directory and then start classes springboot of
Swagger2.java
package com.cheny.demo;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Created by chenyong on 2018/11/15 0015.
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.cheny.demo.web"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
//创建人
.contact(new Contact("MarryFeng", "http://www.baidu.com", ""))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
In the controller for each layer is also used to describe the operation of memory is described in detail in swagger
package com.cheny.demo.web;
import com.cheny.demo.service.PersonService;
import com.cheny.demo.service.VO.PersonVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by chenyong on 2018/11/15 0015.
*/
@RestController
@RequestMapping("/person")
@Api("person测试controller")
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping(value = "/queryList", method = RequestMethod.GET)
@ApiOperation(value = "查询列表")
public ActionResult queryList(){
return new ActionResult(ErrorCode.Success, personService.queryList());
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ApiOperation(value = "新增")
public ActionResult add(PersonVo personVo){
personService.save(personVo);
return new ActionResult();
}
@RequestMapping(value = "/update", method = RequestMethod.PUT)
@ApiOperation(value = "修改")
public ActionResult update(PersonVo personVo){
personService.update(personVo);
return new ActionResult();
}
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
@ApiOperation(value = "删除")
public ActionResult delete(String id){
personService.delete(id);
return new ActionResult();
}
@RequestMapping(value = "/getObjById", method = RequestMethod.GET)
@ApiOperation(value = "根据id查询")
public ActionResult getObjById(String id){
return new ActionResult(ErrorCode.Success, personService.getObjById(id));
}
}
Start the project
address bar, enter http: // localhost: 8080 / swagger -ui.html enter Swagger
Fill the corresponding parameters in the respective methods may be tested directly or more convenient