您的位置:首页 > 编程语言 > Java开发

springboot分表sharding-jdbc-core

2017-09-19 15:05 706 查看
1,maven配置

<dependency>
<groupId>com.dangdang</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>1.5.3</version>
</dependency>


2,XbDataSource

用Sharding JDBC封装的DataSource

@Component
public class XbDataSource {
@Autowired
DataSource dataSource;

DataSource shardingDataSource;

@PostConstruct
public void init() throws SQLException {
Map<String, DataSource> map = new HashMap<String, DataSource>();
map.put("dataSource", dataSource);
DataSourceRule dataSourceRule = new DataSourceRule(map);

List<TableRule> tableRuleList = new ArrayList<>();
List<String> pList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
pList.add("tb_p_" + i);
}
//tb_p逻辑表名,pList实际所有的分表
tableRuleList.add(new TableRule.TableRuleBuilder("tb_p")
.actualTables(pList)
.dataSourceRule(dataSourceRule)
.tableShardingStrategy(new TableShardingStrategy("id", new ProgramShardingAlgorithm())).build());
ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(tableRuleList)
.build();

shardingDataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
}

public DataSource getDataSource() {
return shardingDataSource;
}
}


3,MyBatisConfig 将xdatesource配置到mybatis

@Autowired
XbDataSource xbDataSource;//集成sharding-jdbc分表数据源

@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//		sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setDataSource(xbDataSource.getDataSource());
sqlSessionFactoryBean.setTypeAliasesPackage("com.uway.mobile.domain");
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperPath));
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionFactoryBean.getObject();
}


4,ProgramShardingAlgorithm


具体的策略算法

import java.util.Collection;
import java.util.LinkedHashSet;

import org.springframework.stereotype.Component;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import com.google.common.collect.Range;

@Component
public class ProgramShardingAlgorithm implements SingleKeyTableShardingAlgorithm<Integer> {
/**
* equals比较条件
* @param availableTargetNames
* @param shardingValue
* @return
*/
@Override
public String doEqualSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() %3+ "")) {
return each;
}
}
throw new UnsupportedOperationException();
}

/**
* in比较条件
* @param availableTargetNames
* @param shardingValue
* @return
*/
@Override
public Collection<String> doInSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
Collection<String> result = new LinkedHashSet<String>(availableTargetNames.size());
Collection<Integer> values = shardingValue.getValues();
for (Integer value : values) {
for (String tableNames : availableTargetNames) {
if (tableNames.endsWith(value % 3 + "")) {
result.add(tableNames);
}
}
}
return result;
}

/**
* between比较条件
* @param availableTargetNames
* @param shardingValue
* @return
*/
@Override
public Collection<String> doBetweenSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
Collection<String> result = new LinkedHashSet<String>(availableTargetNames.size());
Range<Integer> range = shardingValue.getValueRange();
for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
for (String each : availableTargetNames) {
if (each.endsWith(i % 3+ "")) {
result.add(each);
}
}
}
return result;
}

}


简单应用并没有使用id生产策略,没有分库。

具体应用类,mapper文件都按正常方式编写即可。

参考
http://m.blog.csdn.net/hpb21/article/details/53352803#sharding-jdbc分表配置 http://blog.csdn.net/tianyaleixiaowu/article/details/70242971
sharding使用限制
http://blog.csdn.net/farrell_zeng/article/details/52958181
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springboot mysql 分库分