91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
灵台县| 武陟县| 南丰县| 芮城县| 那坡县| 五常市| 余姚市| 邹城市| 定州市| 肇源县| 镇赉县| 宜章县| 宝山区| 南康市| 彩票| 花莲市| 潮安县| 平度市| 额敏县| 藁城市| 麻江县| 青田县| 吉隆县| 永嘉县| 邻水| 甘孜| 淳安县| 烟台市| 清涧县| 大连市| 钟山县| 双流县| 夏河县| 巴彦淖尔市| 济宁市| 普宁市| 诸城市| 祁门县| 大同市| 藁城市| 加查县|