springboot multi mybatis datasource

Add configuration

In application.properties file add data source configuration

1
2
3
4
5
6
7
8
9
10
11
spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver
spring.datasource.test1.url=jdbc:mysql://localhost:3306/test1
spring.datasource.test1.username=root
spring.datasource.test1.password=root

spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver
spring.datasource.test2.url=jdbc:mysql://localhost:3306/test2
spring.datasource.test2.username=root
spring.datasource.test2.password=root

mybatis.mapperLocations=classpath:sql-mappers/**/*.xml

Self define data source

New configuration

Point out base package path, sql session reference.

1
2
3
4
5
6
@Configuration
@MapperScan(basePackages="com.burningbright.test1",
sqlSessionFactoryRef="test1SqlSessionFactory")
public class Datasource1 {
...
}

Inject xml path properties

Put mapper xml location into configuration class

1
2
@Value("${mybatis.mapperLocations}")
private String mapperLocation;

Add properties holder

1
2
3
4
5
@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

Create datasource

1
2
3
4
5
@Bean(name="test1Datasource")
@ConfigurationProperties(prefix="spring.datasource.test1")
public DataSource testDatasource() {
return DataSourceBuilder.create().build();
}

Create session factory

Make sure bean name is same as the class reference annotation

1
2
3
4
5
6
7
8
9
10
11
@Bean(name="test1SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(
@Qualifier("test1Datasource")DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().
getResources(mapperLocation));
return bean.getObject();
}

Create transaction manager bean

1
2
3
4
5
@Bean(name="test1TransactionManager")
public DataSourceTransactionManager testTransactionManager(
@Qualifier("test1Datasource")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

Create session template

1
2
3
4
5
@Bean(name="test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}

Use data source

Add test data source test2 the same as above

1
2
3
4
5
6
7
8
@SpringBootApplication
@ComponentScan(basePackages={"com.burningbright.test1","com.burningbright.test2"})
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
...
}

  1. Prefix must the same as properties keys’ prefix in application.properties
  2. If @Primary not be annotated, application will throw exception.
    Unless the reseal framework level has a default primary datasource.
  3. @Qualifier inject object by bean’s name.
  4. basePackages is mapper file’s package path.
  5. sqlSessionTemplateRef is the instance’s reference

https://blog.csdn.net/qq_37142346/article/details/78488452
https://blog.csdn.net/a123demi/article/details/74004499
https://blog.csdn.net/hongweigg/article/details/79104321