pom.xml
其他依赖省略了,这里只举出数据源相关的。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
application.yml
注意这里得用jdbc-url来代替url,不然会报错。
spring:
datasource:
business:
jdbc-url: jdbc:mysql://10.162.254.112:3306/db_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
#初始化大小
initialSize: 15
#最小值
minIdle: 15
#最大值
maxActive: 35
#最大等待时间,配置获取连接等待超时,时间单位都是毫秒ms
maxWait: 60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接
timeBetweenEvictionRunsMillis: 60000
#配置一个连接在池中最小生存的时间
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
device:
jdbc-url: jdbc:mysql://10.162.254.111:3306/db_2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
#初始化大小
initialSize: 15
#最小值
minIdle: 15
#最大值
maxActive: 35
#最大等待时间,配置获取连接等待超时,时间单位都是毫秒ms
maxWait: 60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接
timeBetweenEvictionRunsMillis: 60000
#配置一个连接在池中最小生存的时间
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
数据源配置
项目下新建包,参考如下:
**DataSourceConfig用于配置数据源,**Source定义数据源注解。
@Configuration
// 扫描该数据源接口
@MapperScan(basePackages = "fpi.gyiot.communicat.business.dao")
public class BusinessDataSourceConfig {
// 配置数据源
@Bean(name = "businessDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.business")
public DataSource getBusinessDataSource() {
return DataSourceBuilder.create().build();
}
// SqlSessionFactory
@Bean(name = "businessSessionFactory")
@Primary
public SqlSessionFactory businessSqlSessionFactory(@Qualifier("businessDataSource")
DataSource businessDataSource) throws Exception {
SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
fb.setDataSource(businessDataSource);
fb.setTypeAliasesPackage("fpi.gyiot.communicat.model");
fb.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mapper/business/*.xml"));
return fb.getObject();
}
// 事务
@Bean(name = "businessTransactionManager")
@Primary
public DataSourceTransactionManager businessTransactionManager(@Qualifier("businessDataSource")
DataSource businessDataSource) throws Exception {
return new DataSourceTransactionManager(businessDataSource);
}
@Bean(name = "businessMapperScannerConfigurer")
public MapperScannerConfigurer businessMapperScannerConfigurer() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setSqlSessionFactoryBeanName("businessSessionFactory");
// 接口包
configurer.setBasePackage("fpi.gyiot.communicat.business.dao");
// 使用的注解
configurer.setAnnotationClass(BusinessSource.class);
return configurer;
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface BusinessSource {
String value() default "";
}
另一个数据源是一样的,注意改名字。
根据上面的配置,创建好对应的包目录:
- model;
- business.dao
- device.dao
以及在resources目录下创建mybatis映射文件Mapper目录 - mapper/business
- mapper/device
指定数据源
造dao接口中使用上一步创建的注解即可。
@Repository
@BusinessSource
public interface BusinessDao {
}