zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.changhong.epc.config.datasource;
 
import com.changhong.epc.constter.system.DataSourceKey;
import com.iemsoft.framework.cloud.config.mybatis.MapperConfig;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.MappedStatement;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
 
import java.util.HashMap;
import java.util.Map;
 
@Configuration
@AutoConfigureAfter(DataSourceConfig.class)
@Slf4j
public class MybatisConfig extends MapperConfig implements DataSourceKey{
 
    /**
     * 多数据源改成单数据源
     * @author WangYX
     *
     */
    private static Map<String, String> DATA_SOURCE = new HashMap<>();
    
    static{
        DATA_SOURCE.put("^com[.]changhong[.]epc[.][^.]+[.]mapper[.]centen.+", ADMIN_DATA_SOURCE);
    }
 
    private static void setDataSource(MappedStatement ms, String id) {
        for(Map.Entry<String, String> entry : DATA_SOURCE.entrySet()){
            if(id.matches(entry.getKey())){
                ThreadData.set(Keys.DATA_SOURCE, entry.getValue());
                break;
            }
        }
    }
 
    @Override
    public MapperScannerConfigurer mapperScannerConfigurer(Environment environment) {
        log.debug(environment.getProperty(getBasePackageKey()));
        return super.mapperScannerConfigurer(environment);
    }
 
}