您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關springboot 中怎么利用mybatis實現多數據源,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
項目有一個需求,需要從多數據源讀取數據。因此使用springboot+mybatis進行多數據源配置和操作。
<!-- mybatis db --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency>
# ds1數據源配置 spring.datasource.ds1.url=jdbc:mysql://ip1:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.ds1.username=xxxxxx spring.datasource.ds1.password=xxxxxx spring.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver # ds2數據源配置 spring.datasource.ds2.url=jdbc:mysql://ip2:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.ds2.username=xxxxxx spring.datasource.ds2.password=xxxxxx spring.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver # ds3數據源配置 spring.datasource.ds3.url=jdbc:mysql://ip3:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.ds3.username=xxxxxx spring.datasource.ds3.password=xxxxxx spring.datasource.ds3.driver-class-name=com.mysql.cj.jdbc.Driver
package com.qishu.demo.config; 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 javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.qishu.demo.ds1.dao", sqlSessionFactoryRef = "ds1SqlSessionFactory") public class Ds1DatasourceConfig { @Primary @Bean(name = "ds1DataSource") @ConfigurationProperties("spring.datasource.ds1") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = "ds1SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:ds1-mapper/*.xml")); return sessionFactoryBean.getObject(); } }
package com.qishu.demo.config; 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 javax.sql.DataSource; @Configuration // 掃描的包路徑 @MapperScan(basePackages = "com.qishu.demo.ds2.dao", sqlSessionFactoryRef = "ds2SqlSessionFactory") public class Ds2DatasourceConfig { @Bean(name = "ds2DataSource") // 配置前綴 @ConfigurationProperties("spring.datasource.ds2") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "ds2SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); // mapper文件目錄 sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:ds2-mapper/*.xml")); return sessionFactoryBean.getObject(); } }
ds3就不寫了
使用方式
@Autowired private AppDAO appDAO;
// 排除自動注冊配置 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
// EnableAutoConfiguration注解,關閉springBoot關于mybatis的一些自動注入。 @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class}) public class AppDAOTest { @Autowired private AppDAO appDAO; @Test public void selectTest() { AppDO appDO = appDAO.selectByPrimaryKey(70); System.out.println(JSON.toJSONString(appDO)); } }
以上就是springboot 中怎么利用mybatis實現多數據源,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。