您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在spring boot中使用mybatis實現動態切換數據源,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.加個依賴
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency>
2.application.properties配置文件
#主從數據庫 master.db.driverClassName=com.mysql.jdbc.Driver master.db.url=jdbc:mysql://localhost:3306/cbd?characterEncoding=UTF-8&useUnicode=true&useSSL=false master.db.username=root master.db.password=admin slave.db.driverClassName=com.mysql.jdbc.Driver slave.db.url=jdbc:mysql://localhost:3306/cbd_test?characterEncoding=UTF-8&useUnicode=true&useSSL=false slave.db.username=root slave.db.password=admin mybatis.config-location= classpath:config/mybatis-config.xml mybatis.mapper-locations=classpath:mapper/**/*.xml
3.禁用springboot默認加載數據源配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
4.數據源配置類
/** * 主數據源 */ @Configuration @ConfigurationProperties(prefix = "master.db") public class MasterDataSourceConfig { private String url; private String username; private String password; private String driverClassName; }
/** * 從數據源配置 */ @Configuration @ConfigurationProperties(prefix = "slave.db") public class SlaveDataSourceConfig { private String url; private String username; private String password; private String driverClassName; }
/** * 數據源配置類 */ @Configuration public class DataSourceComponent { @Resource private MasterDataSourceConfig masterDataSourceConfig; @Resource private SlaveDataSourceConfig slaveDataSourceConfig; @Bean(name = "master") public DataSource masterDataSource() { DataSource dataSource = new DataSource(); dataSource.setUrl(masterDataSourceConfig.getUrl()); dataSource.setUsername(masterDataSourceConfig.getUsername()); dataSource.setPassword(masterDataSourceConfig.getPassword()); dataSource.setDriverClassName(masterDataSourceConfig.getDriverClassName()); return dataSource; } @Bean(name = "slave") public DataSource slaveDataSource() { DataSource dataSource = new DataSource(); dataSource.setUrl(slaveDataSourceConfig.getUrl()); dataSource.setUsername(slaveDataSourceConfig.getUsername()); dataSource.setPassword(slaveDataSourceConfig.getPassword()); dataSource.setDriverClassName(slaveDataSourceConfig.getDriverClassName()); return dataSource; } @Primary//不加這個會報錯。 @Bean(name = "multiDataSource") public MultiRouteDataSource exampleRouteDataSource() { MultiRouteDataSource multiDataSource = new MultiRouteDataSource(); Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put("master", masterDataSource()); targetDataSources.put("slave", slaveDataSource()); multiDataSource.setTargetDataSources(targetDataSources); multiDataSource.setDefaultTargetDataSource(masterDataSource()); return multiDataSource; } }
5.數據源上下文
/** * 數據源上下文 */ public class DataSourceContext { private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); public static void setDataSource(String value) { contextHolder.set(value); } public static String getDataSource() { return contextHolder.get(); } public static void clearDataSource() { contextHolder.remove(); } }
6.DataSource路由類
/* * 重寫的函數決定了最后選擇的DataSource */ public class MultiRouteDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { //通過綁定線程的數據源上下文實現多數據源的動態切換,有興趣的可以去查閱資料或源碼 return DataSourceContext.getDataSource(); } }
7.使用,修改上下文中的數據源就可以切換自己想要使用的數據源了。
public UserVO findUser(String username) { DataSourceContext.setDataSource("slave"); UserVO userVO = userMapper.findByVO(username); System.out.println(userVO.getName()+"====================="); return null; }
上述就是小編為大家分享的怎么在spring boot中使用mybatis實現動態切換數據源了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。