0
点赞
收藏
分享

微信扫一扫

Druid配置多数据源

夏沐沐 2022-03-18 阅读 155
@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 条评论