springboot mybatis 多数据源


/**
 * @author zhangwei
 */
@Configuration
@MapperScan(basePackages = "com.xxx.dao." + WebDriudDbConfig.DbModule, sqlSessionFactoryRef = WebDriudDbConfig.DbModule + "SqlSessionFactory")
public class WebDriudDbConfig {

    protected static final String DbModule = "web";

    @Bean(name = DbModule + "DataSource")
    @Primary
    @ConfigurationProperties(prefix = DbModule + ".datasource")
    public DataSource setDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = DbModule + "SqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier(DbModule + "DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/" + DbModule + "/*.xml"));
        return bean.getObject();
    }

}

其他的数据源不用加@Primary

猜你喜欢

转载自my.oschina.net/corleone/blog/1822092