mybatis多数据源 通过扫描不同包实现

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.one")
    DataSource dsOne() {
        return DruidDataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties("spring.datasource.two")
    DataSource dsTwo() {
        return DruidDataSourceBuilder.create().build();
    }
}
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@MapperScan(value = "org.sang.mapper1", sqlSessionFactoryRef = "sqlSessionFactoryBean1")
public class MyBatisConfigOne {
    @Autowired
    @Qualifier("dsOne")
    DataSource dsOne;

    @Bean
    SqlSessionFactory sqlSessionFactoryBean1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dsOne);
        return factoryBean.getObject();
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBean1());
    }
}

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
@MapperScan(value = "org.sang.mapper2", sqlSessionFactoryRef = "sqlSessionFactoryBean2")
public class MyBatisConfigTwo {
    @Autowired
    @Qualifier("dsTwo")
    DataSource dsTwo;
    @Bean
    SqlSessionFactory sqlSessionFactoryBean2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dsTwo);
        return factoryBean.getObject();
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBean2());
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java项目中,通过MyBatis实现多数据源实例可以按照以下步骤进行: 1. 在项目中引入MyBatis和数据库驱动。 2. 在项目中定义多个数据源,对应不同的数据库。可以使用Spring框架的`DataSource`接口实现类来定义数据源。 3. 配置MyBatis的SqlSessionFactory,指定多个数据源。 4. 在Mapper接口中使用`@MapperScan`注解指定要扫描的Mapper接口路径。 5. 在Mapper.xml文件中,使用`<selectKey>`标签指定使用的数据源。 示例代码如下: ``` //定义数据源1 @Bean @ConfigurationProperties(prefix = "spring.datasource.db1") public DataSource dataSource1() { return DataSourceBuilder.create().build(); } //定义数据源2 @Bean @ConfigurationProperties(prefix = "spring.datasource.db2") public DataSource dataSource2() { return DataSourceBuilder.create().build(); } //配置MyBatis的SqlSessionFactory,指定多个数据源 @Bean public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource1, @Qualifier("dataSource2") DataSource dataSource2) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource1); //设置Mapper.xml文件路径 Resource[] mapperLocations = new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/*.xml"); sqlSessionFactoryBean.setMapperLocations(mapperLocations); //设置多个数据源 Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put("dataSource1", dataSource1); targetDataSources.put("dataSource2", dataSource2); DynamicDataSource dataSource = new DynamicDataSource(); dataSource.setTargetDataSources(targetDataSources); dataSource.setDefaultTargetDataSource(dataSource1); sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean.getObject(); } //在Mapper.xml文件中,使用<selectKey>标签指定使用的数据源 <select id="getUserInfoById" resultMap="userInfo" > <selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="dataSource"> SELECT CASE WHEN id < 1000 THEN 'dataSource1' ELSE 'dataSource2' END AS dataSource FROM user_info WHERE id = #{id} </selectKey> SELECT * FROM user_info WHERE id = #{id} </select> ``` 其中,我们使用了`DynamicDataSource`类来实现动态数据源的切换。在`DynamicDataSource`类中,我们需要重写`determineCurrentLookupKey()`方法,根据具体的业务场景来动态切换数据源。 ``` public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDataSourceType(); } } ``` 最后,我们需要在业务代码中设置数据源的类型,以实现动态切换数据源。 ``` public class UserServiceImpl implements UserService { @Override public UserInfo getUserInfoById(Integer id) { DataSourceContextHolder.setDataSourceType("dataSource1"); UserInfo userInfo = userMapper.getUserInfoById(id); return userInfo; } } ``` 以上就是通过MyBatis实现多数据源实例的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值