本篇文章为大家展示了怎么在SpringBoot中配置多数据源,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
首先是配置文件
这里采用yml配置文件,其他类型配置文件同理
我配置了两个数据源,一个名字叫ds1数据源,一个名字叫ds2数据源,如果你想配置更多的数据源,继续加就行了
spring: # 数据源配置 datasource: ds1: #数据源1 driver-class-name: com.mysql.jdbc.Driver # mysql的驱动你可以配置别的关系型数据库 url: jdbc:mysql://ip:3306/db1 #数据源地址 username: root # 用户名 password: root # 密码 ds2: # 数据源2 driver-class-name: com.mysql.jdbc.Driver # mysql的驱动你可以配置别的关系型数据库 url: jdbc:mysql://ip:3307/db2#数据源地址 username: root # 用户名 password: root # 密码
增加一个Springboot的配置类
/** * 多数据源配置 */ @Configuration public class DataSourceConfig { //主数据源配置 ds1数据源 @Primary @Bean(name = "ds1DataSourceProperties") @ConfigurationProperties(prefix = "spring.datasource.ds1") public DataSourceProperties ds1DataSourceProperties() { return new DataSourceProperties(); } //主数据源 ds1数据源 @Primary @Bean(name = "ds1DataSource") public DataSource ds1DataSource(@Qualifier("ds1DataSourceProperties") DataSourceProperties dataSourceProperties) { return dataSourceProperties.initializeDataSourceBuilder().build(); } //第二个ds2数据源配置 @Bean(name = "ds2DataSourceProperties") @ConfigurationProperties(prefix = "spring.datasource.ds2") public DataSourceProperties ds2DataSourceProperties() { return new DataSourceProperties(); } //第二个ds2数据源 @Bean("ds2DataSource") public DataSource ds2DataSource(@Qualifier("ds2DataSourceProperties") DataSourceProperties dataSourceProperties) { return dataSourceProperties.initializeDataSourceBuilder().build(); } }
增加一个Springboot配置类
/** * JdbcTemplate多数据源配置 * 依赖于数据源配置 * * @see DataSourceConfig */ @Configuration public class JdbcTemplateDataSourceConfig { //JdbcTemplate主数据源ds1数据源 @Primary @Bean(name = "ds1JdbcTemplate") public JdbcTemplate ds1JdbcTemplate(@Qualifier("ds1DataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } //JdbcTemplate第二个ds2数据源 @Bean(name = "ds2JdbcTemplate") public JdbcTemplate ds2JdbcTemplate(@Qualifier("ds2DataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
增加一个SpringBoot配置类
mybatis多数据源的原理是根据不同包,调用不同的数据源,你只需要把你的mapper.java和mapper.xml(我喜欢叫dao.java和dao.xml)写在某个package中,springboot自动帮你实现数据源切换
核心代码就这句
@MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef = "ds2SqlSessionTemplate")
用来指定包扫描指定sqlSessionTemplateRef
和sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/web/ds2/**/*.xml"));
用来指定mapper.xml的路径
详细配置代码如下
/** * Mybatis主数据源ds1配置 * 多数据源配置依赖数据源配置 * @see DataSourceConfig */ @Configuration @MapperScan(basePackages ="com.web.ds1.**.dao", sqlSessionTemplateRef = "ds1SqlSessionTemplate") public class MybatisPlusConfig4ds1 { //主数据源 ds1数据源 @Primary @Bean("ds1SqlSessionFactory") public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver(). getResources("classpath*:com/web/ds1/**/*.xml")); return sqlSessionFactory.getObject(); } @Primary @Bean(name = "ds1TransactionManager") public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Primary @Bean(name = "ds1SqlSessionTemplate") public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
/** * Mybatis 第二个ds2数据源配置 * 多数据源配置依赖数据源配置 * @see DataSourceConfig */ @Configuration @MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef = "ds2SqlSessionTemplate") public class MybatisPlusConfig4ds2 { //ds2数据源 @Bean("ds2SqlSessionFactory") public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver(). getResources("classpath*:com/web/ds2/**/*.xml")); return sqlSessionFactory.getObject(); } //事务支持 @Bean(name = "ds2TransactionManager") public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "ds2SqlSessionTemplate") public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
mybatis-plus是mybatis的增强版,只增加,不影响。也就是说使用mybatis-plus兼容原来所有的mybatis代码和配置。然后又做了很多增加和简化使用,具体看官网教程https://mybatis.plus/
相对于mybatis的多数据源配置就是改了下 SqlSessionFactory
核心代码就是修改mybatis为mybatis-plus,如下
@Bean("ds2SqlSessionFactory") public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); configuration.setJdbcTypeForNull(JdbcType.NULL); sqlSessionFactory.setConfiguration(configuration); sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver(). getResources("classpath*:com/web/ds2/**/*.xml")); sqlSessionFactory.setPlugins(new Interceptor[]{ new PaginationInterceptor(), new PerformanceInterceptor() // .setFormat(true), }); sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false)); return sqlSessionFactory.getObject(); }
全部配置代码如下
/** * Mybatis-plus ds2数据源配置 * 多数据源配置依赖数据源配置 * @see DataSourceConfig */ @Configuration @MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef = "ds2SqlSessionTemplate") public class MybatisPlusConfig4ds2{ //ds2数据源 @Bean("ds2SqlSessionFactory") public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); configuration.setJdbcTypeForNull(JdbcType.NULL); sqlSessionFactory.setConfiguration(configuration); sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver(). getResources("classpath*:com/web/ds2/**/*.xml")); sqlSessionFactory.setPlugins(new Interceptor[]{ new PaginationInterceptor(), new PerformanceInterceptor() // .setFormat(true), }); sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false)); return sqlSessionFactory.getObject(); } @Bean(name = "ds2TransactionManager") public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "ds2SqlSessionTemplate") public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
/** * Mybatis-plus 主数据源ds1配置 * 多数据源配置依赖数据源配置 * @see DataSourceConfig */ @Configuration @MapperScan(basePackages ="com.web.ds1.**.dao", sqlSessionTemplateRef = "ds1SqlSessionTemplate") public class MybatisPlusConfig4ds1 { //主数据源 ds1数据源 @Primary @Bean("ds1SqlSessionFactory") public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); configuration.setJdbcTypeForNull(JdbcType.NULL); sqlSessionFactory.setConfiguration(configuration); sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver(). getResources("classpath*:com/ds1/web/ds1/**/*.xml")); sqlSessionFactory.setPlugins(new Interceptor[]{ new PaginationInterceptor(), new PerformanceInterceptor() // .setFormat(true), }); sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false)); return sqlSessionFactory.getObject(); } @Primary @Bean(name = "ds1TransactionManager") public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Primary @Bean(name = "ds1SqlSessionTemplate") public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
上述内容就是怎么在SpringBoot中配置多数据源,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。