com.github.yulichang.config.MPJInterceptorConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-plus-join-core Show documentation
Show all versions of mybatis-plus-join-core Show documentation
An enhanced toolkit of Mybatis-Plus to simplify development.
package com.github.yulichang.config;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.yulichang.interceptor.MPJInterceptor;
import com.github.yulichang.toolkit.InterceptorList;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.InterceptorChain;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import java.lang.reflect.Field;
import java.util.List;
/**
* 兼容 page helper 插件类
*
* @author yulichang
*/
public class MPJInterceptorConfig {
private static final Log logger = LogFactory.getLog(MPJInterceptorConfig.class);
public MPJInterceptorConfig(List sqlSessionFactoryList, Boolean banner) {
replaceInterceptorChain(sqlSessionFactoryList);
if (banner) {
//打印banner
System.out.println(" _ _ |_ _ _|_. ___ _ | _ . _ . _ \n" +
"| | |\\/|_)(_| | |_\\ |_)||_|_\\ | (_) | | | \n" +
" / | /\n" +
" 1.4.5");
}
}
@SuppressWarnings("unchecked")
private void replaceInterceptorChain(List sqlSessionFactoryList) {
if (CollectionUtils.isEmpty(sqlSessionFactoryList)) {
return;
}
for (SqlSessionFactory factory : sqlSessionFactoryList) {
try {
Field interceptorChain = Configuration.class.getDeclaredField("interceptorChain");
interceptorChain.setAccessible(true);
InterceptorChain chain = (InterceptorChain) interceptorChain.get(factory.getConfiguration());
Field interceptors = InterceptorChain.class.getDeclaredField("interceptors");
interceptors.setAccessible(true);
List list = (List) interceptors.get(chain);
if (CollectionUtils.isEmpty(list)) {
interceptors.set(chain, new InterceptorList<>());
} else {
interceptors.set(chain, new InterceptorList<>(list));
}
chain.addInterceptor(new MPJInterceptor());
} catch (NoSuchFieldException | IllegalAccessException e) {
logger.error("初始化 MPJ 拦截器失败");
e.printStackTrace();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy