1.我们在eclipse安装mybatis generator插件
第一步:打开Help=>Install New Software
第二步:打开Install窗口后,点击Work with后的Add…按钮,location输入插件的网址,name自己输入个名字,好了之后点击OK按钮
插件最新的联网地址是:https://dl.bintray.com/mybatis/mybatis-generator/
第三步:点击OK按钮后,Install窗口的列表会进入Pending状态,需要稍等片刻,如果成功了,列表会出现MyBatis Generator,勾选上,点击next进入配置,这个环节需要花费比较长的时间,需要耐心等待
2.安装成功后我们可以创建一个文件
直接点击“Next”->然后点击“Finsh”就会发现生产一个“generatorConfig.xml”文件,我们可以配置我们的数据项目
3.配置数据库信息并且生成代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="spring-boot-mybatis-swagger" targetRuntime="MyBatis3"
defaultModelType="flat">
<commentGenerator>
<property name="suppressDate" value="false" />
<!-- 下面一行用于取消注释的生成 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection
connectionURL="jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai"
driverClass="com.mysql.cj.jdbc.Driver" password="root123"
userId="root">
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<javaModelGenerator
targetPackage="com.qjc.entry"
targetProject="spring-boot-mybatis-swagger/src/main/java" />
<sqlMapGenerator targetPackage="mapper"
targetProject="spring-boot-mybatis-swagger/src/main/resources" />
<javaClientGenerator
targetPackage="com.qjc.mapper"
targetProject="spring-boot-mybatis-swagger/src/main/java" type="XMLMAPPER" />
<table schema="springboot" tableName="user_info"
domainObjectName="UserInfo" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="true" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
生成后的项目结构:
由此我们通过“mybatis generator”插件反向生成结束。
4.我们开始集成swaag2包
一、依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.qjc</groupId>
<artifactId>spring-boot-mybatis-swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-mybatis-swagger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- tomcat启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JUnit test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- swagger2 集成 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、Swagger配置类
其实这个配置类,只要了解具体能配置哪些东西就好了,毕竟这个东西配置一次之后就不用再动了。 特别要注意的是里面配置了api文件也就是controller包的路径,不然生成的文档扫描不到接口。
如果需要生产的时候禁用:
禁用方法1:使用注解@Profile({“dev”,“test”}) 表示在开发或测试环境开启,而在生产关闭。(推荐使用)
禁用方法2:使用注解@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭Swagger.
package com.qjc.swagger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author qjc
* @date 2019年1月24日下午4:12:03
* @version 1.0.0
*/
@Configuration
@EnableSwagger2
//@Profile({"dev","test"})
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2 {
// swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.qjc.controller"))
.paths(PathSelectors.any()).build();
}
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
// 创建人
.contact(new springfox.documentation.service.Contact("qjc", "https://mp.csdn.net/mdeditor/86628875#", "joecheng521@163.com"))
// 版本号
.version("1.0")
// 描述
.description("API 描述").build();
}
}
Application.class 加上注解@EnableTransactionManagement 表示开启事务
package com.qjc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@MapperScan("com.qjc.mapper")
@EnableTransactionManagement
public class SpringBootMybatisSwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisSwaggerApplication.class, args);
}
}
三、Restful 接口
package com.qjc.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.qjc.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
/**
* @author qjc
* @date 2019年1月24日下午4:17:22
* @version 1.0.0
*/
@Api("swaggerDemoController相关的api")
@RestController
public class SwaggerDemoController {
private static final Logger logger = LoggerFactory.getLogger(SwaggerDemoController.class);
@Autowired
IUserInfoService iUserInfoService;
@ApiOperation(value = "根据id查询用户信息", notes = "查询数据库中某个的用户信息")
@ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "Long")
@GetMapping("/user/{id}")
public Object getUserinfo(@PathVariable Long id) {
logger.info("开始查询某个用户信息");
return iUserInfoService.findUserInfoById(id);
}
}
四、Swagger2文档
启动项目“spring-boot-mybatis-swagger”
访问 http://localhost:8081/spring-boot-mybatis-swagger/swagger-ui.html
五、Swagger注解
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
5.项目的pom文件
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.qjc</groupId>
<artifactId>spring-boot-mybatis-swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-mybatis-swagger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- tomcat启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JUnit Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- swagger2 集成 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.项目的 application.properties 文件
#mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root123
# 定义获取连接的超时时间。最小250ms,默认30s
spring.datasource.hikari.connectionTimeout=10000
# 定义连接空闲时间。最小10s,默认10m
spring.datasource.hikari.idleTimeout=50000
# 定义最小的空闲连接数。推荐不设置。或与最大连接数一致;保持固定的连接数目
spring.datasource.hikari.minimumIdle=10
# 定义最大的连接数。默认10
spring.datasource.hikari.maximumPoolSize=10
# 定义连接的最大生命周期。推荐设置该属性。最小30s,默认30m
# spring.datasource.hikari.maxLifeTime=600000
# 从连接池获取到连接后,进行检查的查询语句。推荐设置该属性。默认值为none
spring.datasource.hikari.connectionTestQuery=select 1
7.项目代码地址
https://github.com/joecheng521/springboot/tree/master/spring-boot-mybatis-swagger