springboot 多数据源手动指定 同一个方法可使用多个数据源


        org.springframework.boot
        spring-boot-starter-parent
        2.3.9.RELEASE
        
    

    
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.0
        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            compile
        

        
            org.springframework.boot
            spring-boot-starter-test
        

        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            1.1.5
        
        
        
            com.alibaba
            druid
            1.0.28
        
    
public class DataSourceUtil {

    public static final String USER = "user";
    public static final String ORDER = "order";
    /**
     *  数据源属于一个公共的资源
     *  采用ThreadLocal可以保证在多线程情况下线程隔离
     */
    private static final ThreadLocal contextHolder = new ThreadLocal<>();

    /**
     * 设置数据源名
     * @param dbType
     */
    public static void setDB(String dbType) {
        contextHolder.set(dbType);
    }

    /**
     * 获取数据源名
     * @return
     */
    public static String getDB() {
        return (contextHolder.get());
    }

    /**
     * 清除数据源名
     */
    public static void clearDB() {
        contextHolder.remove();
    }
}

 

public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceUtil.getDB();
    }
}
@Configuration
public class DataSourceConfig {

    /**
     * 数据源1
     * spring.datasource.db1:application.properteis中对应属性的前缀
     * @return
     */
    @Bean(name = "user")
    @ConfigurationProperties(prefix = "spring.datasource.user")
    public DataSource dataSourceOne() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 数据源2
     * spring.datasource.db2:application.properteis中对应属性的前缀
     * @return
     */
    @Bean(name = "order")
    @ConfigurationProperties(prefix = "spring.datasource.order")
    public DataSource dataSourceTwo() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 动态数据源: 通过AOP在不同数据源之间动态切换
     * @return
     */
    @Primary
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(dataSourceOne());
        // 配置多数据源
        Map dsMap = new HashMap<>();
        dsMap.put("user", dataSourceOne());
        dsMap.put("order", dataSourceTwo());

        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

    /**
     * 配置多数据源后IOC中存在多个数据源了,事务管理器需要重新配置,不然器不知道选择哪个数据源
     * 事务管理器此时管理的数据源将是动态数据源dynamicDataSource
     * 配置@Transactional注解
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }
}

启动类还是不能忘记去除DataSourceAutoConfiguration

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DynamicDataSourceApplication {
    public static void main(String[] args) {
        SpringApplication.run(DynamicDataSourceApplication.class,args);
    }
}

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    public Order getOrder(Long id){
        DataSourceUtil.setDB(DataSourceUtil.ORDER);
        Order order =  orderMapper.findById(id);
        DataSourceUtil.setDB(DataSourceUtil.USER);
        User user = userMapper.findById(order.getUserId());
        order.setUser(user);
        return order;
    }
}

DataSourceUtil.setDB(DataSourceUtil.ORDER); 就可以切换数据源

你可能感兴趣的:(java,spring,boot,mybatis,java)