这篇文章主要介绍了SpringBoot多数据源集成的方法的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot多数据源集成的方法文章都会有所收获,下面我们一起来看看吧。
一、多数据源使用场景与弊端
1.场景
业务系统跨数据库
数据转存(这个现在太low了,应该高级点都不用)
系统集成
2.弊端
跨库业务事务问题
service、dao不能重复注入数据源
二、使用步骤
1.引入库
<!-- 多数据源支持 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.3.6</version> </dependency>
2.多数据源配置文件
#数据源配置
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
3.多数据源配置类
数据源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 @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); } @Bean(name = "source1SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("source1DataSource") DataSource dataSource) throws Exception { //不使用下面这个无法使用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 @Primary @Bean(name = "targetDataSource") @ConfigurationProperties(prefix = "spring.datasource.target") public DataSource dataSource() { DataSource build = DataSourceBuilder.create().build(); return build; } @Bean(name = "masterTransactionManager") @Primary public PlatformTransactionManager dataSourceTransactionManager( @Qualifier("targetDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "targetSqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("targetDataSource") DataSource dataSource) throws Exception { //不使用下面这个无法使用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 方法,看注释
4.使用
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;@DS("source1")@Slf4j@Service("fkRegisterVisitorService")public class FkRegisterVisitorServiceImpl extends ServiceImpl <FkRegisterVisitorMapper, FkRegisterVisitor> implements FkRegisterVisitorService { }
注意是在service的实现类上用@DS(“数据源名称”)注解,数据源名称与配置文件里的datasource下的一致。
注意dao不能再用@DS注解,否在报错
关于“SpringBoot多数据源集成的方法”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“SpringBoot多数据源集成的方法”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网行业资讯频道。