Druid配置多数据源

夏沐沐

关注

阅读 155

2022-03-18

@Slf4j
@Configuration
@MapperScan(basePackages = MerchantDruidConfig.PACKAGE, sqlSessionTemplateRef = "merchantSqlSessionTemplate")
public class MerchantDruidConfig {

    static final String PACKAGE = "com.xx.mapper";

    static final String MAPPER_LOCATION = "classpath*:xml/*.xml";

    @Autowired
    @Qualifier(DruidConfig.MERCHANT_DATASOURCE_BEAN_NAME)
    private DataSource merchant;

    @Bean("merchantSqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(merchant);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        factoryBean.setConfiguration(configuration);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MerchantDruidConfig.MAPPER_LOCATION));
        return factoryBean.getObject();
    }

    @Bean("merchantSqlSessionTemplate")
    public SqlSessionTemplate getSqlSessionTemplate() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(getSqlSessionFactory());
        return template;
    }


}

精彩评论(0)

0 0 举报