SpringBoot使用Aop自定义注解展示日志信息

展示效果

成功日志
在这里插入图片描述
在这里插入图片描述
失败日志

在这里插入图片描述
在这里插入图片描述

2:sql文件

-- 日志alter
DROP TABLE IF EXISTS `t_log`;
CREATE TABLE `t_log`
(
    `id`                int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
    `username`          varchar(255)  default null comment '当前登录的用户名',
    `requestIp`         varchar(255)  default null comment '当前登录的ip地址',
    `requestParams`     text          default null comment '当前请求的参数',
    `className`         varchar(3000) default null comment '当前请求的类路径名',
    `requestMethodName` varchar(3000) default null comment '当前请求的方法名称',
    `requestMethod`     varchar(3000) default null comment '当前请求的方法类型',
    `startTime`         varchar(300)  default null comment '当前请求的时间',
    `endDate`           bigint        default null comment '获取JSON格式数据耗时',
    `comment`           varchar(500)  default null comment '方法描述',
    `result`            text          default null comment '返回结果',
    `requestType`       varchar(255)  default null comment '操作类型',
    `requestURI`        varchar(255)  default null comment '相对路径',
    `requestURL`        varchar(255)  default null comment '获取url',
    primary key (id) USING BTREE
) ENGINE = InnoDB
  AUTO_INCREMENT = 68
  CHARACTER SET = utf8mb4
  COLLATE = utf8mb4_0900_ai_ci
  ROW_FORMAT = Dynamic;

-- 日志错误信息收集
DROP TABLE IF EXISTS `t_errorLog`;
CREATE TABLE `t_errorLog`
(
    `id`                int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
    `requestIp`         varchar(255)  default null comment '当前登录的ip地址',
    `requestParams`     text          default null comment '当前请求的参数',
    `className`         varchar(3000) default null comment '当前请求的类路径名',
    `requestMethod`     varchar(3000) default null comment '当前请求的方法类型',
    `requestMethodName` varchar(3000) default null comment '当前请求的方法名称',
    `requestURI`        varchar(255)  default null comment '相对路径',
    `requestURL`        varchar(255)  default null comment '获取url',
    `comment`           varchar(500)  default null comment '方法描述',
    `errorName`         varchar(500)  default null comment '异常名称',
    `errorMessage`      text          default null comment '异常具体信息',
    `requestType`       varchar(255)  default null comment '操作类型',
    primary key (id) USING BTREE
) ENGINE = InnoDB
  AUTO_INCREMENT = 68
  CHARACTER SET = utf8mb4
  COLLATE = utf8mb4_0900_ai_ci
  ROW_FORMAT = Dynamic;

3:pom文件

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>


        <!--引入springAOP依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!--swagger依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!--swaggerUI依赖-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

        <!--easypoi导入导出excel-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.3</version>
        </dependency>

        <!--测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!--JJWT-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
        </dependency>

        <!-- redis依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!--对象池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

        <!--谷歌登录验证码-->
        <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>kaptcha</artifactId>
            <version>0.0.9</version>
        </dependency>

        <!--使用阿里数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!-- RabbitMQ依赖 -->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
        </dependency>

        <!--springboot整合RabbitMQ依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!-- valid校验注解 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

4:枚举类

package com.qycq.server.enums;

import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;


/**
 * @author 七月初七
 * @version 1.0
 * @date 2021/7/19 23:11
 */
@ApiModel(value = "日志操作类型")
@Slf4j
public enum LogEnum {
    LOGIN("登录操作!"),
    LOGOUT("退出操作!"),
    SELECT("查询操作"),
    DELETE("删除操作"),
    UPDATE("更新操作!"),
    CALCULATION("计算操作!"),
    INSERT("增加操作");

    /**
     * 操作类型
     */
    private final String type;

    LogEnum(final String type) {
        this.type = type;
    }

    public String getType() {
        return this.type;
    }

}

5:@Log自定义注解

package com.qycq.server.annotations;

import com.qycq.server.enums.LogEnum;
import io.swagger.annotations.ApiModel;

import java.lang.annotation.*;

