spring+mybatic整合开发添加日志功能
1.添加日志实体对象
package com.yzy.entity; import java.util.Date; public class Log { private Date oprTime; private String type; private String operator; private String moudle; private String operation; private String result; public Log() { } public Date getOprTime() { return oprTime; } public void setOprTime(Date oprTime) { this.oprTime = oprTime; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public String getMoudle() { return moudle; } public void setMoudle(String moudle) { this.moudle = moudle; } public String getOperation() { return operation; } public void setOperation(String operation) { this.operation = operation; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } }
2.编写LogDao接口以及LogDao的mapper配置文件
package com.yzy.Dao; import com.yzy.entity.Log; import org.springframework.stereotype.Repository; import java.util.List; @Repository("logDao") public interface LogDao { List<Log> selectLogByType(String type); void insertLog(Log log); }
<?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"> <!--配置命名空间,需和dao接口一致--> <mapper namespace="com.yzy.Dao.LogDao"> <!-- 配置结果集,将数据库的表与实体类对应--> <resultMap id="resultMap" type="Log"> <result property="oprTime" column="opr_time" javaType="java.util.Date"></result> <result property="type" column="type" javaType="String"></result> <result property="operator" column="operator" javaType="String"></result> <result property="moudle" column="moudle" javaType="String"></result> <result property="operation" column="operation" javaType="String"></result> <result property="result" column="result" javaType="String"></result> </resultMap> <select id="selectLogByType" resultMap="resultMap" parameterType="String"> select * from log where type=#{type} </select> <insert id="insertLog" parameterType="Log"> insert log value(#{oprTime},#{type},#{operator},#{moudle},#{operation},#{result}) </insert> </mapper>
3.编写LogService
package com.yzy.serice.impl; import com.yzy.Dao.LogDao; import com.yzy.entity.Log; import com.yzy.serice.LogService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; import java.util.List; @Service("logService") public class LogServiceImpl implements LogService { @Resource(name = "logDao") private LogDao logDao; @Override public void addOperationLog(Log log) { log.setOprTime(new Date()); log.setType("operation"); logDao.insertLog(log); } @Override public void addSystemLog(Log log) { log.setOprTime(new Date()); log.setType("system"); logDao.insertLog(log); } @Override public void addLoginLog(Log log) { log.setOprTime(new Date()); log.setType("login"); logDao.insertLog(log); } @Override public List<Log> getOperationLog() { return logDao.selectLogByType("operation"); } @Override public List<Log> getSystemLog() { return logDao.selectLogByType("system"); } @Override public List<Log> getLoginLog() { return logDao.selectLogByType("login"); } }
4.编写logAdvice,通过aspect配置advice生成日志
package com.yzy.global; import com.yzy.entity.Log; import com.yzy.entity.Staff; import com.yzy.serice.LogService; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @Component @Aspect public class logAdvice { @Resource(name = "logService") LogService logService; @AfterReturning("execution(* com.yzy.controller.*.*(..)) && !execution(* com.yzy.controller.selfController.*(..))&& !execution(* com.yzy.controller.*.to*(..))") public void operationAdvice(JoinPoint joinPoint){ Log log=new Log(); log.setMoudle(joinPoint.getSignature().getClass().getSimpleName()); //获取类名 log.setOperation(joinPoint.getSignature().getName()); //获取方法名 HttpServletRequest request=(HttpServletRequest) joinPoint.getArgs()[0]; //从joinpoint中获取属性request Staff staff=(Staff) request.getSession().getAttribute("USER"); log.setOperator(staff.getAccount()); log.setResult("成功"); logService.addOperationLog(log); } @AfterThrowing(throwing = "e",pointcut = "execution(* com.yzy.controller.*.*(..)) && !execution(* com.yzy.controller.selfController.*(..))") public void systemAdvice(JoinPoint joinPoint,Throwable e){ Log log=new Log(); log.setMoudle(joinPoint.getSignature().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request=(HttpServletRequest) joinPoint.getArgs()[0]; Staff staff=(Staff) request.getSession().getAttribute("USER"); log.setOperator(staff.getAccount()); log.setResult(e.getClass().getSimpleName()); logService.addSystemLog(log); } @After("execution(* com.yzy.controller.selfController.login(..))") public void loginAdvice(JoinPoint joinPoint){ Log log=new Log(); log.setMoudle(joinPoint.getSignature().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request=(HttpServletRequest) joinPoint.getArgs()[0]; Staff staff=(Staff) request.getSession().getAttribute("USER"); if (staff!=null) { log.setOperator(staff.getAccount()); log.setResult("成功"); }else{ log.setOperator(request.getParameter("account")); log.setResult("失败"); } logService.addLoginLog(log); } @Before("execution(* com.yzy.controller.selfController.logout(..))") public void logoutAdvice(JoinPoint joinPoint){ Log log=new Log(); log.setMoudle(joinPoint.getSignature().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request=(HttpServletRequest) joinPoint.getArgs()[0]; Staff staff=(Staff) request.getSession().getAttribute("USER"); if (staff!=null) { log.setOperator(staff.getAccount()); log.setResult("成功"); }else{ log.setOperator(request.getParameter("account")); log.setResult("失败"); } logService.addLoginLog(log); } }
5.编写logController
package com.yzy.controller; import com.yzy.entity.Log; import com.yzy.serice.LogService; import org.springframework.stereotype.Controller; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @Controller("logController") public class logController { @Resource(name = "logService") private LogService logService; public void operationLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Log> operationLog = logService.getOperationLog(); request.setAttribute("LIST",operationLog); request.setAttribute("TYPE","操作"); request.getRequestDispatcher("/log_list.jsp").forward(request,response); } public void loginLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Log> operationLog = logService.getLoginLog(); request.setAttribute("LIST",operationLog); request.setAttribute("TYPE","登陆"); request.getRequestDispatcher("/log_list.jsp").forward(request,response); } public void systemLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Log> operationLog = logService.getSystemLog(); request.setAttribute("LIST",operationLog); request.setAttribute("TYPE","系统"); request.getRequestDispatcher("/log_list.jsp").forward(request,response); } }
logAdvice