repository和mapper(java怎么拦截mapper方法)
本文目录
java怎么拦截mapper方法
1、使用@Aspect注解
要去使用jdk的代理,否则代理不了mapper(即mybatis代理的mapper没有默认的构造器,cglib无法再给这个代理构造代理,会报如下错误*****: Could not generate CGLIB subclass of class : Common causes of this problem include using a final class or a non-visible class; nested exception is *****: Cannot subclass final class class *****.$Proxy13)
《!-- 启动对@Aspectj的支持 true为cglib,false为jdk代理,为true的话,会导致拦截不了mybatis的mapper--》
《aop:aspectj-autoproxy proxy-target-class="false" /》
之后就是使用注解去配置拦截,进行修改记录的操作
@Aspect
@Component
public class DatalogAspect {
private static final Logger logger = *****(*****);
@Resource
private ActionMapper actionMapper;
@Pointcut("execution(public * *****.*.insert*(..)) && !execution(public * ******(..))")
public void insert(){
}
@Pointcut("execution(public * *****.*.update*(..))")
public void update(){
}
@Pointcut("execution(public * *****.*.delete*(..))")
public void delete(){
}
@Around("insert() || update() || delete()")
public Object addOperateLog(ProceedingJoinPoint pjp) throws Throwable {
...
}
}
2、使用MethodInterceptor
使用aopalliance的MethodInterceptor
(1)配置文件
去掉对@Aspect注解的支持(也可以不去掉,只要不是proxy-target-class = true就可以)。然后配置aop
《bean id="datalogInterceptor" class="*****" /》
《aop:config》
《aop:pointcut id="datalogInsertPointCut" expression="execution(* *****..insert*(..)) && !execution(* *****.*(..))" /》
《aop:pointcut id="datalogUpdatePointCut" expression="execution(* *****..update*(..)) && !execution(* *****.*(..))" /》
《aop:pointcut id="datalogDeletePointCut" expression="execution(* *****..delete*(..)) && !execution(* *****.*(..))" /》
《aop:advisor advice-ref="datalogInterceptor" pointcut-ref="datalogInsertPointCut" /》
《aop:advisor advice-ref="datalogInterceptor" pointcut-ref="datalogUpdatePointCut" /》
《aop:advisor advice-ref="datalogInterceptor" pointcut-ref="datalogDeletePointCut" /》
《/aop:config》
(2)实现MethodInterceptor
public class DatalogInterceptor implements MethodInterceptor{
private static final Logger logger = *****(*****);
public DatalogInterceptor() {
}
@Resource
private ActionMapper actionMapper;
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Method method = *****();
String methodName = *****();
Class《?》 cls = *****();
Object service = *****();
Object args = *****();
Integer actionType = -1;
...
}
}
@mybatisrepository什么意思
应该是自定义的注解,方便*****的扫描。
MyBatis是一个支持普通**L查询,存储过程和高级映射的优秀持久层框架。
repository一般作为持久层的Dao的命名
例子:
@Retention(*****)
@Target(*****)
@Documented
@Component
public @inte***ce MyBatisRepository {
String value() default "";
}
更多文章:
length函数c++中怎么用(s.length()的C++代码是什么意思)
2026年4月30日 21:40
vue textarea高度自适应(随着输入文字的多少,textarea自动变化高度)
2026年4月30日 21:00
repository和mapper(java怎么拦截mapper方法)
2026年4月30日 20:00
计算机二级c语言题库有必要算刷完吗(2021年计算机二级C语言选择题必须对20道(一半)才能过吗)
2026年4月30日 19:20
exploit的名词(a sense of achievement为什么不加s)
2026年4月30日 19:00
chimney(这英文单词 chimney / dictionary 怎么读)
2026年4月30日 18:40




