作者畫了流程圖方便理解

1.首先需要寫一個(gè)自定義注解
? 直接簡(jiǎn)單粗暴上代碼?
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
/**
*? Created by cengyujun on 2020/8/18 5:13 下午
* 自定義注解類? 定義controller方法的中文含義
* @Target({METHOD,TYPE}) 表示這個(gè)注解可以用用在類/接口上,還可以用在方法上
* @Retention(RetentionPolicy.RUNTIME) 表示這是一個(gè)運(yùn)行時(shí)注解,即運(yùn)行起來之后,才獲取注解中的相關(guān)信息,而不像基本注解如@Override 那種不用運(yùn)行,在編譯時(shí)eclipse就可以進(jìn)行相關(guān)工作的編譯時(shí)注解。
* @Inherited 表示這個(gè)注解可以被子類繼承
* @Documented 表示當(dāng)執(zhí)行javadoc的時(shí)候,本注解會(huì)生成相關(guān)文檔
*/
@Target({METHOD, TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Operation {
Stringvalue()default "";
}
2.利用javaAOP思想寫切面處理類
需要自己寫一個(gè)insert 方法??
?這里作者使用了 SpringBoot+MyBatisPlus 直接調(diào)用save方法?
?sysLogService.save(sysLog);(沒用MyBatisPlus,需要自己寫)
/**
* Created by cengyujun on 2020/8/18 5:15 下午
* :切面處理類
*/
@Aspect
@Component
@Slf4j
public class SysLogAspect {
@Autowired
? ? private SysLogServicesysLogService;
? ? //定義切點(diǎn)@Pointcut
? ? //在注解的位置切入代碼
? ? @Pointcut("@annotation(com.baba.apps.common.constant.Operation)")
public void logPoinCut() {
}
//切面 配置通知
? ? @AfterReturning("logPoinCut()")
public void saveSysLog(JoinPoint joinPoint) {
//保存日志
? ? ? ? SysLog sysLog =new SysLog();
? ? ? ? //從切面織入點(diǎn)處通過反射機(jī)制獲取織入點(diǎn)處的方法
? ? ? ? MethodSignature signature = (MethodSignature) joinPoint.getSignature();
? ? ? ? //獲取切入點(diǎn)所在的方法
? ? ? ? Method method = signature.getMethod();
? ? ? ? //獲取操作
? ? ? ? Operation operation = method.getAnnotation(Operation.class);
? ? ? ? if (operation !=null) {
String value = operation.value();
? ? ? ? ? ? sysLog.setOperation(value);//保存獲取的操作
? ? ? ? }
//獲取請(qǐng)求的類名
? ? ? ? String className = joinPoint.getTarget().getClass().getName();
? ? ? ? //獲取請(qǐng)求的方法名
? ? ? ? String methodName = method.getName();
? ? ? ? sysLog.setMethod(className +"." + methodName);
? ? ? ? //請(qǐng)求的參數(shù)
? ? ? ? Object[] args = joinPoint.getArgs();
? ? ? ? //將參數(shù)所在的數(shù)組轉(zhuǎn)換成json
? ? ? ? String params =null;
? ? ? ? try {
params = JacksonUtil.obj2json(args);
? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? }
if (params ==null) {
sysLog.setParams("無參數(shù)");
? ? ? ? }else {
sysLog.setParams(params);
? ? ? ? }
//獲取用戶ip地址
? ? ? ? HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
? ? ? ? sysLog.setIp(IpAdrressUtil.getIpAdrress(request));
? ? ? ? //真實(shí)地址
? ? ? ? sysLog.setAddress(StringUtils.getCityInfo(IpAdrressUtil.getIpAdrress(request)));
? ? ? ? String userName = (String) request.getSession().getAttribute("userName");
? ? ? ? sysLogService.save(sysLog);
? ? }
}
3.在需要記錄的方法加上日志注解
? 加到Controller類的方法上面??
?重點(diǎn) 是@Operation(value="")注解
@RequestMapping(value ="/getUserCount", method = RequestMethod.POST)
@ApiOperation(value ="獲取用戶數(shù)量", notes ="查詢")
@Operation(value="獲取用戶數(shù)量")
public BacResultgetUserCount(TokenDTO tokenDTO){
if (!JwtUtil2.verify(tokenDTO.getToken(),tokenDTO.getSysName())){
return BacResult.failure(ResultCode.LOGIN_DATE);
? ? }
return getUserCounts();
}
4 數(shù)據(jù)存到數(shù)據(jù)庫里面
? 可以自己寫 查詢接口展示出來,根據(jù)需求去使用這些數(shù)據(jù)
