| | |
| | | package com.dy.common.mybatis; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.DbType; |
| | | import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; |
| | | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; |
| | | import jakarta.annotation.PostConstruct; |
| | | import org.apache.ibatis.session.SqlSessionFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | @org.springframework.context.annotation.Configuration |
| | | import java.util.List; |
| | | |
| | | @Configuration |
| | | public class MyBatisConfig { |
| | | @Autowired |
| | | private List<SqlSessionFactory> sqlSessionFactoryList; |
| | | |
| | | @Value("${mybatis-plus.configuration.print-sql}") |
| | | private boolean printSql; |
| | | |
| | | /** |
| | | * 两个拦截器,自动生成ID,异常时输出SQL |
| | | */ |
| | | @PostConstruct |
| | | public void addMyInterceptor() { |
| | | for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) { |
| | | org.apache.ibatis.session.Configuration conf = sqlSessionFactory.getConfiguration(); |
| | | conf.addInterceptor(new AutoGenerateIdInterceptor()); |
| | | conf.addInterceptor(new PrintExceptionSqlInterceptor()); |
| | | if (printSql) { |
| | | conf.addInterceptor(new PrintSqlInterceptor()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * mybatisPlus的分面插件 |
| | | * @return 拦截器 |
| | | */ |
| | | @Bean |
| | | public PaginationInnerInterceptor paginationInnerInterceptor(){ |
| | | return new PaginationInnerInterceptor() ; |
| | | } |
| | | |
| | | |
| | | /* |
| | | @Value("${mybatis-plus.configuration.print-sql}") |
| | | private boolean printSql; |
| | | |
| | | @Bean |
| | | public ConfigurationCustomizer mybatisConfigurationCustomizer() { |
| | | return configuration -> { |
| | | configuration.addInterceptor(new AutoGenerateIdInterceptor()); |
| | | configuration.addInterceptor(new PrintExceptionSqlInterceptor()); |
| | | }; |
| | | /* |
| | | return new ConfigurationCustomizer() { |
| | | @Override |
| | | public void customize(MybatisConfiguration configuration) { |
| | | configuration.addInterceptor(new AutoGenerateIdInterceptor()); |
| | | configuration.addInterceptor(new PrintExceptionSqlInterceptor()); |
| | | if (printSql) { |
| | | configuration.addInterceptor(new PrintSqlInterceptor()); |
| | | } |
| | | }; |
| | | */ |
| | | //return new ConfigurationCustomizer() { |
| | | // @Override |
| | | // public void customize(MybatisConfiguration configuration) { |
| | | // configuration.addInterceptor(new AutoGenerateIdInterceptor()); |
| | | // configuration.addInterceptor(new PrintExceptionSqlInterceptor()); |
| | | // } |
| | | //}; |
| | | } |
| | | |
| | | @Bean |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| | | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| | | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加 |
| | | // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType |
| | | return interceptor; |
| | | } |
| | | */ |
| | | } |