springboot-mybatis配置多数据源

第一步,编写数据源配置类

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceConfig {

    /**
     * 连接池属性
     */
    private String type;
    private String driverClassName;
    private String initialSize;
    private String minIdle;
    private String maxActive;
    private String maxWait;
    private String timeBetweenEvictionRunsMillis;
    private String minEvictableIdleTimeMillis;
    private String validationQuery;
    private String testWhileIdle;
    private String testOnBorrow;
    private String testOnReturn;
    private String filters;

    /**
     * 数据源
     */
    private DataSourceLink configcenter;
    private DataSourceLink basic;

    @Getter
    @Setter
    static class DataSourceLink {
        private String url;
        private String username;
        private String password;
    }

    /**
     * 配置[base]对应数据源
     */
    @Bean(name = "configCenterDataSource")
    @Primary
    public DataSource configCenterDataSource() {
        return buildDataSource(configcenter, "configCenterDataSource");
    }

    /**
     * 配置[base]对应数据源
     */
    @Bean(name = "baseDataSource")
    public DataSource baseDataSource() {
        return buildDataSource(basic, "baseDataSource");
    }

    /**
     * 创建数据源
     *
     * @param dataSourceLink 数据源连接
     * @param dataSourceName 数据源标识
     * @return 数据源
     */
    private AtomikosDataSourceBean buildDataSource(DataSourceLink dataSourceLink, String dataSourceName) {
        Properties prop = new Properties();
        prop.put("url", dataSourceLink.getUrl());
        prop.put("username", dataSourceLink.getUsername());
        prop.put("password", dataSourceLink.getPassword());
        prop.put("driverClassName", driverClassName);
        prop.put("filters", filters);
        prop.put("maxActive", maxActive);
        prop.put("initialSize", initialSize);
        prop.put("maxWait", maxWait);
        prop.put("minIdle", minIdle);
        prop.put("timeBetweenEvictionRunsMillis", timeBetweenEvictionRunsMillis);
        prop.put("minEvictableIdleTimeMillis", minEvictableIdleTimeMillis);
        prop.put("validationQuery", validationQuery);
        prop.put("testWhileIdle", testWhileIdle);
        prop.put("testOnBorrow", testOnBorrow);
        prop.put("testOnReturn", testOnReturn);

        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setXaDataSourceClassName(type);
        ds.setPoolSize(5);
        ds.setXaProperties(prop);
        ds.setUniqueResourceName(dataSourceName);
        ds.setTestQuery("select 1");
        return ds;
    }

}

配置第一个数据源

@Slf4j
@Configuration
@MapperScan(basePackages = {"com.vanew.trade.erp.batch.web.dao.base"}, sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class BaseSqlSessionTemplateConfig {

//初始化数据源,从配置文件中获取数据源的参数
    @Value("${vanew.mybatis.base-locations}")
    private String mapperLocations;

    /**
     * 自定义sqlSessionFactory配置(因为没有用到MybatisAutoConfiguration自动配置类,需要手动配置)
     *
     * @param dataSource 数据源
     * @return SqlSessionFactory
     * @throws Exception 异常信息
     */
//获取mybatis的SqlSessionFactory 
    @Bean
    public SqlSessionFactory baseSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //如果重写了 SqlSessionFactory 需要在初始化的时候手动将 mapper 地址 set到 factory 中,否则会报错:
        //org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
      
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        bean.setVfs(SpringBootVFS.class);
        // bean.setPlugins(new MybatisInterceptor());
        bean.setConfigurationProperties(MyBatisProperties.getProperties());
        return bean.getObject();
    }

    /**
     * SqlSessionTemplate 是 SqlSession接口的实现类,是spring-mybatis中的,实现了SqlSession线程安全
     *
     * @param sqlSessionFactory sqlSessionFactory
     * @return SqlSessionTemplate
     */
    @Bean
    public SqlSessionTemplate baseSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
  **设置mapper文件存放位置,通过存放位置来区分数据源**
posted @ 2021-01-20 10:58  小白白白白白白白白白  阅读(136)  评论(0编辑  收藏  举报