/**
 * @author 七月初七
 * @version 1.0
 * @date 2021/7/19 22:50
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ApiModel(value = "日志注解")
public @interface Log {
    /**
     * 日志描述
     *
     * @return
     */
    LogEnum[] type() default {};


}

6:切面类

package com.qycq.server.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qycq.server.encapsulation.ResultBean;
import com.qycq.server.enums.LogEnum;
import com.qycq.server.exception.GlobalException;
import com.qycq.server.pojo.Errorlog;
import com.qycq.server.pojo.Log;
import com.qycq.server.service.ErrorlogService;
import com.qycq.server.service.LogService;
import com.qycq.server.utils.HttpServletRequestUtil;
import com.qycq.server.utils.IpUtil;
import com.qycq.server.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author 七月初七
 * @version 1.0
 * @date 2021/7/19 23:17
 */
@ApiModel(value = "日志注解切面类")
@Slf4j
@Configuration
@Aspect
@Order(1)
public class LogAspectImpl {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogAspectImpl.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private LogService logService;

    @Autowired
    private ErrorlogService errorlogService;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 切点
     */
    @Pointcut("@annotation(com.qycq.server.annotations.Log)")
    public void pointcut() {

    }

    /**
     * 前置增强
     *
     * @param joinPoint
     */
    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        LOGGER.info("=========  前置增强 start...  =========");
    }

    /**
     * 环绕增强
     *
     * @param proceedingJoinPoint
     * @return
     */
    @Around("pointcut()")
    public Object result(ProceedingJoinPoint proceedingJoinPoint) {
        LOGGER.info("=========  环绕增强 start...  =========");
        //获取开始时间
        long startTime = System.currentTimeMillis();
        HttpServletRequest httpServletRequest = HttpServletRequestUtil.getHttpServletRequest();
        Log log = new Log();
        Object proceed = null;
        try {
            //返回结果
            proceed = proceedingJoinPoint.proceed();

            Signature signature = proceedingJoinPoint.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            Method method = methodSignature.getMethod();
            if (method.isAnnotationPresent(ApiOperation.class)) {
                //获取方法描述
                ApiOperation annotation = method.getAnnotation(ApiOperation.class);
                log.setComment(annotation.value());
                LOGGER.info(annotation.value());
            }


            if (method.isAnnotationPresent(com.qycq.server.annotations.Log.class)) {
                //获取操作类型
                com.qycq.server.annotations.Log annotation = method.getAnnotation(com.qycq.server.annotations.Log.class);
                for (LogEnum logEnum : annotation.type()) {
                    log.setRequestType(logEnum.getType());
                    LOGGER.info(logEnum.getType());
                }
            }

            //获取登录用户名
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (!(authentication instanceof AnonymousAuthenticationToken)) {
                log.setUsername(authentication.getName());
                LOGGER.info(authentication.getName());
            }


            //获取JSON数据耗时
            long endDate = System.currentTimeMillis();

            //获取方法类型(get,post...)
            String methodType = httpServletRequest.getMethod();
            String requestURL = httpServletRequest.getRequestURL().toString();
            String requestURI = httpServletRequest.getRequestURI();
            String className = proceedingJoinPoint.getTarget().getClass().getName() + "." + proceedingJoinPoint.getSignature().getName();
            String requestIp = IpUtil.getIpAddr(httpServletRequest);
            Object params = getParams(method, proceedingJoinPoint.getArgs());

            log.setRequestMethod(methodType);
            log.setRequestURL(requestURL);
            log.setRequestURI(requestURI);
            log.setClassName(className);
            log.setRequestIp(IpUtil.getIpAddr(httpServletRequest));
            log.setRequestParams(objectMapper.writeValueAsString(params));
            log.setStartTime(simpleDateFormat.format(new Date(startTime)));
            log.setEndDate(System.currentTimeMillis() - endDate);
            log.setRequestMethodName(proceedingJoinPoint.getSignature().getName());
            log.setResult(objectMapper.writeValueAsString(proceed));

            // 打印日志信息输出到控制台
            LOGGER.info("methodType:{}", methodType);
            LOGGER.info("requestMethodName:{}", proceedingJoinPoint.getSignature().getName());
            LOGGER.info("requestURL:{}", requestURL);
            LOGGER.info("requestURI:{}", requestURI);
            LOGGER.info("className:{}", className);
            LOGGER.info("requestIp:{}", requestIp);
            LOGGER.info("params:{}", objectMapper.writeValueAsString(params));
            LOGGER.info("startDate:{}", simpleDateFormat.format(new Date(startTime)));
            LOGGER.info("endDate:{}", startTime - endDate);
            LOGGER.info("result:{}", objectMapper.writeValueAsString(proceed));

            //保存日志
            boolean save = logService.save(log);
            if (save) {
                LOGGER.info("日志保存成功!");
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            LOGGER.error("日志保存失败:{" + throwable.getMessage() + "}");
        }
        return proceed;
    }

    /**
     * 后置增强
     *
     * @param joinPoint
     */
    @AfterReturning("pointcut()")
    public void afterReturning(JoinPoint joinPoint) {
        LOGGER.info("==========  进入后置增强...  ==========");
    }

    /**
     * 异常增强
     *
     * @param joinPoint
     * @param error
     */
    @AfterThrowing(value = "pointcut()", throwing = "error")
    public void afterThrowing(JoinPoint joinPoint, Throwable error) {
        Errorlog errorlog = new Errorlog();
        HttpServletRequest httpServletRequest = HttpServletRequestUtil.getHttpServletRequest();
        try {
            //获取代理方法
            Signature signature = joinPoint.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            //获取方法名称
            Method method = methodSignature.getMethod();


            if (method.isAnnotationPresent(ApiOperation.class)) {
                //获取方法描述
                ApiOperation annotation = method.getAnnotation(ApiOperation.class);
                errorlog.setComment(annotation.value());
                LOGGER.info(annotation.value());
            }


            if (method.isAnnotationPresent(com.qycq.server.annotations.Log.class)) {
                //获取操作类型
                com.qycq.server.annotations.Log annotation = method.getAnnotation(com.qycq.server.annotations.Log.class);
                for (LogEnum logEnum : annotation.type()) {
                    errorlog.setRequestType(logEnum.getType());
                    LOGGER.info(logEnum.getType());
                }
            }


            //获取完整类路径名
            String classMethodName = method + "";
            errorlog.setClassName(classMethodName);
            //获取请求地址
            String ipAddr = IpUtil.getIpAddr(httpServletRequest);
            errorlog.setRequestIp(ipAddr);
            //获取参数
            Object args = getParams(method, joinPoint.getArgs());
            errorlog.setRequestParams(objectMapper.writeValueAsString(args));
            //获取异常名称
            String errorName = error.getClass().getName();
            errorlog.setErrorName(errorName);

            //获取请求方法类型
            String methodType = httpServletRequest.getMethod();
            errorlog.setRequestMethod(methodType);

            //获取相对方法名称
            String simpleName = joinPoint.getTarget().getClass().getSimpleName();
            errorlog.setRequestMethodName(simpleName);

            //获取方法的url
            String requestURL = httpServletRequest.getRequestURL().toString();
            errorlog.setRequestURL(requestURL);
            //相对路径
            String requestURI = httpServletRequest.getRequestURI();
            errorlog.setRequestURI(requestURI);
            //返回堆栈跟踪元素
            String errorMessage = this.stackTraceToString(errorName, error.getMessage(), error.getStackTrace());
            errorlog.setErrorMessage(errorMessage);
            LOGGER.info("方法:{}", classMethodName);
            LOGGER.info("入参:{}", args);
            LOGGER.info("uri:{}", requestURI);
            LOGGER.info("url:{}", requestURL);
            LOGGER.info("methodType:{}", methodType);
            LOGGER.info("ipAddr:{}", ipAddr);
            LOGGER.error("异常信息:{}", errorMessage);
            boolean save = errorlogService.save(errorlog);
            if (save) {
                LOGGER.info("错误日志保存成功!");
            }
//            LOGGER.error("方法:{}, 入参:{}, uri:{}, 请求ip:{}, 异常信息:{}", classMethodName, args, requestURI, ipAddr, errorMessage);
        } catch (Throwable throwable) {
            log.error("{}", throwable.getMessage(), throwable);
        }
    }

    /**
     * 组装异常信息
     *
     * @param errorName
     * @param errorMessage
     * @param stackTrace
     * @return
     */
    private String stackTraceToString(String errorName, String errorMessage, StackTraceElement[] stackTrace) {
        StringBuilder stringBuffer = new StringBuilder();
        for (StackTraceElement traceElement : stackTrace) {
            stringBuffer.append(traceElement).append("\n");
        }
        return errorName + ":" + errorMessage + "\n\t" + stringBuffer.toString();
    }


    /**
     * 获取参数
     *
     * @param method 当前方法
     * @param args   当前参数
     * @return
     */
    private Object getParams(Method method, Object[] args) {
        List<Object> paramsList = new ArrayList<>();
        //拿到当前方法的参数数组
        Parameter[] parameters = method.getParameters();

        for (int i = 0; i < parameters.length; i++) {

            //获取RequestBody注解修饰的参数
            if (parameters[i].isAnnotationPresent(RequestBody.class)) {
                RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
                if (requestBody != null) {
                    paramsList.add(args[i]);
                }
            }

            //获取RequestParam注解修饰的参数
            if (parameters[i].isAnnotationPresent(RequestParam.class)) {
                RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
                Map<String, Object> map = new HashMap<>(16);
                //获取的形参名称作为key
                String key = parameters[i].getName();
                System.out.println("key = " + key);
                if (StringUtil.isNotEmpty(requestParam.value())) {
                    key = requestParam.value();
                }
                map.put(key, args[i]);
                paramsList.add(map);
            }
        }
        if (paramsList.size() == 0) {
            return null;
        } else if (paramsList.size() == 1) {
            return paramsList.get(0);
        } else {
            return paramsList;
        }
    }
}

