您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot多數據源集成的方法的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SpringBoot多數據源集成的方法文章都會有所收獲,下面我們一起來看看吧。
業務系統跨數據庫
數據轉存(這個現在太low了,應該高級點都不用)
系統集成
跨庫業務事務問題
service、dao不能重復注入數據源
<!-- 多數據源支持 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.3.6</version> </dependency>
#數據源配置
spring:
application:
name: olap
datasource:
dynamic:
primary: target
target:
jdbc-url: jdbc:mysql://192.168.1.101:15051/db1?useUnicode=true&characterEncoding=utf8&useSSL=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
source1:
jdbc-url: jdbc:mysql://192.168.102:15052/db2?useUnicode=true&characterEncoding=utf8&useSSL=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: admin
password: 456
driver-class-name: com.mysql.jdbc.Driver
數據源1
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @Configuration @MapperScan(basePackages = "com.easylinkin.dc.mapper.source1", sqlSessionFactoryRef = "source1SqlSessionFactory") public class Source1DataSourceConfig { // mapper.xml所在地址 private static final String MAPPER_LOCATION = "classpath*:mapper/business/source1/*.xml"; /** * 數據源 */ @Bean(name = "source1DataSource") @ConfigurationProperties(prefix = "spring.datasource.source1") public DataSource dataSource() { DataSource build = DataSourceBuilder.create().build(); return build; } /** * 事務管理器 */ @Bean(name = "source1TransactionManager") public PlatformTransactionManager dataSourceTransactionManager( @Qualifier("source1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * session工廠 */ @Bean(name = "source1SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("source1DataSource") DataSource dataSource) throws Exception { /* final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources( Source1DataSourceConfig.MAPPER_LOCATION));*/ //不使用下面這個無法使用mybatis-plus提供的BaseMapper中的方法,需要增加mybatis-plus 的拓展依賴 MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); //不在這里注入分頁插件,會失效 //bean.setPlugins(new PaginationInterceptor[]{new PaginationInterceptor()}); return bean.getObject(); } }
數據源2
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @Configuration @MapperScan(basePackages = "com.easylinkin.dc.mapper.target", sqlSessionFactoryRef = "targetSqlSessionFactory") public class TargetDataSourceConfig { // mapper.xml所在地址 private static final String MAPPER_LOCATION = "classpath*:mapper/business/target/*.xml"; /** * 主數據源,Primary注解必須增加,它表示該數據源為默認數據源 項目中還可能存在其他的數據源,如獲取時不指定名稱,則默認獲取這個數據源,如果不添加,則啟動時候回報錯 */ @Primary @Bean(name = "targetDataSource") @ConfigurationProperties(prefix = "spring.datasource.target") public DataSource dataSource() { DataSource build = DataSourceBuilder.create().build(); return build; } /** * 事務管理器,Primary注解作用同上 */ @Bean(name = "masterTransactionManager") @Primary public PlatformTransactionManager dataSourceTransactionManager( @Qualifier("targetDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * session工廠,Primary注解作用同上 */ @Bean(name = "targetSqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("targetDataSource") DataSource dataSource) throws Exception { /*final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources( TargetDataSourceConfig.MAPPER_LOCATION)); return sessionFactoryBean.getObject();*/ //不使用下面這個無法使用mybatis-plus提供的BaseMapper中的方法,需要增加mybatis-plus 的拓展依賴 MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); //不在這里注入分頁插件,會失效 //bean.setPlugins(new PaginationInterceptor[]{new PaginationInterceptor()}); return bean.getObject(); } }
注意dao一定要在@MapperScan注解設置的包下
注意xml文件要在MAPPER_LOCATION=的路徑下
springBoot啟動類不要再用@MapperScan注解
其他注意配置類的SqlSessionFactory 方法,看注釋
import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.easylinkin.dc.common.exceptions.BusinessException; import com.easylinkin.dc.common.utils.io.OutputStreamUtil; import com.easylinkin.dc.mapper.source1.FkRegisterVisitorMapper; import com.easylinkin.dc.business.visitor.entity.FkRegisterVisitor; import com.easylinkin.dc.business.visitor.entity.vo.FkRegisterVisitorVo; import com.easylinkin.dc.business.visitor.service.FkRegisterVisitorService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * FkRegisterVisitor表服務實現類 * * @author CodeGenerator * @date 2022/05/16 */ @DS("source1") @Slf4j @Service("fkRegisterVisitorService") public class FkRegisterVisitorServiceImpl extends ServiceImpl <FkRegisterVisitorMapper, FkRegisterVisitor> implements FkRegisterVisitorService { }
注意是在service的實現類上用@DS(“數據源名稱”)注解,數據源名稱與配置文件里的datasource下的一致。
注意dao不能再用@DS注解,否在報錯
關于“SpringBoot多數據源集成的方法”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“SpringBoot多數據源集成的方法”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。