You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/10/30 08:14:21 UTC

[GitHub] [shardingsphere] haiy1026 commented on issue #21849: Question:ShardingSphereAlgorithm sharding strategy configuration is not executed

haiy1026 commented on issue #21849:
URL: https://github.com/apache/shardingsphere/issues/21849#issuecomment-1296161652

   In DataSourceConfig.class, i forget add sqlsession
   The complete code is as follows
   ```
   @Configuration
   @MapperScan(basePackages = "com.xx.datasync.mapper",sqlSessionFactoryRef ="sqlSessionFactoryForShardingJdbc")
   public class DataSourceConfig {
       @Bean("shardingDataSource")
       @Primary
       public DataSource dataSource() throws SQLException {
           Map<String,DataSource> dataSourceMap=new HashMap<>(2);
   
           DruidDataSource dataSource = new DruidDataSource();
           dataSource.setDriverClassName("com.mysql.jdbc.Driver");
           dataSource.setUrl("jdbc:mysql://localhost:23306/db1?allowPublicKeyRetrieval=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8");
           dataSource.setUsername("root");
           dataSource.setPassword("root");
           dataSourceMap.put("db1", dataSource);
           Properties prop=new Properties();
           prop.setProperty("sql-show","true");
           return ShardingSphereDataSourceFactory.createDataSource("db1",dataSourceMap, Collections.singleton(createShardingRuleConfiguration()), prop);
   
       }
   
       private ShardingRuleConfiguration createShardingRuleConfiguration() {
           ShardingRuleConfiguration result = new ShardingRuleConfiguration();
           result.getTables().add(getTableRuleConfiguration());
           result.getBindingTableGroups().add("employee");
           result.setDefaultTableShardingStrategy(new ComplexShardingStrategyConfiguration("biz_date", "hisAlgo"));
           Properties propsComplex = new Properties();
           propsComplex.setProperty("strategy", "COMPLEX");
           propsComplex.setProperty("algorithmClassName","com.xx.datasync.config.MyComplexShardingAlgo");
           result.getShardingAlgorithms().put("hisAlgo", new ShardingSphereAlgorithmConfiguration("CLASS_BASED", propsComplex));
   
           return result;
       }
   
       private ShardingTableRuleConfiguration getTableRuleConfiguration() {
           ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("employee", "db1.his_employee_$->{20221018..20221019}");
           result.setTableShardingStrategy(new ComplexShardingStrategyConfiguration("biz_date","hisAlgo"));
           return result;
       }
   
       @Bean("sqlSessionFactoryForShardingJdbc")
       public SqlSessionFactory sqlSessionFactoryForShardingJdbc(@Qualifier("shardingDataSource") DataSource dataSource) throws Exception {
   
           MybatisConfiguration configuration = new MybatisConfiguration();
           configuration.setMapUnderscoreToCamelCase(true);
           configuration.setCacheEnabled(false);
           configuration.setCallSettersOnNulls(true);
           configuration.setJdbcTypeForNull(JdbcType.NULL);
           configuration.addInterceptor(new PaginationInterceptor());
   
           MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
           sqlSessionFactoryBean.setConfiguration(configuration);
           sqlSessionFactoryBean.setDataSource(dataSource);
   
           return sqlSessionFactoryBean.getObject();
       }
   }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org