在对应方法上添加注解:
@OperationLog(operationType= OperationType.INSERT,operationBu="隐患",operationContent="新增隐患")
日志参数默认值设置:
public enum OperationType {
UNKNOWN("unknown"),
DELETE("delete"),
SELECT("select"),
UPDATE("update"),
INSERT("insert");
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
OperationType(String s) {
this.value = s;
}
}
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLog {
//操作内容
String operationContent() default "";
//操作类型
OperationType operationType() default OperationType.UNKNOWN;
//操作业务模块
String operationBu() default "";
}
记录日志
import com.alibaba.fastjson.JSON;
import com.zhjt.annotation.OperationLog;
import com.zhjt.security.UserUtil;
import lombok.Getter;
import lombok.Setter;
import org.aspectj.lang.ProceedingJoinPoint;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.UUID;
/**
* @ClassName OpLog
* @Description 日志切面类
* @Author wang
* @Date 2019/10/29 16:07
**/
@Component
@Aspect
public class OpLog {
@Getter
@Setter
@Value("${spring.datasource.url}")
private String url;
@Getter
@Setter
@Value("${spring.datasource.username}")
private String user;
@Getter
@Setter
@Value("${spring.datasource.password}")
private String password;
public OpLog(){
System.out.println("-----日志初始化");
}
@Pointcut("@annotation(com.zhjt.annotation.OperationLog)")
private void log() {
}
@Around("log()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
PreparedStatement pst = null;
Connection conn = null;
Object res = null;
System.out.print("进入日志记录页面");
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
try {
res = joinPoint.proceed();
return res;
} finally {
try {
//获取到拦截的接口的方法名
// String methodName = joinPoint.getSignature().getName();
//被拦截的方法的参数名称数组
// String[] parameterNames = signature.getParameterNames();
//被拦截的方法的参数值对象数组,这里我们将传入的参数转为json字符串格式便于存入数据库
//1.加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.获得数据库链接
conn= DriverManager.getConnection(url, user, password);
//方法执行完成后增加日志
OperationLog annotation = signature.getMethod().getAnnotation(OperationLog.class);
String content = annotation.operationContent();
String type = annotation.operationType().getValue();
String bu = annotation.operationBu();
String uid = UserUtil.getCurrUserId();
String id = UUID.randomUUID().toString();
Object[] objects = joinPoint.getArgs();
StringBuilder args = new StringBuilder("");
//暂时只记录删除操作
if("delete".equals(type)){
for (int i = 0; i < objects.length; i++) {
String s = JSON.toJSONString(objects[i]);
if (i == objects.length - 1){
args.append(s);
}else {
args.append(s).append(",");
}
}
}
String businessId = "";
//记录修改
if("update".equals(type)) {
if(bu.equals("菜单管理")) {
args.append(objects[0]);
}
}
String inSql = "insert into opreate_log (id,operate_man,operate_content,operate_type,operate_time,operate_bu,operate_opinions) values(?,?,?,?,GETDATE(),?,?)";
pst = conn.prepareStatement(inSql);
pst.setString(1,id);
pst.setString(2,uid);
pst.setString(3,content);
pst.setString(4,type);
pst.setString(5,bu);
pst.setString(6,args.toString());
pst.execute();
}catch (Exception e){
e.printStackTrace();
}
finally {
pst.close();
conn.close();
}
}
}
}