您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。