@Configuration
public class ImpalaDataSourceConfig {
@Value("${impala.drive_class_name}")
private String driverClassName;
@Value("${impala.jdbc_url}")
private String jdbcUrl;
@Value("${impala.mapper-locations}")
private String locations;
@Bean(name = "impalaDataSource")
@Qualifier(value = "impalaDataSource")
public DataSource primaryDataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClassName);
config.setJdbcUrl(jdbcUrl);
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
@Bean(name = "impalaSqlSessionFactory")
public SqlSessionFactory impalaSqlSessionFactory(@Qualifier("impalaDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//此处创建一个Configuration 注意包不要引错了
org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
//配置日志实现
configuration.setLogImpl(StdOutImpl.class);
//此处可以添加其他mybatis配置 例如转驼峰命名
configuration.setMapUnderscoreToCamelCase(true);
//bena工厂装载上面配置的Configuration
bean.setConfiguration(configuration);
//设置mapper配置文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locations));
return bean.getObject();
}
/**创建SqlSessionTemplate*/
@Bean(name = "impalaSqlSessionTemplate")
public SqlSessionTemplate impalaSqlSessionTemplate(@Qualifier("impalaSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
多数据源配置代码
于 2022-06-27 12:58:53 首次发布