7:mybatisPlus代码生成器pom


 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>

        <!-- 模板引擎代码生成器 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>

        <!-- 代码生成器主要依赖 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.1.tmp</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

8:直接运行下面代码即可生成对应的pojo、service、mapper、mapper.xml、serviceImpl、controller,我这里就不一一去截图了。注意修改你的项目路径

package com.qycq.generator;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


/**
 * @author 七月初七
 * @version 1.0
 * @date 2021/7/10 15:51
 */
public class MyBatisPlusGenerator {
    /**
     *  数据库链接
     */
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    /**
     *  用户名
     */
    private static final String USERNAME = "root";
    /**
     *  密码
     */
    private static final String PASSWORD = "2829";
    /**
     * 链接的url
     */
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/yeb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
    /**
     * 项目的绝对路径
     */
    private static final String PATH = "E:\\Project\\qycq\\yeb-server";
//    private static final String PATH = "E:\\Project\\qycq";
    /**
     * 作者
     */
    private static final String AUTHOR = "七月初七";
    /**
     * 包名
     */
    private static final String PACKAGE = "com.qycq.server";
    /**
     * 实体类
     */
    private static final String ENTITY = "pojo";
    /**
     * mapper
     */
    private static final String MAPPER = "mapper";
    /**
     * controller
     */
    private static final String CONTROLLER = "controller";
    /**
     * service
     */
    private static final String SERVICE = "service";
    /**
     * serviceImpl
     */
    private static final String SERVICE_IMPL = "service.impl";
    /**
     * 根据表名生成多个模块代码
     */
    private static final String[] TABLE_NAME = {"t_log","t_errorLog"};


