日志

日志:

  1. 依赖:
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.0.6.RELEASE</version>
   </dependency>
  1. 建表
    在这里插入图片描述
  2. 生成mapper,model,写service
  3. 日志注解
package com.test.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogRecord {
   String msg() default  "";
   LogOptType optType();
}

  1. 日志切面类
package com.test.util;

import com.test.base.customer.model.Log;
import com.test.base.customer.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class LogAspect {

    @Resource
    private LogService logService;


    @Pointcut("@annotation(com.test.util.LogRecord)")
    public void pointCutLog() {
    }


    @Around("pointCutLog()")
    public Object addLog(ProceedingJoinPoint pjp) throws Throwable {
        //获取方法签名(方法签名=方法名+参数类型)
        MethodSignature signature = (MethodSignature) pjp.getSignature();
       //从方法签名里获取方法
        Method method = signature.getMethod();

        //判断该方法是否被LogRecord注解注解
        if (!method.isAnnotationPresent(LogRecord.class))
            return pjp.proceed();
        //获取注解对象
        LogRecord logRecord = method.getAnnotation(LogRecord.class);

        //获取方法参数列表
        Object[] args = pjp.getArgs();
        if (args == null || args.length == 0) {
            return pjp.proceed();
        }
        Log log = new Log();
        log.setOptUser("皮卡丘");
        log.setDetailMsg(args[0].toString());
        log.setOptTime(new Date());
        log.setOptType(logRecord.optType().getCode());
        log.setSimpleMsg(logRecord.msg());
        logService.addLog(log);

        return pjp.proceed();
    }

}

  1. 日志枚举类
package com.test.util;

public enum LogOptType {
    ADD("1"),UPDATE("2"),DELETE("3");

    String code;

    public String getCode() {
        return code;
    }

    LogOptType(String code) {
        this.code = code;
    }
}

  1. 切面层加注解
    例:
    @LogRecord(optType = LogOptType.UPDATE,msg = "更新客户信息")
    @RequestMapping("updateCustomer")
    public Result updateCustomer(Customer customer) {
        customerService.updateByPrimaryKey(customer);
        return Result.success();
    }

    @LogRecord(optType = LogOptType.ADD,msg = "新增客户信息")
    @RequestMapping("addCustomer")
    public Result addCustomer(Customer customer) {
        customerService.addCutomer(customer);
        return Result.success();
    }

    @LogRecord(optType = LogOptType.DELETE,msg = "删除客户信息")
    @RequestMapping("deleteCustomer")
    public Result deleteCustomer(Customer customer) {
        customerService.deleteCutomer(customer.getId());
        return Result.success();
    }

  1. application.yml
#db
spring:
  datasource:
    url: jdbc:mysql://localhost/home
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  aop:
    auto: true
    
#mybatis
mybatis:
  mapper-locations: mapper/*.xml
  type-aliases-package: com.test.base
  config-location: classpath:mybatis-config.xml

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值