shardingjdbc 1.5配置

package cn.licoy.dbs.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.mysql.jdbc.Driver;
import org.hibernate.mapping.IdGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author licoy.cn
 * @version 2017/9/30
 */
@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource getDataSource() throws SQLException {
        return buildDataSource();
    }


   /*
    * 
    *  **
     * 分库分表每个库都有一张user_login_log_0,user_login_log_1
     * @return
     * @throws SQLException
     *
     private DataSource buildDataSource() throws SQLException {
        //设置分库映射
        Map<String,DataSource> dataSourceMap = Collections.synchronizedMap(new HashMap<>());
        //添加数据库
        dataSourceMap.put("dbs_0",createDataSource("dbs_0"));
        dataSourceMap.put("dbs_1",createDataSource("dbs_1"));
        //设置默认数据库
        DataSourceRule rule = new DataSourceRule(dataSourceMap,"dbs_0");
        
        Map<String,DataSource> dataSourceMap0 = Collections.synchronizedMap(new HashMap<>());
        dataSourceMap0.put("dbs_0",createDataSource("dbs_0"));
        DataSourceRule rule0 = new DataSourceRule(dataSourceMap0);
        
        Map<String,DataSource> dataSourceMap1 = Collections.synchronizedMap(new HashMap<>());
        dataSourceMap1.put("dbs_1",createDataSource("dbs_1"));
        DataSourceRule rule1 = new DataSourceRule(dataSourceMap1);
        

        TableRule orderTableRule0 = TableRule.builder("user_login_log")
                .actualTables(Arrays.asList("user_login_log_0"))
                .dataSourceRule(rule0)
                .generateKeyColumn("id")
                .build();
        
        
        TableRule orderTableRule1 = TableRule.builder("user_login_log")
                .actualTables(Arrays.asList("user_login_log_1"))
                .dataSourceRule(rule1)
                .generateKeyColumn("id")
                .build();
        
        
        TableRule orderTableRuleAll = TableRule.builder("user_login_log")
                .actualTables(Arrays.asList("user_login_log_0","user_login_log_1"))
                .dataSourceRule(rule)
                .generateKeyColumn("id")
                .build();
        

        ShardingRule shardingRule = ShardingRule.builder()
                .dataSourceRule(rule)
                .tableRules(Arrays.asList(orderTableRuleAll))
                .databaseShardingStrategy(new DatabaseShardingStrategy("user_id",new ModuloDatabaseShardingAlgorithm()))
                .tableShardingStrategy(new TableShardingStrategy("id",new ModuloTableShardingAlgorithm()))
                .build();

        Properties props=new Properties();
        props.put("sql.show", "true");
        
		DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule, props);

        return dataSource;
    }*/


    
    
  /*  *//**
     * 只分库每个库只有一张user_login_log
     * @return
     * @throws SQLException
     *//*
    private DataSource buildDataSource() throws SQLException {
        //设置分库映射
        Map<String,DataSource> dataSourceMap = Collections.synchronizedMap(new HashMap<>());
        //添加数据库
        dataSourceMap.put("dbs_0",createDataSource("dbs_0"));
        dataSourceMap.put("dbs_1",createDataSource("dbs_1"));
        //设置默认数据库
        DataSourceRule rule = new DataSourceRule(dataSourceMap,"dbs_1");
        
        TableRule orderTableRuleAll = TableRule.builder("user_login_log")
                .actualTables(Arrays.asList("user_login_log"))
                .dataSourceRule(rule)
                .generateKeyColumn("id")
                .build();

        ShardingRule shardingRule = ShardingRule.builder()
                .dataSourceRule(rule)
                .tableRules(Arrays.asList(orderTableRuleAll))
                .databaseShardingStrategy(new DatabaseShardingStrategy("id",new ModuloDatabaseShardingAlgorithm()))
                .build();

        Properties props=new Properties();
        props.put("sql.show", "true");
        
		DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule, props);

        return dataSource;
    }
    
    private static DataSource createDataSource(final String dataSourceName) {
        //使用druid连接数据库
        DruidDataSource result = new DruidDataSource();
        result.setDriverClassName(Driver.class.getName());
        result.setUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName));
        result.setUsername("root");
        result.setPassword("123456");
        return result;
    }
*/
    
    
    /**
     * 只分表每个库只有2张user_login_log_0,user_login_log_1
     * @return
     * @throws SQLException
     */
    private DataSource buildDataSource() throws SQLException {
        //设置分库映射
        Map<String,DataSource> dataSourceMap = Collections.synchronizedMap(new HashMap<>());
        //添加数据库
        dataSourceMap.put("dbs_0",createDataSource("dbs_0"));
        //设置默认数据库
        DataSourceRule rule = new DataSourceRule(dataSourceMap,"dbs_0");
        
        TableRule orderTableRuleAll = TableRule.builder("user_login_log")
                .actualTables(Arrays.asList("user_login_log_0","user_login_log_1"))
                .dataSourceRule(rule)
                .generateKeyColumn("id")
                .build();

        ShardingRule shardingRule = ShardingRule.builder()
                .dataSourceRule(rule)
                .tableRules(Arrays.asList(orderTableRuleAll))
                .tableShardingStrategy(new TableShardingStrategy("id",new ModuloTableShardingAlgorithm()))
                // .databaseShardingStrategy(new DatabaseShardingStrategy("id",new ModuloDatabaseShardingAlgorithm()))
                .build();

        Properties props=new Properties();
        props.put("sql.show", "true");
        
		DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule, props);

        return dataSource;
    }
    
    private static DataSource createDataSource(final String dataSourceName) {
        //使用druid连接数据库
        DruidDataSource result = new DruidDataSource();
        result.setDriverClassName(Driver.class.getName());
        result.setUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName));
        result.setUsername("root");
        result.setPassword("123456");
        return result;
    }


}

猜你喜欢

转载自my.oschina.net/xiaominmin/blog/1826198