内容使用的是druidDataSource
首先,在application.properties中配置数据源
spring.datasource.data1.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.data1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.data1.jdbc-url=jdbc:mysql://12.99.123.123:3306/db1?characterEncoding=UTF-8
spring.datasource.data1.username=root
spring.datasource.data1.password=123456
spring.datasource.data2.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.data2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.data2.jdbc-url=jdbc:mysql://12.99.123.123:3306/db2?characterEncoding=UTF-8
spring.datasource.data2.username=root
spring.datasource.data2.password=123456
注意:
为什么用的是jdbc-url和driver-class-name而不是url和driver-class
因为如果用后者可能会报错:
jdbcUrl is required with driverClassName这个错误
然后构建两个配置类
@Configuration
@MapperScan(basePackage="com.bsym.web.mapper.primary",sqlSessionFactory="data1SqlSessionFactory")
public class DataSourceConfig{
//返回db1数据库的数据源(这个作为主库)
@Bean(name="data1Source")
@ConfigurationProperties(prefix="spring.datasource.data1")
@Primary
public DataSource getDataSource(){
return DataSource.creater().build();
}
//返回db1数据库的会话工厂
@Bean(name="data1SqlSessionFactory")
public SqlSessionFactory getSqlSessionFactory(@Qualifier("data1Source") DataSource dataSouorce)throws Exception{
SqlSessionFactory sqlSessionFactory = new SqlSessionFactory();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));//可以不写,在properties中用mybatis配置mapper的扫描
sqlSessionFactory.setTypeAliasesPackage("com.bsym.web.entity");
return sqlSessionFactoryBean.getObject;
}
//返回db1数据库的会话模板
@Bean(name="dataTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data1SqlSessionFactory")SqlSessionFactory ssf)throws Expection{
return new SqlSessionTemplate(ssf);
}
//返回db1数据库的事务
@Bean(name = "data1TransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("data1Source")DataSource dataSource){
return new DataSourceTransactionManager(dataSource)
}
}
@Configuration
@MapperScan(basePackage="com.bsym.web.mapper.second",sqlSessionFactory="data2SqlSessionFactory")
public class Data2SourceConfig{
//返回db1数据库的数据源(这个作为主库)
@Bean(name="data2Source")
@ConfigurationProperties(prefix="spring.datasource.data2")
public DataSource getDataSource(){
return DataSource.creater().build();
}
//返回db2数据库的会话工厂
@Bean(name="data2SqlSessionFactory")
public SqlSessionFactory getSqlSessionFactory(@Qualifier("data2Source") DataSource dataSouorce)throws Exception{
SqlSessionFactory sqlSessionFactory = new SqlSessionFactory();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));//可以不写,在properties中用mybatis配置mapper的扫描
sqlSessionFactory.setTypeAliasesPackage("com.bsym.web.entity");
return sqlSessionFactoryBean.getObject;
}
//返回db1数据库的会话模板
@Bean(name="data2Template")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data2SqlSessionFactory")SqlSessionFactory ssf)throws Expection{
return new SqlSessionTemplate(ssf);
}
//返回db2数据库的事务
@Bean(name = "data1TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("data2Source")DataSource dataSource){
return new DataSourceTransactionManager(dataSource)
}
}
配置完成后就可以启动了