Taking the login user ID as an example, add it to the interceptor and get the parameters in the file through #{currentUserId} or ${currentUserId}.
1. Interceptor sample code
package ; import ; import ; import .slf4j.Slf4j; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Global parameter interceptor * * @author */ @Slf4j @Intercepts({ @Signature(type = , method = "query", args = {, , , }), @Signature(type = , method = "query", args = {, , , , , })} ) public class GlobalParametersInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = (); Object params = args[1]; if (params instanceof BaseEntity) { BaseEntity baseEntity = (BaseEntity) params; (getCurrentUserId()); } else if (params instanceof ) { <Object> map = () params; ("currentUserId", getCurrentUserId()); } ()[1] = params; return (); } /** * Get the current logged-in user ID * * @return User ID */ private String getCurrentUserId() { try { return ().toString(); } catch (Exception ignored) { return null; } } }
2. Interceptor configuration
Note: If the PageHelper pager is referenced in the project, this method will fail.
package ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Interceptor configuration * * @author */ @Configuration @ConditionalOnBean({}) @RequiredArgsConstructor(onConstructor_ = {@Autowired}) public class MybatisInterceptorConfig { private final List<SqlSessionFactory> sqlSessionFactories; @PostConstruct public void addPageInterceptor() { GlobalParametersInterceptor globalParametersInterceptor = new GlobalParametersInterceptor(); for (SqlSessionFactory sqlSessionFactory : sqlSessionFactories) { ().addInterceptor(globalParametersInterceptor); } } }
3. PageHelper Interceptor Configuration
Mybatis interceptor adopts the chain of responsibility mode. Generally, the interceptor method is executed in the intercept method. The PageInterceptor paging device does not pass parameters backwards but executes the query method. Therefore, it is necessary to place the custom interceptor behind the PageInterceptor (PS: The last interceptor added is executed first).
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Interceptor configuration * * @author */ @Configuration @RequiredArgsConstructor(onConstructor_ = {@Autowired}) public class MybatisInterceptorConfig implements BeanPostProcessor { private final List<SqlSessionFactory> sqlSessionFactories; @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof PageHelperAutoConfiguration) { GlobalParametersInterceptor globalParametersInterceptor = new GlobalParametersInterceptor(); for (SqlSessionFactory sqlSessionFactory : sqlSessionFactories) { ().addInterceptor(globalParametersInterceptor); } } return bean; } }
This is the end of this article about the implementation of adding parameters to mybatis interceptor. For more related content to adding parameters to mybatis interceptor, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!