SSM框架把日志信息保存到数据库过程详解

2020-07-01 13:01 来源:易采站长站 作者:于丽 点击: 评论:

A-A+

原标题:SSM框架把日志信息保存到数据库过程详解

1)在service层和mapper层中写一个插入方法和查询方法;

我们先写一个日志类;定义属性;并且要在数据库中建好表;

package entity;

public class Log {
  private Integer id;
  private Integer logtype;
  private String description;
  private String param;

  public Log(){

  }
  public Log(Integer id, Integer logtype, String description, String param) {
    this.id = id;
    this.logtype = logtype;
    this.description = description;
    this.param = param;
  }

  @Override
  public String toString() {
    return "Log{" +
        "id=" + id +
        ", logtype=" + logtype +
        ", description='" + description + '\'' +
        ", param='" + param + '\'' +
        '}';
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Integer getLogtype() {
    return logtype;
  }

  public void setLogtype(Integer logtype) {
    this.logtype = logtype;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getParam() {
    return param;
  }

  public void setParam(String param) {
    this.param = param;
  }
}

该写方法了

1、logService.java页面;

public interface LogService {
  int insert(Log log);
  List<Log> findAll();
}

2、logServiceImpl.java页面;

@Service
public class LogServiceImpl implements LogService {
  @Autowired
  private LogMapper logMapper;
  @Override
  public int insert(Log log) {
    int i=logMapper.insert(log);
    return i;
  }
  @Override
  public List<Log> findAll() {
    List<Log> logs=logMapper.findAll();
    return logs;
  }
}

3、logMapper.java页面:

public interface LogMapper {
  int insert(Log log);
  List<Log> findAll();
}

4、logMapper.xml页面;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.LogMapper">
  <insert id="insert">
    <selectKey keyProperty="id" resultType="integer" order="BEFORE">
      select seq_logaspect.nextval from dual
    </selectKey>
    insert into logaspect(id,logtype,description,param) values (#{id},#{logtype},#{description},#{param})
  </insert>
  <select id="findAll" resultType="entity.Log">
    select * from logaspect
  </select>
</mapper>

5、由于我们打印日志是通过切面,所以我们写一个切面类;

package aop;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import entity.Log;
import entity.Student;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import service.LogService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;

@Component//对象由spring管理
@Aspect//切面注解
public class LogAspect {
  @Autowired
  private LogService logService;
  private static final Logger LOGGER = LogManager.getLogger(LogAspect.class);

  //定义切入点,切入到添加了LogData注解的方法上
  @Pointcut("@annotation(aop.LogData)")
  public void pointCut(){

  }

  /**
   * 记录日志的切面方法
   * 在该方法中定义统一的日志记录逻辑
   * @param joinPoint
   */
  @Before("pointCut()")
  public void log(JoinPoint joinPoint){
    System.out.println("进入日志Aspect");
    //获取到方法签名
    MethodSignature signature= (MethodSignature) joinPoint.getSignature();
    //获取到连接点方法对象
    Method method=signature.getMethod();
    //获取方法上面特定的注解
    LogData annotation=method.getAnnotation(LogData.class);
    LogType logType=annotation.logType();
    String description=annotation.description();
    LOGGER.info("获取到注解内容:logType="+logType.getType()
        +",description:"+description);
    //aop中获取request
    ServletRequestAttributes requestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request=requestAttributes.getRequest();
    HttpSession session=request.getSession();
    //获取操作人
    Student student= (Student) session.getAttribute("student");
    //获取请求数据
    Map<String,String[]> parameterMap=request.getParameterMap();
    //将对象转换成json字符串==>存储到请求数据字段中
    //jackSon json字符串操作
    ObjectMapper objectMapper=new ObjectMapper();
    try {
      String s=objectMapper.writeValueAsString(parameterMap);
      LOGGER.info("请求数据:"+s);
      Log log = new Log();
      log.setLogtype(logType.getType());
      log.setDescription(description);
      log.setParam(s);
      logService.insert(log);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }

    //todo 将日志信息保存到数据库 LogController service mapper jsp
  }
}

【易采站长站编辑:秋军】