使用Druid和MyBatis,配置多数据源

背景

当使用Spring Boot项目并需要使用Druid和MyBatis实现多数据源时,你可以按照以下步骤进行配置和编写代码:

示例代码(请结合实际配置)

  1. 首先,确保在pom.xml文件中添加Druid、MyBatis和MyBatis-Spring Boot的依赖:(具体版本结合实际情况)

    	<dependency>
    	    <groupId>com.alibaba</groupId>
    	    <artifactId>druid-spring-boot-starter</artifactId>
    	    <version>1.2.6</version>
    	</dependency>
    	
    	<dependency>
    	    <groupId>org.mybatis.spring.boot</groupId>
    	    <artifactId>mybatis-spring-boot-starter</artifactId>
    	    <!-- 指定版本 -->
    	    <version>2.2.0</version>
    	</dependency>
    
  2. 创建两个数据源配置类,分别对应两个数据源(假设为dataSource1和dataSource2):

    	@Configuration
    	public class DataSource1Config {
    	    
    	    @Bean
    	    @ConfigurationProperties("spring.datasource.druid.datasource1")
    	    public DataSource dataSource1() {
    	        return DruidDataSourceBuilder.create().build();
    	    }
    	    
    	    @Bean
    	    public SqlSessionFactory sqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource1) throws Exception {
    	        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    	        factoryBean.setDataSource(dataSource1);
    	        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper1/*.xml"));
    	        return factoryBean.getObject();
    	    }
    	    
    	    @Bean
    	    public DataSourceTransactionManager dataSourceTransactionManager1(@Qualifier("dataSource1") DataSource dataSource1) {
    	        return new DataSourceTransactionManager(dataSource1);
    	    }
    	    
    	    @Bean
    	    public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory1) {
    	        return new SqlSessionTemplate(sqlSessionFactory1);
    	    }
    	}
    
    	@Configuration
    	public class DataSource2Config {
    	    
    	    @Bean
    	    @ConfigurationProperties("spring.datasource.druid.datasource2")
    	    public DataSource dataSource2() {
    	        return DruidDataSourceBuilder.create().build();
    	    }
    	    
    	    @Bean
    	    public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource2) throws Exception {
    	        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    	        factoryBean.setDataSource(dataSource2);
    	        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*.xml"));
    	        return factoryBean.getObject();
    	    }
    	    
    	    @Bean
    	    public DataSourceTransactionManager dataSourceTransactionManager2(@Qualifier("dataSource2") DataSource dataSource2) {
    	        return new DataSourceTransactionManager(dataSource2);
    	    }
    	    
    	    @Bean
    	    public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory2) {
    	        return new SqlSessionTemplate(sqlSessionFactory2);
    	    }
    	}
    

    在上述代码中,分别创建了两个数据源的配置类,并使用**@ConfigurationProperties注解将Druid配置属性绑定到数据源对象上。同时,配置了MyBatis的SqlSessionFactory**、DataSourceTransactionManagerSqlSessionTemplate

  3. 在application.properties或application.yml文件中配置数据源相关属性:

    • application.properties示例:

      	spring.datasource.druid.datasource1.url=jdbc:mysql://localhost:3306/db1
      	spring.datasource.druid.datasource1.username=username1
      	spring.datasource.druid.datasource1.password=password1
      	spring.datasource.druid.datasource1.driver-class-name=com.mysql.jdbc.Driver
      	
      	spring.datasource.druid.datasource2.url=jdbc:mysql://localhost:3306/db2
      	spring.datasource.druid.datasource2.username=username2
      	spring.datasource.druid.datasource2.password=password2
      	spring.datasource.druid.datasource2.driver-class-name=com.mysql.jdbc.Driver
      
    • application.yml示例:

      	spring:
      	  datasource:
      	    druid:
      	      datasource1:
      	        url: jdbc:mysql://localhost:3306/db1
      	        username: username1
      	        password: password1
      	        driver-class-name: com.mysql.jdbc.Driver
      	      datasource2:
      	        url: jdbc:mysql://localhost:3306/db2
      	        username: username2
      	        password: password2
      	        driver-class-name: com.mysql.jdbc.Driver
      

    在上述配置中,你可以根据实际情况修改URL、用户名和密码等数据源配置属性。

  4. 创建两个Mapper接口,分别对应两个数据源(假设为UserMapper1和UserMapper2):

    	public interface UserMapper1 {
    	    List<User> getAllUsers();
    	}
    	
    	public interface UserMapper2 {
    	    List<User> getAllUsers();
    	}
    

    在上述代码中,你可以根据实际情况编写mapper层。

  5. 在需要使用数据源的地方,注入相应的Mapper接口并使用:

    	@Service
    	public class UserService {
    	    
    	    private final UserMapper1 userMapper1;
    	    private final UserMapper2 userMapper2;
    	    
    	    public UserService(UserMapper1 userMapper1, UserMapper2 userMapper2) {
    	        this.userMapper1 = userMapper1;
    	        this.userMapper2 = userMapper2;
    	    }
    	    
    	    public List<User> getAllUsersFromDataSource1() {
    	        return userMapper1.getAllUsers();
    	    }
    	    
    	    public List<User> getAllUsersFromDataSource2() {
    	        return userMapper2.getAllUsers();
    	    }
    	}
    

    在上述代码中,通过构造函数注入两个Mapper接口,并使用它们调用相应的SQL查询方法。

总结

通过以上配置和代码,你可以在Spring Boot项目中使用Druid和MyBatis实现多数据源的配置和使用。每个数据源都有单独的Druid配置和MyBatis相关配置,并通过Mapper接口进行数据库操作。注意,需要根据实际情况在**@ConfigurationProperties注解中指定数据源的配置属性,并在SqlSessionFactory**的配置中指定Mapper XML文件的位置。

如果大家遇到类似问题,欢迎评论区讨论,如有错误之处,敬请留言。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘刘刘刘刘先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值