package com.zhgd.config;


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 com.zhgd.mybatis.DataScopeHandler;
import com.zhgd.mybatis.DataScopeInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;

/**
 *  Mybatis-Plus进行分页
 *  返回的数据中total总是为0问题
 */
@Configuration
public class MybatisPlusConfig {
    /**
     *   mybatis-plus分页插件
     */
    /*@Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        return page;
    }*/

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        DataScopeInterceptor dataScopeInterceptor = new DataScopeInterceptor();
        dataScopeInterceptor.setDataScopeHandler(dataScopeHandler());
        interceptor.addInnerInterceptor(dataScopeInterceptor);
        return interceptor;
    }


    @Bean
    @ConditionalOnMissingBean
    public DataScopeHandler dataScopeHandler() {
        return new DataScopeHandler();
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);

    }
















}