content: package com.xiliu.server.common.mybatis.config;
  
  import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
  import com.baomidou.mybatisplus.annotation.DbType;
  import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
  import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
  import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
  import com.xiliu.server.common.mybatis.handler.MyTenantLineHandler;
  import org.mybatis.spring.annotation.MapperScan;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import javax.sql.DataSource;
  
  
  /**
   * @author xiliu
   * @description mybatis-plus配置类
   * @date 2021/8/30
   */
  @Configuration
  @MapperScan("com.xiliu.**.mapper")
  @EnableTransactionManagement
  public class MybatisPlusConfig {
  
      /**
       * druid注入
       * @return dataSource
       */
      @Bean(name = "dataSource")
      @ConfigurationProperties(prefix = "spring.datasource.druid")
      public DataSource dataSource() {
          return DruidDataSourceBuilder.create().build();
      }
  
      /**
       * 配置事物管理器
       * @return DataSourceTransactionManager
       */
      @Bean(name="transactionManager")
      public DataSourceTransactionManager transactionManager(){
          return new DataSourceTransactionManager(dataSource());
      }
  
      @Bean
      public MybatisPlusInterceptor mybatisPlusInterceptor() {
          MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
          //租户插件
          interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new MyTenantLineHandler()));
          // 分页插件
          interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
          // 乐观锁插件
          interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
          // 阻断插件
          interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
          return interceptor;
      }
  
      /**
       * 乐观锁
       */
      @Bean
      public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
          return new OptimisticLockerInnerInterceptor();
      }
      /**
       * 防止全表更新与删除
       */
      @Bean
      public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
          return new BlockAttackInnerInterceptor();
      }
  }
  