sharding jdbc 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 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 dataSourceMap0 = Collections.synchronizedMap(new HashMap<>()); dataSourceMap0.put("dbs_0",createDataSource("dbs_0")); DataSourceRule rule0 = new DataSourceRule(dataSourceMap0); Map 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 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 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; } }

转载于:https://my.oschina.net/xiaominmin/blog/1829394

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值