使用gradle和springboot进行多模块开发,如果每个模块都有各自的mybatis的mapper,但是springboot容器内有两个数据源,且多模块中只有1个主模块有main方法,那么怎么指定从模块的mapper映射对应的数据源呢?
时间: 2024-03-12 14:46:35 浏览: 83
在多数据源的情况下,需要通过配置多个SqlSessionFactory和DataSource来实现,然后通过在每个Mapper上使用@Qualifier注解指定要使用的数据源。
假设我们有两个数据源,分别为主数据源和从数据源,每个模块都有对应的Mapper映射,我们可以在每个模块的build.gradle文件中添加对应的依赖:
```groovy
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'com.alibaba:druid-spring-boot-starter:1.2.6'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
//其他依赖
}
```
然后在每个模块的application.yml配置文件中,需要配置对应的数据源和SqlSessionFactory:
```yml
#主数据源配置
spring:
datasource:
master:
url: jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entity
master:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entity
#从数据源配置
slave:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/slave?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entity
```
在主模块的启动类中,需要配置多个SqlSessionFactory和DataSource,并使用@Qualifier注解指定要使用的数据源:
```java
@SpringBootApplication
@EnableAutoConfiguration
public class MainApp {
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "master.datasource")
public DataSource masterDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix = "slave.datasource")
public DataSource slaveDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(masterDataSource);
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml");
bean.setMapperLocations(resources);
return bean.getObject();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(slaveDataSource);
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml");
bean.setMapperLocations(resources);
return bean.getObject();
}
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory masterSqlSessionFactory) {
return new SqlSessionTemplate(masterSqlSessionFactory);
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory slaveSqlSessionFactory) {
return new SqlSessionTemplate(slaveSqlSessionFactory);
}
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
```
在从模块的Mapper接口中,可以在@Mapper注解上添加@Qualifier注解指定要使用的数据源:
```java
@Mapper
@Qualifier("slaveSqlSessionFactory")
public interface SlaveMapper {
//从数据源查询用户
User selectUserById(Integer id);
}
```
这样,每个Mapper就可以指定要使用的数据源,实现了多数据源的配置。
阅读全文