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

本文档介绍了如何在Springboot2.0项目中集成Swagger2用于API文档管理,使用Lombok简化代码,Fastjson进行JSON解析,并配合MybatisPlus实现分页查询,同时添加SQL执行监控。详细步骤包括添加依赖、配置MP分页插件和SQL监控、配置主文件、设置Swagger、接口注释、访问Swagger页面以及启用Logback日志。源代码已上传至GitHub。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值