为什么要使用这个注解呢?如果我们直接在yml中配置其打印语句(sql的),有时这些sql在控制台中会很多会让人很烦躁,所以想着怎样做一下我想要显示哪个函数中sql打印就打印哪个.
1.此方法是作用在方法上
其在前边也讲解过了注解的作用 java自定义注解
2.首先我们先写@interface
package com.example.demo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
public @interface SQLLogger {
}
2.通过yml配置中我们可以看一下,思路就是如果log-impl配置是mybatisPlus封装好的类,我们就不扫描我们自己的直接使用其自带的,如果我们只需要在我们自己的注解上方法上打印sql语句我们需要重写log啦
代码如下:
(1):先看yml吧
# 开发环境配置
server:
# 服务器的HTTP端口,默认为8080
port: 9090
spring:
datasource:
# hikari:
# maximum-pool-size: 15
# minimum-idle: 1
# idle-timeout: 600000
# max-lifetime: 28740000
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/batch-test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
type: com.zaxxer.hikari.HikariDataSource
mybatis-plus:
configuration:
# log-impl: com.example.demo.service.LogService
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.example.demo.pojo
executor-type: batch
#IMybatis-plus:
#log:
#info: com.example.demo.controller
(2):我们需要去扫描我们加注解的方法
package com.example.demo.annotation;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Slf4j
public class AnnotationScanService implements ApplicationRunner {
@Value(value = "com.example.demo.controller")
String baseFilePath;
@Value("${mybatis-plus.configuration.log-impl:}")
String stdOutName;
public static volatile ConcurrentHashMap<String, Method> methodMap = new ConcurrentHashMap<>();
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(stdOutName);
if (stdOutName == null || !stdOutName.contains("LogService")){
return;
}
Reflections reflections = new Reflections(baseFilePath, new MethodAnnotationsScanner());
Set<Method> methods = reflections.getMethodsAnnotatedWith(SQLLogger.class);
for (Method method : methods) {
SQLLogger annotation = method.getAnnotation(SQLLogger.class);
if (annotation != null) {
methodMap.put(method.getName(), method);
}
}
}
}
(3):我们就要实现org.apache.ibatis.logging.Log;其下的Log啦
package com.example.demo.service;
import com.example.demo.annotation.AnnotationScanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.Log;
@Slf4j
public class LogService implements Log {
public LogService(String clazz) {
}
public boolean isDebugEnabled() {
return true;
}
public boolean isTraceEnabled() {
return false;
}
public void error(String s, Throwable e) {
System.err.println(s);
e.printStackTrace(System.err);
}
public void error(String s) {
System.err.println(s);
}
public void debug(String s) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (!s.contains("==")) return;
for (StackTraceElement stackTraceElement : stackTrace) {
if (!AnnotationScanService.methodMap.containsKey(stackTraceElement.getMethodName())) {
continue;
}
System.out.println(s);
}
}
public void trace(String s) {
System.out.println(s);
}
public void warn(String s) {
System.out.println(s);
}
}
这样我们就实现了,注解加在哪个方法上,其方法中的逻辑操作若有增删改查,就会打印其sql语句,兄弟们我们下次再见喽