配置文件
spring:
#关键词检索、5G网关组件配置
datasource:
name: test
type: com.alibaba.druid.pool.DruidDataSource
# #本地
db:
url: jdbc:mysql://localhost:3306/5g?allowMultiQueries=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
db2:
url: jdbc:mysql://localhost:3306/5gg?allowMultiQueries=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
写两个类接收配置文件的参数
@Component
@ConfigurationProperties(prefix = "spring.datasource.db")
public class DB {
private static final Logger logger = LoggerFactory.getLogger(DB.class);
private String url;
private String username;
private String password;
private String driverClassName;
省略set,get
}
@Component
@ConfigurationProperties(prefix = "spring.datasource.db2")
public class DB2 {
private static final Logger logger = LoggerFactory.getLogger(DB2.class);
private String url;
private String username;
private String password;
private String driverClassName;
省略set,get
}
写两个配置类
@Configuration
//只从org.cmic.rcs.dao.db包下读取dao文件,并且该报下的dao使用本datasource
@MapperScan(basePackages = "org.cmic.rcs.dao.db", sqlSessionTemplateRef = "dbSqlSessionTemplate")
public class DBConfig {
private static final Logger logger = LoggerFactory.getLogger(DBConfig.class);
@Bean(name = "dbDataSource")
//对于多数据源,必须制定primary,否则报错有2个datasource,并且,只能制定一个primary
@Primary
//从db从获取配置信息
public DataSource setDataSource(DB dbc) {
DruidDataSource ds = new DruidDataSource();
ds.setUrl(dbc.getUrl());
ds.setUsername(dbc.getUsername());
ds.setPassword(dbc.getPassword());
ds.setDriverClassName(dbc.getDriverClassName());
return ds;
}
@Bean(name = "dbSqlSessionFactory")
@Primary
public SqlSessionFactory setSqlSessionFactory(@Qualifier("dbDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//指定mapper.xml文件存放位置
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:org/cmic/rcs/dao/db/*.xml"));
return bean.getObject();
}
@Bean(name = "dbSqlSessionTemplate")
@Primary
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("dbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
//只从org.cmic.rcs.dao.db2包下读取dao文件,并且该报下的dao使用本datasource
@MapperScan(basePackages = "org.cmic.rcs.dao.db2", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DB2Config {
private static final Logger logger = LoggerFactory.getLogger(DB2Config.class);
@Bean(name = "db2DataSource")
public DataSource setDataSource(DB2 dbc) {
DruidDataSource ds = new DruidDataSource();
ds.setUrl(dbc.getUrl());
ds.setUsername(dbc.getUsername());
ds.setPassword(dbc.getPassword());
ds.setDriverClassName(dbc.getDriverClassName());
return ds;
}
@Bean(name = "db2SqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//设置读取xml文件的位置
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:org/cmic/rcs/dao/db2/*.xml"));
return bean.getObject();
}
@Bean(name = "db2SqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
对应的dao写两个
对应的mapper写两个
总体图