ssm设置日志拦截器写入日志信息至数据库

新建maven项目,pom.xml中jar包如下:


  4.0.0
  cn.jzj
  easyuiDemo
  0.0.1-SNAPSHOT
  war
 
   
        org.springframework
        spring-context
        5.0.5.RELEASE
   

   

    org.springframework
    spring-webmvc
    5.0.5.RELEASE



    org.hibernate
    hibernate-core
    5.2.13.Final



    commons-fileupload
    commons-fileupload
    1.3.3



    com.google.code.gson
    gson
    2.8.2



    org.aspectj
    aspectjrt
    1.9.0


    org.springframework
    spring-tx
    5.0.5.RELEASE


    mysql
    mysql-connector-java
    5.1.38


    org.mybatis
    mybatis-spring
    1.3.2


    org.springframework
    spring-jdbc
    5.0.5.RELEASE


    org.mybatis
    mybatis
    3.4.6


    org.springframework
    spring-test
    5.0.5.RELEASE
    test


jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


    fhgfjf

web.xml:



  Hello
 
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
 

   
    HelloWorld
    org.springframework.web.servlet.DispatcherServlet
   
      contextConfigLocation
      classpath:applicationContext.xml
   

    1
 

 
    HelloWorld
    *.action
 

applicationContext.xml:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


     
   

   
   

class="org.springframework.jdbc.datasource.DriverManagerDataSource">





   









 
      
      
          
          
          
   
  
   





设置日志注解:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BusinessOption {
String moduleName() default "";
String menuName() default "";

}

HelloWorld:

package cn.jzj;




import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;






@Controller("/Hello")
@RequestMapping("/Hello")
@Configuration
public class Helloworld {

@Autowired  
private UserMapper userMapper;
@BusinessOption(moduleName="的说法都是", menuName="谁不是")
@RequestMapping(value="/hello")
public String helloWorld() {

return "success";
}

}

UserMapper.java:

import java.util.List;


public interface UserMapper {
void save(User user);
void update(User user);
void delete(int id);
User findById(int id);
List findAll();

}

UserMapper.xml:














insert into user_log(name,desc1)
values(#{name},#{desc})




update user_log
set name = #{name},
desc1 = #{desc},
where id = #{id}



delete from user_log
where id = #{id}






LogIntercept:

package cn.jzj;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import cn.jzj.UserMapper;






@Component
public class LogInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private UserMapper userMapper;
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
super.afterCompletion(request, response, handler, ex);
}


@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
super.afterConcurrentHandlingStarted(request, response, handler);
}


@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
HandlerMethod hm =  (HandlerMethod) handler;//将其强转过来  
        BusinessOption businessOption = hm.getMethodAnnotation(BusinessOption.class);
String s=String.format("操作了【%s】模块下【%s】功能",  businessOption.moduleName(), businessOption.menuName());
        User user=new User();
        user.setName("jzj");
        user.setDesc(s);
        userMapper.save(user);

super.postHandle(request, response, handler, modelAndView);
}


@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
return super.preHandle(request, response, handler);
}
}

你可能感兴趣的:(ssm设置日志拦截器写入日志信息至数据库)