    public static void main(String[] args) {
        //代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);//指定数据库类型
        dataSourceConfig.setDriverName(DRIVER_NAME);
        dataSourceConfig.setUsername(USERNAME);
        dataSourceConfig.setPassword(PASSWORD);
        dataSourceConfig.setUrl(JDBC_URL);
        autoGenerator.setDataSource(dataSourceConfig);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false);
        globalConfig.setFileOverride(false); //重新生成时文件是否覆盖

        globalConfig.setBaseResultMap(true);
        //xml 开启BaseColumnList
        globalConfig.setBaseColumnList(true);
        // 实体属性 Swagger2 注解
        globalConfig.setSwagger2(true);
        //输出路径
        globalConfig.setOutputDir(PATH + "/src/main/java");
        //设置作者名字
        globalConfig.setAuthor(AUTHOR);
        //去掉service的I前缀,一般只需要设置service就行
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setServiceName("%sService");
        autoGenerator.setGlobalConfig(globalConfig);

        //包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(PACKAGE);//自定义包的路径
        packageConfig.setEntity(ENTITY);
        packageConfig.setMapper(MAPPER);
        packageConfig.setController(CONTROLLER);
        packageConfig.setService(SERVICE);
        packageConfig.setServiceImpl(SERVICE_IMPL);
        autoGenerator.setPackageInfo(packageConfig);

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();

