使用gradle和springboot进行多模块开发,只有主模块有Application.class的main方法,且主模块有2个DataSource数据源,每个模块都有各自的mybatis的mapper文件,怎么指定各模块的mapper文件与数据源的对应关系呢?
时间: 2024-03-12 09:46:52 浏览: 194
可以使用SpringBoot的多数据源配置,同时在每个模块的配置文件中指定对应的数据源和mapper文件位置。
首先,在主模块的配置文件中定义两个数据源,例如:
```
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=primaryDataSource
```
```
mybatis.mapper-locations=classpath:mapper/module2/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.name=secondaryDataSource
```
最后,在每个模块中定义对应的SqlSessionFactoryBean和MapperScannerConfigurer:
```java
@Configuration
public class Module1Config {
@Autowired
@Qualifier("primaryDataSource")
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("secondaryDataSource")
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配置了。
阅读全文