2020-03-05 操作日志类

package com.kuyun.syslog.controller;

import java.lang.reflect.Method;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

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.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kuyun.auth.entity.Login;
import com.kuyun.auth.entity.User;
import com.kuyun.common.entity.SysLog;
import com.kuyun.common.utils.DateCalc;
import com.kuyun.common.utils.HexUtil;
import com.kuyun.syslog.service.SyslogService;

/*
这是一个操作日志,记录在磁盘文件 (未完成 不知道保存到磁盘文件的日志保存位置)
*/
@Component //当前的类是一个ioc容器的实体类
@Aspect//当前类是一个切面类 = 切点(每一次正在执行的方法)+增强
@Order(6)
public class MySysLogAspect {
@Resource
private SyslogService syslogService;

@Autowired
private HttpServletRequest request;

@Autowired
private HttpSession session;
//定义切点   指定方法                                             包                           类   方法  参数

// @Pointcut("execution(* com.kuyun.auth.controller.LoginController.(..))")
// public void transmit () {
// }
@Pointcut("execution(
com.kuyun..controller..*(..))")
public void systemOf () {
}
//对controller下的每一个方法都进行增强
@Around("systemOf ()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object o = null;

    //开始记录日志
    User loginUser = (User)session.getAttribute("loginUser");
    String username="";
    if(loginUser!=null){
        //userName;//操作的人
         username = loginUser.getUserName();
    }
    //time; //操作时间
    String time = DateCalc.getCurrentDate1();


    // 获得方法的签名对象
    MethodSignature methodSignature = (MethodSignature)pjp.getSignature();
    //通过签名对象可以获得方法对象
    Method method = methodSignature.getMethod();
    //method;//操作方法 操作方法的英文名称
   String opeMethod = method.getName();
   String opeAction="";
    //action;//操作方法的名称  操作方法的中文名称
    boolean flag = method.isAnnotationPresent(RequestMapping.class);
    if(flag){//true 表示有RequestMapping注解
        //获得注解
        RequestMapping annotation = method.getAnnotation(RequestMapping.class);
        //注解获得属性 注解不需要get   属性名称()即可
         opeAction = annotation.name();
         if (!StringUtils.isEmpty(opeAction)) {//注解的名字不为空
             //获取id
             String id = HexUtil.resultId();
             //保存磁盘文件(未完成)
             syslogService.save(id,username,time,opeMethod,opeAction);
        }
    }

    //保存日志
    //放行执行service
    o = pjp.proceed();
    //放行
    return o;
}

}

实体类

package com.kuyun.common.entity;
/**

  • 操作日志类
  • @author a

*/
public class SysLog {
private String id; //id
private String userName;//用户姓名
private String opeTime;//操作时间
private String opeMethod;//操作方法的英文名
private String opeAction;//操作方法的中文名
public SysLog() {
super();
}
public SysLog(String id, String userName, String opeTime, String opeMethod, String opeAction) {
super();
this.id = id;
this.userName = userName;
this.opeTime = opeTime;
this.opeMethod = opeMethod;
this.opeAction = opeAction;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getOpeTime() {
return opeTime;
}
public void setOpeTime(String opeTime) {
this.opeTime = opeTime;
}
public String getOpeMethod() {
return opeMethod;
}
public void setOpeMethod(String opeMethod) {
this.opeMethod = opeMethod;
}
public String getOpeAction() {
return opeAction;
}
public void setOpeAction(String opeAction) {
this.opeAction = opeAction;
}
@Override
public String toString() {
return "SysLog [id=" + id + ", userName=" + userName + ", opeTime=" + opeTime + ", opeMethod=" + opeMethod
+ ", opeAction=" + opeAction + "]";
}

}

查询的日志进行分页
package com.kuyun.repair.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ISyslogDao {

@Select ("select count(*) from  syslog")
String logQueryTotal();

@Select ("select id,user_name,ope_time,ope_method,ope_action from  syslog  limit (#{page}-1)*#{size},#{size}")
List> logQuery(String page,String size);

}

插入日志的dao
package com.kuyun.syslog.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface SyslogServiceDao {

@Insert("insert into syslog(id,user_name,ope_time,ope_method,ope_action) values(#{id},#{username},#{time},#{opeMethod},#{opeAction})")
void save(String id, String username, String time, String opeMethod, String opeAction);

}
使用 加上name
@ResponseBody
@RequestMapping(value = "/addUser",method = RequestMethod.POST,name = "增加用户")
public Map addUser(String id,String userName) {
log.info("开始增加用户");
log.info("传入的参数为id:{}userName:{}",id,userName);
Map map =IUserService.addUser(id,userName);
log.info("增加用户结束");
return map;
}

你可能感兴趣的:(2020-03-05 操作日志类)