在实际开发中需要使用多数据库的情况,druid可以解决这个问题,在配置文件中可以同时配置多个数据源,同时需要设置它们的配置类。这里实现一个例子,同时获取两个数据库中各个的表的信息。
主配置类,这里出现一个bug之前配置别名的时候都是在yml配置文件中配置的,而现在需要在配置类中以sessionFactory.setTypeAliasesPackage("com.shuai.springbootdruid.springdruid.domain");这种形式进行配置。
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = MastDatasourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MastDatasourceConfig {
// 精确到 master 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.shuai.springbootdruid.springdruid.dao.master";
static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
@Value("${master.datasource.url}")
private String url;
@Value("${master.datasource.username}")
private String user;
@Value("${master.datasource.password}")
private String password;
@Value("${master.datasource.driverClassName}")
private String driverClass;
@Bean(name = "masterDataSource")
@Primary
public DataSource masterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MastDatasourceConfig.MAPPER_LOCATION));
sessionFactory.setTypeAliasesPackage("com.shuai.springbootdruid.springdruid.domain");
return sessionFactory.getObject();
}
}
复配置类
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class ClusterDataSourceConfig {
// 精确到 cluster 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.shuai.springbootdruid.springdruid.dao.cluster";
static final String MAPPER_LOCATION = "classpath:mapper/cluster/*.xml";
@Value("${cluster.datasource.url}")
private String url;
@Value("${cluster.datasource.username}")
private String user;
@Value("${cluster.datasource.password}")
private String password;
@Value("${cluster.datasource.driverClassName}")
private String driverClass;
@Bean(name = "clusterDataSource")
public DataSource clusterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "clusterTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(clusterDataSource());
}
@Bean(name = "clusterSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
sessionFactory.setTypeAliasesPackage("com.shuai.springbootdruid.springdruid.domain");
return sessionFactory.getObject();
}
}
配置文件
## master 数据源配置
master.datasource.url=jdbc:mysql://localhost:3306/mypeople?serverTimezone=UTC
master.datasource.username=root
master.datasource.password=3041
master.datasource.driverClassName=com.mysql.cj.jdbc.Driver
## cluster 数据源配置
cluster.datasource.url=jdbc:mysql://localhost:3306/myrole?serverTimezone=UTC
cluster.datasource.username=root
cluster.datasource.password=3041
cluster.datasource.driverClassName=com.mysql.cj.jdbc.Driver
dao层
public interface PeopleDao {
People findByName( String name);
}
public interface RoleDao {
Role findByName(String name);
}
xml配置文件
<mapper namespace="com.shuai.springbootdruid.springdruid.dao.cluster.RoleDao">
<select id="findByName" resultType="Role" parameterType="java.lang.String">
select * from role where name=#{name};
</select>
<mapper namespace="com.shuai.springbootdruid.springdruid.dao.master.PeopleDao">
<select id="findByName" resultType="People" parameterType="java.lang.String">
select * from people where name=#{name};
</select>
</mapper>
service层
@Service
public class PeopleServiceImpl implements PeopleService {
@Autowired
PeopleDao peopleDao;
@Autowired
RoleDao roleDao;
@Override
public People findByName(String name) {
People people= peopleDao.findByName(name);
Role role= roleDao.findByName(name);
people.setRole(role);
return people;
}
}
网友评论