Personnel management system --第八天
日志处理的实现
记载用户操作的过程,用到了AspectJ的aop框架.比如说用户在执行了department模块下的add方法之后,我们要在日志中添加被执行的模块与方法,但又不能写在该方法中,所以使用切面化的方式处理,自动注入来实现。
1.实体类
public class Log { private Date operationTime; private String type; private String operator; private String module; private String operation; private String result; public Date getOperationTime() { return operationTime; } public void setOperationTime(Date operationTime) { this.operationTime = operationTime; } 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 getModule() { return module; } public void setModule(String module) { this.module = module; } 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; } }
持久层及其实现:一般与数据库交互就两种:要么插入日志,要么查询日志。
@Repository("logDao")
public interface LogDao {
public void insert (Log log);
public List<Log> selectByType(String type);
}
<?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="per.lc.sms.dao.LogDao"> <resultMap id="resultMap" type="Log"> <result property="operationTime" column="opr_time" javaType="Date"/> <result property="type" column="type" javaType="String"/> <result property="operator" column="operator" javaType="String"/> <result property="module" column="moudle" javaType="String"/> <result property="operation" column="operation" javaType="String"/> <result property="result" column="result" javaType="String"/> </resultMap> <insert id="insert"> insert into log values(#{operationTime},#{type},#{operator},#{module},#{operation},#{result}) </insert> <select id="selectByType" resultMap="resultMap"> select * from log where type=#{type} order by opr_time desc //按操作时间的倒序排列 </select> </mapper>
业务层及其实现:
public interface LogService { public void addSystemLog(Log log); public void addLoginLog(Log log); public void addOperationLog(Log log); public List<Log> systemLog(); public List<Log> LoginLog(); public List<Log> operationLog(); }
@Service("logService")
public class LogServiceImp implements LogService {
@Autowired
private LogDao logDao;
public void addSystemLog(Log log) {
log.setOperationTime(new Date()); //有些业务规则提前设置好
log.setType("system");
logDao.insert(log);
}
public void addLoginLog(Log log) {
log.setOperationTime(new Date());
log.setType("login");
logDao.insert(log);
}
public void addOperationLog(Log log) {
log.setOperationTime(new Date());
log.setType("operation");
logDao.insert(log);
}
public List<Log> systemLog() {
return logDao.selectByType("system");
}
public List<Log> LoginLog() {
return logDao.selectByType("login");
}
public List<Log> operationLog() {
return logDao.selectByType("operation");
}
}
控制层:接收从页面发来的请求,将各类日志从数据库中查询出来进行展示。也就是它只做了查询的工作,那插入的工作由另外的类来完成。
@Controller("logController")
public class LogController {
@Autowired
private LogService logService;
public void operationLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Log> logList=logService.operationLog();
request.setAttribute("LOGLIST",logList);
request.setAttribute("TYPE","操作");
request.getRequestDispatcher("../log_list.jsp").forward(request,response);
}
public void loginLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Log> logList=logService.LoginLog();
request.setAttribute("LOGLIST",logList);
request.setAttribute("TYPE","登录");
request.getRequestDispatcher("../log_list.jsp").forward(request,response);
}
public void systemLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Log> logList=logService.systemLog();
request.setAttribute("LOGLIST",logList);
request.setAttribute("TYPE","系统");
request.getRequestDispatcher("../log_list.jsp").forward(request,response);
}
}
日志的插入操作;
@Component @Aspect public class LogAdvice { @Autowired private LogService logService; @AfterReturning("execution(* per.lc.sms.controller.*.*(..))&&!execution(* per.lc.sms.controller.selfController.*(..))&&!execution(* per.lc.sms.controller.*.to*(..))") public void operationLog(JoinPoint joinPoint){ Log log=new Log(); log.setModule(joinPoint.getTarget().getClass().getSimpleName()); // 获取目标类的类名 log.setOperation(joinPoint.getSignature().getName()); //获取所执行的方法名 HttpServletRequest request= (HttpServletRequest) joinPoint.getArgs()[0];//获取参数request?获取的就应该是HttpServletRequest类型的啊?怎么还要强制转换 Staff staff= (Staff) request.getSession().getAttribute("staff"); log.setOperator(staff.getAccount()); log.setResult("正常"); logService.addOperationLog(log); } @AfterThrowing(throwing ="e",pointcut = "execution(* per.lc.sms.controller.*.*(..))&&!execution(* per.lc.sms.controller.selfController.*(..))") public void systemLog(JoinPoint joinPoint,Throwable e){ Log log=new Log(); log.setModule(joinPoint.getTarget().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request= (HttpServletRequest) joinPoint.getArgs()[0]; Staff staff= (Staff) request.getSession().getAttribute("staff"); log.setOperator(staff.getAccount()); log.setResult(e.getClass().getSimpleName()); logService.addSystemLog(log); } @AfterReturning("execution(* per.lc.sms.controller.selfController.login(..))") public void loginLog(JoinPoint joinPoint){ Log log=new Log(); log.setModule(joinPoint.getTarget().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request= (HttpServletRequest) joinPoint.getArgs()[0]; Staff staff= (Staff) request.getSession().getAttribute("staff"); if (staff==null){ log.setOperator(request.getParameter("account")); log.setResult("失败"); }else{ log.setOperator(staff.getAccount()); log.setResult("成功"); } logService.addLoginLog(log); } @Before("execution(* per.lc.sms.controller.selfController.logout(..))") public void logoutLog(JoinPoint joinPoint){ Log log=new Log(); log.setModule(joinPoint.getTarget().getClass().getSimpleName()); log.setOperation(joinPoint.getSignature().getName()); HttpServletRequest request= (HttpServletRequest) joinPoint.getArgs()[0]; Staff staff= (Staff) request.getSession().getAttribute("staff"); log.setOperator(staff.getAccount()); log.setResult("成功"); logService.addLoginLog(log); } }
整个项目基本就算是完成了,学了这么久,第一次把一个项目完整的写完跑起来。加油!!

浙公网安备 33010602011771号