mybatisplus 自定义拦截器
时间: 2023-12-11 20:33:48 浏览: 81
自定义拦截器
Mybatis-Plus是Mybatis的增强工具,在Mybatis的基础上进行了封装,简化了开发流程。Mybatis-Plus同样支持自定义拦截器,下面是自定义拦截器的步骤:
1.实现Interceptor接口
```java
public class MyInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 拦截逻辑
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
// 设置属性
}
}
```
2.添加拦截器注解
```java
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
})
public class MyInterceptor implements Interceptor {
// ...
}
```
3.在Mybatis配置文件中添加拦截器
```xml
<configuration>
<plugins>
<plugin interceptor="com.example.MyInterceptor">
<property name="prop1" value="value1"/>
<property name="prop2" value="value2"/>
</plugin>
</plugins>
</configuration>
```
阅读全文