        //是否使用Lombok
        strategyConfig.setEntityLombokModel(true);
        //生成RestController
        strategyConfig.setRestControllerStyle(true);
        //根据表名生成模块
        strategyConfig.setInclude(TABLE_NAME);
        //去掉表前缀
        strategyConfig.setTablePrefix("t_");
        autoGenerator.setStrategy(strategyConfig);
        //数据库表映射到实体的命名策略
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        //数据库表字段映射到实体的命名策略
        strategyConfig.setColumnNaming(NamingStrategy.no_change);

        //执行
        autoGenerator.execute();

    }
}

9:yml文件

# yml文件必须注意缩进
# 端口号
server:
  port: 9091

  # 数据源
spring:
  # 数据源配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 2829
    hikari:
      # 连接池名
      pool-name: DateHikariCP
      # 最小空闲连接数
      minimum-idle: 5
      # 空闲连接存活最大时间,默认600000(10分钟)
      idle-timeout: 180000
      # 最大连接数,默认10
      maximum-pool-size: 10
      # 从连接池返回的连接的自动提交
      auto-commit: true
      # 连接最大存活时间,0表示永久存活,默认1800000(30分钟)
      max-lifetime: 1800000
      # 连接超时时间,默认30000(30秒)
      connection-timeout: 30000
      # 测试连接是否可用的查询语句
      connection-test-query: SELECT 1

  #redis
  redis:
    host: 127.0.0.1
    port: 6379
    database: 0
    timeout: 10000ms  #超时时间
    #    password: 2829
    lettuce:
      pool:
        max-active: 1024 #最大连接数
        max-wait: 10000ms #最大等待时间
        max-idle: 200 #最大空闲连接
        min-idle: 5 #最小空闲链接

  #rabbitmq
  #  rabbitmq:
  #    password: ems
  #    username: 123
  #    host: 127.0.0.1
  #    port: 5672
  #    virtual-host: /ems #虚拟主机

  # Mybatis-plus配置
mybatis-plus:
  #配置Mapper映射文件
  mapper-locations: classpath*:/mapper/*Mapper.xml
  # 配置MyBatis数据返回类型别名(默认别名是类名)
  type-aliases-package: com.qycq.server.pojo
  configuration:
    # 自动驼峰命名
    map-underscore-to-camel-case: false

    ## Mybatis SQL 打印(方法接口所在的包,不是Mapper.xml所在的包)
logging:
  level:
    com.qycq.server.mapper: debug

10:展示效果

   /**
     * 获取当前登录用户对象的用户名
     *
     * @param principal 该参数的getName 获取的就是我们放在security全局对象中的用户对象
     * @return
     */
    @GetMapping("/admin/info")
    @Log(type = LogEnum.SELECT)
    @ApiOperation(value = "获取当前登录用户对象的用户名")
    public ResultBean<Admin> getAdminByUserName(Principal principal) {
        if (StringUtil.isEmpty(principal)) {
            throw new UsernameNotFoundException("该用户不存在!");
        }
        Admin admin = adminService.getAdminByUserName(principal.getName());
        admin.setPassword(null);
        //设置角色
        admin.setRoles(adminService.getRolesByAdminId(admin.getId()));
        return ResultBean.data("", admin);
    }

展示效果:
在这里插入图片描述
数据库肯定存入进去了。
如果是错误的日志则会产生如下效果
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值