? ? ?一個(gè)項(xiàng)目大部分都是單一數(shù)據(jù)庫(kù)多一些,但是有時(shí)候會(huì)需要用多個(gè)庫(kù),所以這時(shí)候據(jù)需要使用多數(shù)據(jù)源。我這里使用springboot+mybatis(plus)+druid多數(shù)據(jù)源.
目前我知道有兩種方式,一種方式是需要在service實(shí)現(xiàn)類(lèi)上添加@DS,一種方式是通過(guò)配置的方式,配置不同的SqlSessionFactory實(shí)現(xiàn)。
1. 第一種方式
? 這種方式主要通過(guò)配置不同的SqlSessionFactory實(shí)現(xiàn)。
1.1 引入依賴
<dependencies>
<!-- 第一步:選擇ORM框架,使用springboot整合mybatis-plus依賴包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 第二步:選擇數(shù)據(jù)庫(kù)驅(qū)動(dòng),這里是Mysql所以就選擇Mysql的驅(qū)動(dòng),PG的就選擇PG-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- 第三步(可選):數(shù)據(jù)庫(kù)連接池,可以使用druid的連接池。springboot-jdbc已經(jīng)默認(rèn)依賴了Hikari的連接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
</dependencies>
?1.2 增加配置
#druid數(shù)據(jù)庫(kù)鏈接池配置
spring:
datasource:
#1.JDBC
type: com.alibaba.druid.pool.DruidDataSource
druid:
myboot:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myboot?useUnicode=true&characterEncoding=utf8
username: root
password: root
mydatabase:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf8
username: root
password: root
1.3 配置myboot的源連接
@Configuration
@MapperScan(basePackages = "com.lx.mybatisplus.myboot.mappers", sqlSessionTemplateRef = "mybootSqlSessionTemplate")
public class MyBootDataSourceConfig {
@Bean(name = "myboot")
@ConfigurationProperties(prefix = "spring.datasource.druid.myboot")
public DruidDataSource myboot() {
DruidDataSource myboot = new DruidDataSource();
return myboot;
}
@Primary
@Bean("mybootSqlSessionFactory")
public SqlSessionFactory psiSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(myboot());
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
factoryBean.setConfiguration(configuration);
//指定xml路徑.
// factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/a/b/c/d/e/psi/mapper/*Dao.xml"));
//這里可以設(shè)置MetaObjectHandler
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new MetaObjectHandler() {
@Override
public void insertFill(MetaObject metaObject) {
}
@Override
public void updateFill(MetaObject metaObject) {
}
});
factoryBean.setGlobalConfig(globalConfig);
return factoryBean.getObject();
}
@Bean("mybootTransactionManager")
@Primary
// 數(shù)據(jù)源事務(wù)管理器
public DataSourceTransactionManager mybootTransactionManager(@Qualifier("myboot") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean("mybootSqlSessionTemplate")
public SqlSessionTemplate psiSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(psiSqlSessionFactory()); // 使用上面配置的Factory
return template;
}
}
?1.4 配置mydatabase的源連接
@Configuration
@MapperScan(basePackages = "com.lx.mybatisplus.mydatabase.mappers", sqlSessionTemplateRef = "mydatabaseSqlSessionTemplate")
public class MyDataBaseDataSourceConfig {
@Primary
@Bean(name = "mydatabase")
@ConfigurationProperties(prefix = "spring.datasource.druid.mydatabase")
public DruidDataSource mydatabase() {
return new DruidDataSource();
}
@Bean("mydatabaseSqlSessionFactory")
public SqlSessionFactory mydatabaseSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(mydatabase());
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
factoryBean.setConfiguration(configuration);
return factoryBean.getObject();
}
@Bean("mydatabaseTransactionManager")
// 數(shù)據(jù)源事務(wù)管理器
public DataSourceTransactionManager mydatabaseTransactionManager(@Qualifier("mydatabase") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("mydatabaseSqlSessionTemplate")
public SqlSessionTemplate b2bSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(mydatabaseSqlSessionFactory()); // 使用上面配置的Factory
return template;
}
}
因?yàn)槭莾蓚€(gè)源,所以使用了兩個(gè)事務(wù)管理。需要使用 @Transcational() 注解來(lái)使用事務(wù)管理器。
但是我們實(shí)際上注冊(cè)了兩個(gè), 如果不指定事務(wù)管理器的, springboot是不知道該使用那個(gè)的。
不過(guò)我們?cè)谧?cè)的時(shí)候,在 @Bean(name = "mybootTransactionManager"), 這一行下面使用了 @Primary 注解。所以我們的spring會(huì)默認(rèn)使用 mybootTransactionManager這個(gè)事務(wù)管理器。
如果當(dāng)你需要用到 另一個(gè)事務(wù)管理器的時(shí)候,你需要在你的 @Transcational 注解中指定事務(wù)管理器名稱
ps: 使用事務(wù)的方法或者類(lèi)上指定事務(wù)管理器。
@Transactional(readOnly = true, transactionManager = "mybootTransactionManager")
@Transactional(readOnly = true, transactionManager = "mydatabaseTransactionManager")
這樣就配置完成了,因?yàn)橹付薭asePackages 對(duì)應(yīng)的sqlSessionTemplateRef,所以就可以區(qū)分不同的源配置了
2.第二種方式
?這種方式主要使用@DS注解,在使用的地方指定就行
2.1 依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
?2.2 配置文件
#druid數(shù)據(jù)庫(kù)鏈接池配置
spring:
datasource:
#1.JDBC
type: com.alibaba.druid.pool.DruidDataSource
druid:
myboot:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myboot?useUnicode=true&characterEncoding=utf8
username: root
password: root
mydatabase:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf8
username: root
password: root
2.3?在service實(shí)現(xiàn)類(lèi)上添加@DS
@Service
@DS("myboot")
public class ProductServiceImpl extends ServiceImpl<ProductMapper,Product> implements ProductService {
}
@Service
@DS("mydatabase")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
這樣就完成了配置不同源,但是這種方式局限性太大,所以建議還是使用第一種方式。
參考:
Springboot+mybatis(plus)+druid多數(shù)據(jù)源 - 知乎文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-457128.html
Java學(xué)習(xí) --- mybatisplus配置多數(shù)據(jù)源_鴨鴨老板的博客-CSDN博客_mybatisplus多數(shù)據(jù)源配置文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-457128.html
到了這里,關(guān)于mybatis(plus)多數(shù)據(jù)源的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!