service层:实体类Log,dao,mybatis(xml),service接口,serviceImp(接口实现)
Log
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 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; } }
dao层
import com.imooc.sm.entity.Log; import org.springframework.stereotype.Repository; import java.util.List; @Repository("logDao") public interface LogDao { void insert(Log log); List<Log> selectByType(String type); }
service接口:
import com.imooc.sm.entity.Log; import java.util.List; public interface LogService { void addSystemLog(Log log); void addLoginLog(Log log); void addOperationLog(Log log); List<Log> getSystemLog(); List<Log> getLoginLog(); List<Log> getOperationLog(); }
LogServiceImp
import com.imooc.sm.dao.LogDao; import com.imooc.sm.entity.Log; import com.imooc.sm.service.LogService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; @Service("logService") public class LogServiceImpl implements LogService { @Autowired private LogDao logDao; public void addSystemLog(Log log) { log.setOprTime(new Date()); log.setType("system"); logDao.insert(log); } public void addLoginLog(Log log) { log.setOprTime(new Date()); log.setType("login"); logDao.insert(log); } public void addOperationLog(Log log) { log.setOprTime(new Date()); log.setType("operation"); logDao.insert(log); } public List<Log> getSystemLog() { return logDao.selectByType("system"); } public List<Log> getLoginLog() { return logDao.selectByType("login"); } public List<Log> getOperationLog() { return logDao.selectByType("operation"); } }
LogDao:
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.imooc.sm.dao.LogDao"> <resultMap id="resultMap" type="Log"> <result property="oprTime" column="opr_time" javaType="java.util.Date"/> <result property="type" column="type" javaType="String"/> <result property="operator" column="operator" javaType="String"/> <result property="moudle" column="moudle" javaType="String"/> <result property="operation" column="operation" javaType="String"/> <result property="result" column="result" javaType="String"/> </resultMap> <insert id="insert" parameterType="Log"> insert into log values(#{oprTime},#{type},#{operator},#{moudle},#{operation},#{result}); </insert> <select id="selectByType" parameterType="String" resultMap="resultMap"> select * from log where type=#{type} order by opr_time desc </select> </mapper>
spring配置:
<aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.imooc.sm.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!-- 全局扫描 --> <context:component-scan base-package="com.imooc.sm"/> <aop:aspectj-autoproxy/>
web层:
controller:
package com.imooc.sm.controller; import com.imooc.sm.entity.Department; import com.imooc.sm.entity.Log; import com.imooc.sm.service.LogService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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 { @Autowired private LogService logService; public void operationLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Log> list = logService.getOperationLog(); request.setAttribute("LIST",list); request.setAttribute("TYPE","操作"); request.getRequestDispatcher("../log_list.jsp").forward(request,response); } public void loginLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Log> list = logService.getLoginLog(); request.setAttribute("LIST",list); request.setAttribute("TYPE","登陆"); request.getRequestDispatcher("../log_list.jsp").forward(request,response); } public void systemLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Log> list = logService.getSystemLog(); request.setAttribute("LIST",list); request.setAttribute("TYPE","系统"); request.getRequestDispatcher("../log_list.jsp").forward(request,response); } }
global(配置具体的日志切入点、日志操作)
package com.imooc.sm.global; import com.imooc.sm.entity.Log; import com.imooc.sm.entity.Staff; import com.imooc.sm.service.LogService; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @Component @Aspect public class LogAdvice { @Autowired private LogService logService; @AfterReturning("execution(* com.imooc.sm.controller.*.*(..)) && !execution(* com.imooc.sm.controller.SelfController.*(..)) && !execution(* com.imooc.sm.controller.*.to*(..))") public void operationLog(JoinPoint joinPoint){ Log log = new Log(); log.setMoudle(joinPoint.getTarget().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request =(HttpServletRequest) joinPoint.getArgs()[0]; HttpSession session = request.getSession(); Object obj = session.getAttribute("USER"); Staff staff =(Staff)obj; log.setOperator(staff.getAccount()); log.setResult("成功"); logService.addOperationLog(log); } @AfterThrowing(throwing ="e",pointcut ="execution(* com.imooc.sm.controller.*.*(..)) && !execution(* com.imooc.sm.controller.SelfController.*(..))") public void systemLog(JoinPoint joinPoint,Throwable e){ Log log = new Log(); log.setMoudle(joinPoint.getTarget().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request =(HttpServletRequest) joinPoint.getArgs()[0]; HttpSession session = request.getSession(); Object obj = session.getAttribute("USER"); Staff staff =(Staff)obj; log.setOperator(staff.getAccount()); log.setResult(e.getClass().getSimpleName()); logService.addSystemLog(log); } @After("execution(* com.imooc.sm.controller.SelfController.login(..))") public void loginLog(JoinPoint joinPoint){ log(joinPoint); } @Before("execution(* com.imooc.sm.controller.SelfController.logout(..))") public void logoutLog(JoinPoint joinPoint){ log(joinPoint); } private void log(JoinPoint joinPoint){ Log log = new Log(); log.setMoudle(joinPoint.getTarget().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request =(HttpServletRequest) joinPoint.getArgs()[0]; HttpSession session = request.getSession(); Object obj = session.getAttribute("USER"); if(obj==null){ log.setOperator(request.getParameter("account")); log.setResult("失败"); }else { Staff staff = (Staff) obj; log.setOperator(staff.getAccount()); log.setResult("成功"); } logService.addLoginLog(log); } }
实现效果: