亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

mybatis中druid多數據源怎么配置

小億
92
2024-06-12 16:24:16
欄目: 編程語言

在MyBatis中配置Druid多數據源主要需要進行以下步驟:

  1. 首先,在pom.xml文件中添加Druid和MyBatis的依賴:
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.23</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version>
</dependency>
  1. 然后在application.properties或者application.yml文件中配置Druid的數據源信息,例如:
spring.datasource.druid.datasource1.url=jdbc:mysql://localhost:3306/test1
spring.datasource.druid.datasource1.username=root
spring.datasource.druid.datasource1.password=root

spring.datasource.druid.datasource2.url=jdbc:mysql://localhost:3306/test2
spring.datasource.druid.datasource2.username=root
spring.datasource.druid.datasource2.password=root
  1. 創建DruidDataSource配置類,用于配置Druid數據源:
@Configuration
public class DruidDataSourceConfig {

    @Bean(name = "dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource.druid.datasource1")
    public DataSource dataSource1() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "dataSource2")
    @ConfigurationProperties(prefix = "spring.datasource.druid.datasource2")
    public DataSource dataSource2() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public DynamicDataSource dataSource(@Qualifier("dataSource1") DataSource dataSource1, @Qualifier("dataSource2") DataSource dataSource2) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("dataSource1", dataSource1);
        targetDataSources.put("dataSource2", dataSource2);

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(targetDataSources);
        dynamicDataSource.setDefaultTargetDataSource(dataSource1);

        return dynamicDataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
  1. 創建DynamicDataSource類,用于動態切換數據源:
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDataSource();
    }
}
  1. 創建DataSourceContextHolder類,用于設置數據源的key:
public class DataSourceContextHolder {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    public static void setDataSource(String key) {
        contextHolder.set(key);
    }

    public static String getDataSource() {
        return contextHolder.get();
    }

    public static void clearDataSource() {
        contextHolder.remove();
    }
}

通過以上步驟,就可以實現在MyBatis中配置Druid多數據源。在需要切換數據源的地方,可以通過調用DataSourceContextHolder.setDataSource("dataSource2")來切換到相應的數據源。

0
瑞金市| 朔州市| 凉城县| 达日县| 黄平县| 榆树市| 南阳市| 祁连县| 呼图壁县| 岑巩县| 三穗县| 凌云县| 镇远县| 沽源县| 囊谦县| 虎林市| 吴忠市| 丹东市| 府谷县| 绥化市| 吉隆县| 苏尼特右旗| 休宁县| 犍为县| 永清县| 清镇市| 花莲市| 芮城县| 密山市| 九江县| 招远市| 桐城市| 阿拉善右旗| 灵宝市| 波密县| 顺平县| 沛县| 固安县| 区。| 广宁县| 肇州县|