mybatis-plus在application.yml中如何使自定义拦截器生效
时间: 2023-05-22 20:07:15 浏览: 311
可以在application.yml中配置mybatis-plus.interceptor属性,指定自定义拦截器的类名即可使其生效。比如:
mybatis-plus:
interceptor:
- com.example.CustomInterceptor
相关问题
mybatis配置拦截器实现分页
### MyBatis 拦截器实现分页功能
#### 描述分页需求
为了满足应用中的分页显示需求,可以利用MyBatis的`Interceptor`接口来自定义插件,从而在执行SQL语句之前修改原始SQL以加入分页参数。通过这种方式可以在不改变原有Mapper XML文件的情况下完成对查询结果集的分页处理。
#### 创建自定义拦截器类
创建一个新的Java类继承`org.apache.ibatis.plugin.Interceptor`并重写其中的方法:
```java
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.*;
import java.sql.Connection;
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class PaginationInterceptor implements Interceptor {
private static final String SQL_COUNT_PREFIX = "SELECT COUNT(0) FROM ( ";
public Object intercept(Invocation invocation) throws Throwable {
// 获取当前要执行的真实sql以及相关参数对象
final StatementHandler statement = (StatementHandler)invocation.getTarget();
BoundSql boundSql = statement.getBoundSql();
Connection connection = (Connection)invocation.getArgs()[0];
// 这里可以根据业务逻辑获取分页所需的参数,比如currentPage和pageSize
int currentPage = ...; // 用户传入的具体值
int pageSize = ...; // 用户传入的具体值
// 修改原生SQL为带有LIMIT子句的形式(适用于MySQL)
String originalSql = boundSql.getSql().trim();
String pageSql = originalSql + " LIMIT " + ((currentPage - 1)*pageSize) + "," + pageSize;
// 如果需要统计总记录数,则可以通过包装后的SQL再次查询总数
String countSql = SQL_COUNT_PREFIX + originalSql + ") AS total";
// 更新boundSql内的SQL字符串以便后续操作使用新的pageSql
ReflectionUtils.setField(boundSql.getClass(), boundSql, "sql", pageSql);
return invocation.proceed();
}
public Object plugin(Object target){
return Plugin.wrap(target,this);
}
}
```
此代码片段展示了如何编写一个简单的分页拦截器[^1]。需要注意的是,在实际项目开发过程中应当更加严谨地对待输入验证等问题,并且考虑到不同数据库产品之间语法差异可能带来的兼容性挑战。
#### 配置拦截器到Spring Boot环境中
当上述拦截器完成后,还需将其注册至应用程序上下文中才能生效。假设正在使用的框架是Spring Boot,则可在application.yml中添加如下配置项:
```yaml
mybatis-plus:
configuration:
interceptors:
- com.example.demo.PaginationInterceptor
```
或者采用编程的方式向`Configuration`实例注入该拦截器:
```java
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer myBatisConfigurationCustomizer() {
return new ConfigurationCustomizer() {{
addInterceptor(new PaginationInterceptor());
}};
}
}
```
这样就完成了整个分页机制的设计与实现了[^2]。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)