dongyukun
9 天以前 e88fba9645a57535d858ce48da8e9d9a3dc84adc
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.mybatis.config;
H 2
3 import cn.hutool.core.util.StrUtil;
4 import com.iailab.framework.mybatis.core.enums.SqlConstants;
5 import com.iailab.framework.mybatis.core.handler.DefaultDBFieldHandler;
6 import com.baomidou.mybatisplus.annotation.DbType;
7 import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
8 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
9 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
10 import com.baomidou.mybatisplus.extension.incrementer.*;
11 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
12 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
13 import org.apache.ibatis.annotations.Mapper;
14 import org.mybatis.spring.annotation.MapperScan;
15 import org.springframework.boot.autoconfigure.AutoConfiguration;
16 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
17 import org.springframework.context.annotation.Bean;
18 import org.springframework.core.env.ConfigurableEnvironment;
19
20 /**
21  * MyBaits 配置类
22  *
23  * @author iailab
24  */
25 @AutoConfiguration(before = MybatisPlusAutoConfiguration.class) // 目的:先于 MyBatis Plus 自动配置,避免 @MapperScan 可能扫描不到 Mapper 打印 warn 日志
26 @MapperScan(value = "${iailab.info.base-package}", annotationClass = Mapper.class,
27         lazyInitialization = "${mybatis.lazy-initialization:false}") // Mapper 懒加载,目前仅用于单元测试
28 public class IailabMybatisAutoConfiguration {
29
30     @Bean
31     public MybatisPlusInterceptor mybatisPlusInterceptor() {
32         MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
33         mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件
34         return mybatisPlusInterceptor;
35     }
36
37     @Bean
38     public MetaObjectHandler defaultMetaObjectHandler(){
39         return new DefaultDBFieldHandler(); // 自动填充参数类
40     }
41
42     @Bean
43     @ConditionalOnProperty(prefix = "mybatis-plus.global-config.db-config", name = "id-type", havingValue = "INPUT")
44     public IKeyGenerator keyGenerator(ConfigurableEnvironment environment) {
45         DbType dbType = IdTypeEnvironmentPostProcessor.getDbType(environment);
46         if (dbType != null) {
47             switch (dbType) {
48                 case POSTGRE_SQL:
49                     return new PostgreKeyGenerator();
50                 case ORACLE:
51                 case ORACLE_12C:
52                     return new OracleKeyGenerator();
53                 case H2:
54                     return new H2KeyGenerator();
55                 case KINGBASE_ES:
56                     return new KingbaseKeyGenerator();
57                 case DM:
58                     return new DmKeyGenerator();
59             }
60         }
61         // 找不到合适的 IKeyGenerator 实现类
62         throw new IllegalArgumentException(StrUtil.format("DbType{} 找不到合适的 IKeyGenerator 实现类", dbType));
63     }
64
65 }