天天看點

Springboot2.0從零開始搭建腳手架(三)-內建swagger2+lombok+fastjosn+MP分頁

Springboot2.0從零開始搭建腳手架(三)-內建swagger2+lombok+fastjosn+MybatisPlus分頁插件+sqlj執行性能監控+

添加依賴

<!-- lombok -->
         <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        <!--     <scope>test</scope> -->
        </dependency>
        <!-- TypeBuilder -->
        <dependency>
            <groupId>com.github.ikidou</groupId>
            <artifactId>TypeBuilder</artifactId>
            <version>1.0</version>
           <!--  <scope>test</scope> -->
        </dependency>
        <!-- swagger2 -->
        <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>           

添加MP分頁插件和sql執行監控配置

package com.nqmysb.scaffold.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;

/**
 * @author liaocan
 * @since 2018-08-10
 */
@Configuration
@MapperScan("com.nqmysb.scaffold.mapper.*.*")
public class MybatisPlusConfig {

    /**
     * 分頁插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
    
    
    /**
     * SQL執行效率插件
     * 性能分析攔截器,用于輸出每條 SQL 語句及其執行時間
     */
    @Bean
//    @Profile({"dev","pro"})// 設定 dev pro 環境開啟
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
}           

主配置檔案

3.添加mapper位址
#服務端口
server:
  port=8080

# druid配置   
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:oracle:thin:@//192.168.6.6:1521/orclpdb
    username: nqmysb
    password: nqmysb
    druid:
      initial-size: 2
      max-active: 30
      min-idle: 2
      max-wait: 1234
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 5
      filter:
        stat:
          enabled: true
      filters: stat,wall,log4j
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      web-stat-filter:
        enabled: true 
      stat-view-servlet:
        enabled: true
        reset-enable: false
      aop-patterns: com.nqmysb.scaffold.user.service.*.*,com.nqmysb.scaffold.user.controller.*.*
      
      
mybatis-plus:
  mapper-locations: classpath:/mapper/*/*Mapper.xml           

swagger配置類

package com.nqmysb.scaffold.config;

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;

@Configuration
@EnableSwagger2 // 啟用Swagger2 
public class Swagger2 {
    
    @Bean  
    public Docket createRestApi() {// 建立API基本資訊  
        return new Docket(DocumentationType.SWAGGER_2)  
                .apiInfo(apiInfo())  
                .select()  
                .apis(RequestHandlerSelectors.basePackage("com.nqmysb.scaffold.controller"))// 掃描該包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外  
                .paths(PathSelectors.any())  
                .build();  
    }  
  
    private ApiInfo apiInfo() {// 建立API的基本資訊,這些資訊會在Swagger UI中進行顯示  
        return new ApiInfoBuilder()  
                .title("微服務背景腳手架工程API")// API 标題  
                .description("swagger2API清單")// API描述  
                .version("1.0")// 版本号  
                .build();  
    }  
}           

接口添加文檔描述

@Controller
@RequestMapping("/user")
@Api(value="使用者資源", tags="使用者管理")  
public class UserinfoController {

    @Autowired
    UserinfoServiceImpl userinfoServiceImpl;

    @ApiOperation(value="查詢使用者資訊", notes="通過使用者輸入的條件,查詢滿足條件的使用者資訊清單", httpMethod = "GET")  
    @ApiImplicitParams({  
        @ApiImplicitParam(paramType = "query", name = "userId", dataType = "string", required = false, value = "使用者編号"),  
        @ApiImplicitParam(paramType = "query", name = "userName", dataType = "string", required = false, value = "使用者賬号,可以模糊查找"),  
        @ApiImplicitParam(paramType = "query", name = "fullName", dataType = "string", required = false, value = "使用者姓名,可以模糊查找"),  
        @ApiImplicitParam(paramType = "query", name = "email", dataType = "string", required = false, value = "電子郵件,可以模糊查找"),    
        @ApiImplicitParam(paramType = "query", name = "mobile", dataType = "string", required = false, value = "聯系方式,可以模糊查找"),
        @ApiImplicitParam(paramType = "query", name = "status", dataType = "string", required = false, value = "使用者狀态,0:停用、1:正常")
    })  
    @RequestMapping("/getUsers")
    @ResponseBody
    public ArrayList<Userinfo> getUsers() {
        Wrapper<Userinfo> queryWrapper = null;
        ArrayList<Userinfo> data = (ArrayList<Userinfo>) userinfoServiceImpl.list(queryWrapper);
        return data;
    }           

通路swagger頁面

http://localhost:8080/swagger-ui.html

開啟logback日志

添加配置檔案 logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="1800 seconds" debug="true">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <logger name="com.nqmysb.scaffold.dao" level="DEBUG" />
    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>            

工程源代碼位址

https://github.com/nqmysb/springboot-scaffold