|  |  |  | 
|---|
|  |  |  | package com.dy.common.mybatis; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private List<SqlSessionFactory> sqlSessionFactoryList; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${mybatis-plus.configuration.print-sql}") | 
|---|
|  |  |  | private boolean printSql; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 两个拦截器,自动生成ID,异常时输出SQL | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @PostConstruct | 
|---|
|  |  |  | public void addMyInterceptor() { | 
|---|
|  |  |  | for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) { | 
|---|
|  |  |  | sqlSessionFactory.getConfiguration().addInterceptor(new AutoGenerateIdInterceptor()); | 
|---|
|  |  |  | sqlSessionFactory.getConfiguration().addInterceptor(new PrintExceptionSqlInterceptor()); | 
|---|
|  |  |  | if (printSql) { | 
|---|
|  |  |  | sqlSessionFactory.getConfiguration().addInterceptor(new PrintSqlInterceptor()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * mybatisPlus的分面插件 | 
|---|
|  |  |  | * @return 拦截器 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public PaginationInnerInterceptor paginationInnerInterceptor(){ | 
|---|
|  |  |  | return new PaginationInnerInterceptor() ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|