Springboot2.0从零开始搭建脚手架(三)-集成swagger2+lombok+fastjosn+MP分页

简介: Springboot2.0从零开始搭建脚手架(三)-集成swagger2+lombok+fastjosn+MybatisPlus分页插件+sqlj执行性能监控+添加依赖<!-- lombok --> <dependency> <groupId>org.

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

目录
相关文章
|
缓存 Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
本文介绍了在Spring Boot中配置Swagger2的方法。通过创建一个配置类,添加`@Configuration`和`@EnableSwagger2`注解,使用Docket对象定义API文档的详细信息,包括标题、描述、版本和包路径等。配置完成后,访问`localhost:8080/swagger-ui.html`即可查看接口文档。文中还提示了可能因浏览器缓存导致的问题及解决方法。
1545 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
|
JSON Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的使用
本文详细介绍了Swagger2的使用方法,包括在Spring Boot项目中的配置与应用。重点讲解了Swagger2中常用的注解,如实体类上的`@ApiModel`和`@ApiModelProperty`,Controller类上的`@Api`、`@ApiOperation`以及参数上的`@ApiParam`等。通过示例代码展示了如何为实体类和接口添加注解,并在页面上生成在线接口文档,实现接口测试。最后总结了Swagger的优势及其在项目开发中的重要性,提供了课程源代码下载链接供学习参考。
1176 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的使用
|
Java Maven 微服务
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的 maven 依赖
在项目中使用Swagger2工具时,需导入Maven依赖。尽管官方最高版本为2.8.0,但其展示效果不够理想且稳定性欠佳。实际开发中常用2.2.2版本,因其稳定且界面友好。以下是围绕2.2.2版本的Maven依赖配置,包括`springfox-swagger2`和`springfox-swagger-ui`两个模块。
743 0
|
前端开发 Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档—— Swagger 简介
第6课介绍了在Spring Boot中集成Swagger2以展示在线接口文档的方法。随着前后端分离架构的发展,API文档成为连接前端与后端开发的重要纽带。然而,代码更新频繁导致文档难以同步维护,Swagger2解决了这一问题。通过Swagger,在线API文档不仅方便了接口调用方查看和测试,还支持开发者实时测试接口数据。本文使用Swagger 2.2.2版本,讲解如何在Spring Boot项目中导入并配置Swagger2工具,从而高效管理接口文档。
574 0
|
SQL 前端开发 Java
深入理解 Spring Boot 项目中的分页与排序功能
本文深入讲解了在Spring Boot项目中实现分页与排序功能的完整流程。通过实际案例,从Service层接口设计到Mapper层SQL动态生成,再到Controller层参数传递及前端页面交互,逐一剖析每个环节的核心逻辑与实现细节。重点包括分页计算、排序参数校验、动态SQL处理以及前后端联动,确保数据展示高效且安全。适合希望掌握分页排序实现原理的开发者参考学习。
1005 4
|
Java 测试技术 API
详解Swagger:Spring Boot中的API文档生成与测试工具
详解Swagger:Spring Boot中的API文档生成与测试工具
1467 4
|
前端开发 Java Maven
深入解析:如何用 Spring Boot 实现分页和排序
深入解析:如何用 Spring Boot 实现分页和排序
1440 2
|
前端开发 Java 程序员
springboot 学习十五:Spring Boot 优雅的集成Swagger2、Knife4j
这篇文章是关于如何在Spring Boot项目中集成Swagger2和Knife4j来生成和美化API接口文档的详细教程。
2883 1
|
Java Maven Docker
gitlab-ci 集成 k3s 部署spring boot 应用
gitlab-ci 集成 k3s 部署spring boot 应用
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
714 0