1. 配置数据库信息
spring.datasource.girl-images.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.girl-images.username=dba_gxm
spring.datasource.girl-images.password=root
spring.datasource.girl-images.jdbc-url=jdbc:mysql://localhost:3306/girl_images?useUnicode=true&serverTimezone=Hongkong&characterEncoding=UTF-8
spring.datasource.myimages.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.myimages.username=dba_myimages
spring.datasource.myimages.password=root
spring.datasource.myimages.jdbc-url=jdbc:mysql://localhost:3306/myimages?useUnicode=true&serverTimezone=Hongkong&characterEncoding=UTF-8
2.不同数据库对应的mapper和pojo放在不同的包中
package.png3.创建配置类
数据库1:
GirlImagesDataSourceConfig.java:
@Configuration
@MapperScan(basePackages = "com.gongxm.images.mapper",sqlSessionFactoryRef = "girlImagesSqlSessionFactory")
public class GirlImagesDataSourceConfig {
//配置数据源
@Bean(name = "girlImagesDataSource")
@ConfigurationProperties(prefix = "spring.datasource.girl-images")
@Primary
public DataSource getDataSource() {
return DataSourceBuilder.create().build();
}
//创建SqlSessionFactory, 需要数据源作为参数
@Bean(name = "girlImagesSqlSessionFactory")
@Primary
public SqlSessionFactory getSqlSessionFactory(@Qualifier("girlImagesDataSource")DataSource ds) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds);
return factoryBean.getObject();
}
//配置事务管理器, 需要数据源作为参数
@Bean(name = "girlImagesTX")
@Primary
public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("girlImagesDataSource")DataSource ds) {
return new DataSourceTransactionManager(ds);
}
//配置SqlSessionTemplate
@Bean(name = "girlImagesSqlSessionTemplate")
@Primary
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("girlImagesSqlSessionFactory")SqlSessionFactory sf) {
return new SqlSessionTemplate(sf);
}
}
数据库2:
MyImagesDataSourceConfig.java:
@Configuration
@MapperScan(basePackages = "com.gongxm.myimages.mapper",sqlSessionFactoryRef = "myimagesSqlSessionFactory")
public class MyImagesDataSourceConfig {
//配置数据源
@Bean(name = "myimagesDataSource")
@ConfigurationProperties(prefix = "spring.datasource.myimages")
public DataSource getDataSource() {
return DataSourceBuilder.create().build();
}
//创建SqlSessionFactory, 需要数据源作为参数
@Bean(name = "myimagesSqlSessionFactory")
public SqlSessionFactory getSqlSessionFactory(@Qualifier("myimagesDataSource")DataSource ds) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds);
return factoryBean.getObject();
}
//配置事务管理器, 需要数据源作为参数
@Bean(name = "myimagesTX")
public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("myimagesDataSource")DataSource ds) {
return new DataSourceTransactionManager(ds);
}
//配置SqlSessionTemplate
@Bean(name = "myimagesSqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("myimagesSqlSessionFactory")SqlSessionFactory sf) {
return new SqlSessionTemplate(sf);
}
}
配置完成, 这样配置后, 调用对应的mapper对象, 它就会自动使用对应的数据库了
注意事项:
porperties文件:
一个数据源的jdbc地址使用url来配置
多数据源中的jdbc地址使用jdbc-url来配置
网友评论