jdbc sharding yml配置_ShardingSphere + MyBatis + Spring Boot单数据源分表配置

本文介绍了如何在Spring Boot应用中使用ShardingSphere实现单数据源分表配置,详细讲解了POM配置、YAML配置以及Java配置,并解决了分表字段校验的问题。
摘要由CSDN通过智能技术生成

我的项目中依赖的DB很多,但是并不都需要通过ShardingSphere访问,所以需要额外配置一个通过ShardingSphere访问的数据源。我当初的第一选择是引入sharding-jdbc-spring-boot-starter,但是目前sharding-jdbc-spring-boot-starter在没有按照starter要求进行配置的情况下会报错。因此放弃了,改为单独引用sharding-jdbc。

POM

io.shardingsphere

sharding-jdbc

3.1.0

YAML配置

这是最坑的一部分,官网上的Yaml配置配上了根本不生效,于是根据源码的类自己写了yml文件。

两张表:stuff、company

分表规则:userid % 16sharding-sphere:

datasources:

test:

jdbc-url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK&zeroDateTimeBehavior=convertToNull&noAccessToProcedureBodies=true

username: test

password: test

max-pool-size: 5

pool-name: ironman-hikari-pool

min-idle: 1

connection-timeout: 15000

idle-timeout: 300000

sharding-rules:

tableRule-configs:

-

logicTable: stuff

actualDataNodes: "test.stuff$->{0..15}"

tableShardingStrategyConfig:

shardingColumn: userid

algorithmExpression: stuff$->{userid % 16}

-

logicTable: company

actualDataNodes: "test.company$->{0..15}"

tableStrategy:

shardingColumn: userid

algorithmExpression: company{userid % 16}

binding-tables:

- stuff, company

default-dataSource-name: "test"

Java中的配置@Configuration

@MapperScan(basePackages = "com.garin.testapp.testapp-dal.mapper",

sqlSessionFactoryRef = "shardingMybatisSqlSessionFactory")

public class ShardingDataSourceConfig {

@Bean(name = "shardingRuleConf")

@ConfigurationProperties(prefix = "sharding-sphere.sharding-rules")

public ShardingRuleConfiguration shardingRuleConf() {

return new ShardingRuleConfiguration();

}

@Bean(name = "mysqlDatasource")

@ConfigurationProperties(prefix = "sharding-sphere.datasources.test")

public DataSource mysqlDatasource() {

return DataSourceBuilder.create().type(HikariDataSource.class).build();

}

@Bean(name = "shardingDatasource")

public DataSource shardingDatasource(@Qualifier("shardingRuleConf") ShardingRuleConfiguration shardingRuleConf,

@Qualifier("mysqlDatasource") DataSource mysqlDatasource)

throws SQLException {

Map dataSourceMap = new HashMap<>();

dataSourceMap.put("test", mysqlDatasource);

return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConf, new HashMap<>(), new Properties());

}

@Bean("shardingMybatisSqlSessionFactory")

public SqlSessionFactory shardingMybatisSqlSessionFactory(@Qualifier("shardingDatasource") DataSource dataSource)

throws Exception {

SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();

sessionFactoryBean.setDataSource(dataSource);

String patternPath = ResourcePatternResolver.CLASSPATH_URL_PREFIX + "sharding/*Mapper.xml";

sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()

.getResources(patternPath));

sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);

return sessionFactoryBean.getObject();

}

}

其他问题

引入后遇到了一些报错,sharding-sphere会对分表的字段在项目启动时做强校验,校验每个分表的字段、字段的顺序做强校验。校验失败会导致项目启动失败。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值