spring:
datasource:
db1:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://127.0.0.1:3306/hello_spring_boot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
db2:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://127.0.0.1:3306/hello_spring_boot_2?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
@Configuration
public class DataSourceConfig {
@Primary
@Bean("db1")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSourceProperties db1() {
return new DataSourceProperties();
}
@Primary
@Bean
public DataSource dataSource1(@Qualifier("db1") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
@Bean("db2")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSourceProperties db2() {
return new DataSourceProperties();
}
@Bean
public DataSource dataSource2(@Qualifier("db2") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
}
@Configuration
@MapperScan(
basePackages = "com.tcoding.demo.mybatis.mbg.mapper1",
annotationClass = Db1.class, sqlSessionTemplateRef =
"sqlSessionTemplate1")
public class MybatisConfigDb1 {
@Primary
@Bean
public SqlSessionFactory db1SqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
sqlSessionFactoryBean.setTypeHandlers(new EncryptTypeHandler());
return sqlSessionFactoryBean.getObject();
}
@Primary
@Bean
public DataSourceTransactionManager transactionManager1(@Qualifier("dataSource1") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean("sqlSessionTemplate1")
public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("db1SqlSessionFactory1") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(
basePackages = "com.tcoding.demo.mybatis.mbg.mapper2",
annotationClass = Db2.class,
sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MybatisConfigDb2 {
@Bean
public SqlSessionFactory db1SqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
// XML 配置时开启
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/db2/*.xml"));
sqlSessionFactoryBean.setTypeHandlers(new EncryptTypeHandler());
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
// 开启驼峰命名
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setConfiguration(configuration);
return sqlSessionFactoryBean.getObject();
}
@Bean
public DataSourceTransactionManager dataSourceTransactionManager2(@Qualifier("dataSource2") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("sqlSessionTemplate2")
public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("db1SqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
写demo的时候basePackages和annotationClass两个同时使用了,死活不好使,项目起不来
原因如下 annotationClass 需要需要如下的定义
需要@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)不然不生效
@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface Db1 {
}
坑的是basePackages和annotationClass取的是交集,所以annotationClass能不用就不用吧
xml配置是需要在
SqlSessionFactory是指定 mapper.xml的路径
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
注解的方式为了方便可以开启驼峰命名
https://github.com/googalAmbition/hello-spring-boot/tree/main/03-mybatis