package com.newyetai.mybatis.config; import java.util.List; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.type.TypeHandlerRegistry; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration; import com.newyetai.mybatis.handle.BaseEnumTypeHandler; import com.newyetai.mybatis.interceptor.MyPageInterceptor; import lombok.extern.slf4j.Slf4j; /** * @author guhong * @date 2019/8/19 * 设置mapper扫描的包 */ @Configuration @Slf4j public class MybatisConfig { @Autowired private List sqlSessionFactoryList; /** * 添加自定义的分页插件,pageHelper 的分页插件PageInterceptor是用@PostConstruct添加的,自定义的应该在其后面添加 * 真正执行时顺序是反过来,先执行MyPageInterceptor,再执行 PageInterceptor * * 所以要保证 PageHelperAutoConfiguration 先执行 */ @Autowired public void addPageInterceptor(PageHelperAutoConfiguration pageHelperAutoConfiguration) { MyPageInterceptor interceptor = new MyPageInterceptor(); for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) { sqlSessionFactory.getConfiguration().addInterceptor(interceptor); log.info("注册自定义分页插件成功"); // 取得类型转换注册器 TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry(); // 注册默认枚举转换器 typeHandlerRegistry.setDefaultEnumTypeHandler(BaseEnumTypeHandler.class); } } }