使用gradle和springboot进行多模块开发,只有主模块有Application.class的main方法,且主模块有2个DataSource数据源,每个模块都有各自的mybatis的mapper文件,怎么指定子模块的mapper文件使用固定的第2个数据源?
时间: 2024-03-12 09:46:58 浏览: 41
基于springboot,采用mybatis和mapper3插件,基于shiro的sso cookies单机实现+源代码+文档说
可以在每个子模块的配置文件中指定使用哪一个数据源。
首先,在主模块的配置文件中定义两个数据源,例如:
```
spring.datasource.primary.url=jdbc:mysql://localhost:3306/db1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
```
接着,在主模块中定义两个数据源的bean:
```java
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
```
然后,在每个子模块的配置文件中指定使用哪一个数据源和mapper文件位置,例如:
```
mybatis.mapper-locations=classpath:mapper/module1/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.name=secondaryDataSource
```
```
mybatis.mapper-locations=classpath:mapper/module2/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.name=primaryDataSource
```
最后,在每个子模块中定义对应的SqlSessionFactoryBean和MapperScannerConfigurer:
```java
@Configuration
public class Module1Config {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource dataSource;
@Bean(name = "module1SqlSessionFactory")
public SqlSessionFactoryBean module1SqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/module1/*.xml"));
return sessionFactory;
}
@Bean(name = "module1MapperScannerConfigurer")
public MapperScannerConfigurer module1MapperScannerConfigurer() {
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
scannerConfigurer.setBasePackage("com.example.module1.mapper");
scannerConfigurer.setSqlSessionFactoryBeanName("module1SqlSessionFactory");
return scannerConfigurer;
}
}
```
```java
@Configuration
public class Module2Config {
@Autowired
@Qualifier("primaryDataSource")
private DataSource dataSource;
@Bean(name = "module2SqlSessionFactory")
public SqlSessionFactoryBean module2SqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/module2/*.xml"));
return sessionFactory;
}
@Bean(name = "module2MapperScannerConfigurer")
public MapperScannerConfigurer module2MapperScannerConfigurer() {
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
scannerConfigurer.setBasePackage("com.example.module2.mapper");
scannerConfigurer.setSqlSessionFactoryBeanName("module2SqlSessionFactory");
return scannerConfigurer;
}
}
```
这样就可以实现多数据源和多模块的mybatis配置,并且指定子模块的mapper文件使用固定的第2个数据源了。
阅读全文