序言
这里博主准备了两种方式,自己跟据自己需求使用就好,第一种注解比较简单,但自我感觉相较单一,如果使用多数据源操作不是很多可以使用注解;第二种配置相对多一些,但是更加方便。这两种配置都需要依赖yml中的数据源配置,话不多说上代码。
yml相关配置
spring:
datasource:
dataSrc:
primary: master #设置默认数据源为 master
strict: false #设置严格模式,默认为false不启动
datasource:
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.0.xxx:3306/xxx?useUnicode=true&characterEncoding=utf-8&serverTimezon=GMT%2b8&rewriteBatchedStatements=true&tinyInt1isBit=false&zeroDateTimeBehavior=convertToNull
username: root
password: root
config:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.0.xxx:3306/xxxx?useUnicode=true&characterEncoding=utf-8&serverTimezon=GMT%2b8&rewriteBatchedStatements=true&tinyInt1isBit=false&zeroDateTimeBehavior=convertToNull
username: root
password: root
druid:
# 初始连接数
initialSize: 6
# 最小连接池数量
minIdle: 12
# 最大连接池数量
maxActive: 24
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 400000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 600000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /xxx/xxx
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
Config配置文件
第一种配置@DataSource
直接使用注解@DataSource("config"),这样使用该注解的Service实现类中都是连接该库的,这种会出现一个问题,就是在使用该该注解的Service中不能够查询其他库数据,自己根据自己情况使用就可以。@DataSource("config")也可以在Mapper上使用,但是 建议在Service上使用。
@Slf4j
@Service
@DataSource("config")
public class ConfigServiceIml implements IConfigService {}
第二种配置
第一步:创建DataSourceConfig配置文件
配置对应数据源的DataSource 对象
@Configuration
public class DataSourceConfig {
/**
* master数据库配置
*/
@Primary
@Bean("master")
@ConfigurationProperties(prefix = "spring.datasource.dataSrc.datasource.master")
public DataSource masterDataSource(){
return DruidDataSourceBuilder.create().build();
}
/**
* 配置数据库配置
*/
@Bean("config")
@ConfigurationProperties(prefix = "spring.datasource.dataSrc.datasource.config")
public DataSource configDataSource(){
return DruidDataSourceBuilder.create().build();
}
}
第二步:创建与数据源对应的配置文件指定Mapper
这里主要配置各数据源对应的mapper文件,这样就可以使用不同mapper文件对不同数据库进行操作。
@Configuration
@MapperScan(basePackages = { "mapper文件所在位置"}, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisMasterConfig {
@Autowired
@Qualifier("master")
private DataSource master;
@Bean
public SqlSessionFactory masterSqlSessionFactory() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(master);
//这一部分 可以使用配置 也可以省略 ----
//可以使用MybatisConfiguration 做一些所需的配置
MybatisConfiguration configuration = new MybatisConfiguration();
//驼峰命名
configuration.setMapUnderscoreToCamelCase(false);
//开启缓存
configuration.setCacheEnabled(true);
factory.setConfiguration(configuration);
//这一部分 可以使用配置 也可以省略 ----
return factory.getObject();
}
@Bean
public SqlSessionTemplate masterSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(masterSqlSessionFactory()); // 使用上面配置的Factory
return template;
}
}
@Configuration
@MapperScan(basePackages = { "mapper文件所在位置"}, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisConConfig {
@Autowired
@Qualifier("config")
private DataSource config;
@Bean
public SqlSessionFactory configSqlSessionFactory() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(config);
//这一部分 可以使用配置 也可以省略 ----
//可以使用MybatisConfiguration 做一些所需的配置
MybatisConfiguration configuration = new MybatisConfiguration();
//驼峰命名
configuration.setMapUnderscoreToCamelCase(false);
//开启缓存
configuration.setCacheEnabled(true);
factory.setConfiguration(configuration);
//这一部分 可以使用配置 也可以省略 ----
return factory.getObject();
}
@Bean
public SqlSessionTemplate configSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(configSqlSessionFactory()); // 使用上面配置的Factory
return template;
}
}
结语
该文章只是记录自己的一些总结,以备不时之需,发出的文章希望对各位有所帮助,如果文中有不正确的请指出,一起学习,结束了,谢谢观看!