bes2008/sqlhelper

我的多数据源配置不生效,分页信息都没传到sql里

yaozhigangyeah opened this issue · 6 comments

我的多数据源是这样配置的,不用切换
@configuration
@MapperScan(basePackages = "com.lingdong.business.mapper,com.lingdong.mapper",sqlSessionTemplateRef ="firstSqlSessionTemplate")
public class FirstDataSourceConfig {

@Autowired
private FirstDataBaseProperties prop;

/**创建数据源*/
@Bean(name = "firstDS")
@ConfigurationProperties(prefix = "spring.datasource.first")
@Primary
public DataSource getFirstDataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName(prop.driverClassName);
    dataSource.setUrl(prop.url);
    dataSource.setUsername(prop.username);
    dataSource.setPassword(prop.password);

    return dataSource;
}


/**创建SessionFactory*/
@Bean(name = "firstSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDS") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setTypeAliasesPackage("com.lingdong.bean,com.lingdong.bean.business");

    Resource[] mapperLocations1 = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml");
    Resource[] mapperLocations2 = new PathMatchingResourcePatternResolver().getResources("classpath*:business/mapper/*.xml");
    Resource[] mapperLocations = new Resource[mapperLocations1.length+mapperLocations2.length];
    int inx = 0;
    for(Resource r:mapperLocations1){
        mapperLocations[inx++] = r;
    }
    for(Resource r:mapperLocations2){
        mapperLocations[inx++] = r;
    }
    bean.setMapperLocations(mapperLocations);

    return bean.getObject();
}

那应该怎样配置多数据源,是要配置成动态切换吗

DBB71CE5-9702-4fa6-B8FF-A5FEA3B96E11

针对你这种,自定义 SqlSessionFactory的场景,可以这么做

另外,那个VendorDatabaseIdProvider 要用 sqlhelper 提供的,通过下面的代码,可以直接拿到:

com.jn.sqlhelper.mybatis.MybatisUtils.vendorDatabaseIdProvider()

this.sqlHelperMybatisProperties怎么来的

springboot application.yml,有默认值,可以不配置

可以去看看sqlhelper-examples 中的例子

另外,有专门的QQ群,你可以在群里直接问,QQ群就在首页可以找到

另外告诉你一个小技巧,很多情况下,根本不必自己去定义SQLSessionFactory的,可以看一下 ConfigurationCustomizer这个类