Shard DataSource

动态数据源路由
本文介绍了一种名为动态数据源路由的技术,该技术允许应用程序在运行时根据特定条件选择不同的数据源进行操作。此方法增强了系统的灵活性和可扩展性。
[url]http://opensource.atlassian.com/projects/hibernate/browse/HSHARDS-37;jsessionid=a-i8XZ_53LB5aHxsU2?page=com.atlassian.jira.ext.fisheye%3Afisheye-issuepanel[/url]

[b]dynamic datasource routing[/b]

[url]http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/[/url]
@Configuration public class MyBatisConfig { @Bean("shardingSqlSessionFactory") public SqlSessionFactory shardingSqlSessionFactory( @Qualifier("shardingDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath*:mapper/sharding/*.xml")); return bean.getObject(); } @Bean("defaultSqlSessionFactory") public SqlSessionFactory defaultSqlSessionFactory(@Qualifier("defaultDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath*:mapper/default/*.xml")); return factoryBean.getObject(); } } @Configuration public class DataSourceConfig { @Bean("defaultDataSource") @ConfigurationProperties(prefix = "spring.sharding-sphere.datasource.shard1") public DataSource defaultDataSource() { return DataSourceBuilder.create().build(); } } @Configuration public class MapperScannerConfig { @Bean public MapperScannerConfigurer shardingMapperScannerConfigurer(@Qualifier("shardingSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setSqlSessionFactory(sqlSessionFactory); configurer.setBasePackage("com.example.mapper.sharding"); // 分片表的 Mapper 接口 return configurer; } @Bean public MapperScannerConfigurer defaultMapperScannerConfigurer(@Qualifier("defaultSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setSqlSessionFactory(sqlSessionFactory); configurer.setBasePackage("com.example.mapper.defaultdb"); // 普通表的 Mapper 接口 return configurer; } }spring: application: name: coupon-send-service sharding-sphere: datasource: names: shard0,shard1 shard0: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/sk_day09?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username: root password: 123456 # Druid连接池配置 initial-size: 5 min-idle: 5 max-active: 20 shard1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/sk_day10?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username: root password: 123456 # Druid连接池配置 initial-size: 5 min-idle: 5 max-active: 20 sharding: tables: task_user: actual-data-nodes: shard$->{0..1}.task_user database-strategy: inline: sharding-column: user_id algorithm-expression: shard$->{user_id % 2} props: sql: show: true 这些是按你说的改的代码,现在还是报org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shardingMapperScannerConfigurer' defined in class path resource [com/c/c_coupon_send/config/MapperScannerConfig.class]: Unsatisfied dependency expressed through method 'shardingMapperScannerConfigurer' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shardingSqlSessionFactory' defined in class path resource [com/c/c_coupon_send/config/MyBatisConfig.class]: Unsatisfied dependency expressed through method 'shardingSqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.apache.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration.<init>()
09-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值