栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

sharding-jdbc

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

sharding-jdbc

一,配置文件

1.参考官网springboot的配置

使用 Spring Boot Starter :: ShardingSphere

2.注意公共配置用common标识,如下图,否则项目启动报错。参考别人踩过的坑:

  springboot2.0集成ShardingSphere-jdbc5.0-alpha所遇到的一些坑 - 简书

shardingsphere 配置启动报错 - org.apache.shardingsphere.spring.boot.SpringBootConfiguration_曦陽惜夏的博客-CSDN博客

 3.完整配置

spring:
  application:
    name: my-projects

  datasource:
    default:
      jdbc-url: jdbc:mysql:/
@Configuration
@MapperScan(basePackages = "com.shenke.myprojects.dao.mapper.suo", sqlSessionTemplateRef = "defaultSqlSessionTemplate")
public class DefaultDataSourceConfig {

    @Autowired
    private PaginationInterceptor paginationInterceptor;

    
    @Bean(name = "defaultDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.default")
    public DataSource defaultDataSource() {
	return DataSourceBuilder.create().build();
    }

    @Bean(name = "defaultSqlSessionFactory")
    @Primary
    public SqlSessionFactory defaultSqlSessionFactory(@Qualifier("defaultDataSource") DataSource dataSource)
		    throws Exception {
	MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
	bean.setDataSource(dataSource);
	Interceptor[] plugins = { paginationInterceptor };
	bean.setPlugins(plugins);
	bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/suo
    @Bean(name = "defaultTransactionManager")
    @Primary
    public DataSourceTransactionManager defaultDataSourceTransactionManager(
		    @Qualifier("defaultDataSource") DataSource dataSource) {
	return new DataSourceTransactionManager(dataSource);
    }

    
    @Bean(name = "defaultSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate defaultSqlSessionTemplate(
		    @Qualifier("defaultSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
	return new SqlSessionTemplate(sqlSessionFactory);
    }

}
2,分表数据源
@Configuration
@MapperScan(basePackages = "com.shenke.myprojects.dao.mapper.sharding", sqlSessionTemplateRef = "shardingSqlSessionTemplate")
public class ShardingDataSourceConfig {

    @Autowired
    private PaginationInterceptor paginationInterceptor;

    
    @Bean(name = "shardingSqlSessionFactory")
    public SqlSessionFactory shardingSqlSessionFactory(@Qualifier("shardingSphereDataSource") DataSource dataSource)
		    throws Exception {
	MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
	bean.setDataSource(dataSource);
	Interceptor[] plugins = { paginationInterceptor };
	bean.setPlugins(plugins);
	bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/sharding
    @Bean(name = "shardingTransactionManager")
    public DataSourceTransactionManager shardingDataSourceTransactionManager(
		    @Qualifier("shardingSphereDataSource") DataSource dataSource) {
	return new DataSourceTransactionManager(dataSource);
    }

    
    @Bean(name = "shardingSqlSessionTemplate")
    public SqlSessionTemplate sharingSqlSessionTemplate(
		    @Qualifier("shardingSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
	return new SqlSessionTemplate(sqlSessionFactory);
    }

}
三,引入依赖
 
            org.apache.shardingsphere
            shardingsphere-jdbc-core-spring-boot-starter
            ${shardingsphere.version}
        
        
            com.baomidou
            mybatis-plus-boot-starter
            2.3
        
        
            com.baomidou
            mybatis-plus-extension
            3.1.2
        
        
            mysql
            mysql-connector-java
        
 四,建表语句

shard_item 分表字段

DROP TABLE
IF
	EXISTS `player`;
CREATE TABLE `player` (
	`id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT,
	`shard_item` INT ( 10 ) NOT NULL COMMENT '分片字段',
	`name` VARCHAr ( 20 ) NOT NULL DEFAULT '' COMMENT '姓名',
	`number` INT ( 10 ) NOT NULL COMMENT '号码',
	`team` VARCHAr ( 20 ) NOT NULL DEFAULT '' COMMENT '球队',
	`is_delete` TINYINT ( 1 ) NOT NULL DEFAULT '0' COMMENT '删除标记 0:正常 1:删除',
	`created_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
	`created_by` VARCHAr ( 30 ) NOT NULL DEFAULT 'sys' COMMENT '创建人',
	`updated_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
	`updated_by` VARCHAr ( 30 ) NOT NULL DEFAULT 'sys' COMMENT '更新人',
	PRIMARY KEY ( `id` )
) ENGINE = INNODB COMMENT = '运动员表';

主键问题

1.用redis自增数据作为分表字段,避免分表主键重复

2.为什么不用雪花算法,存在时钟回拨,导致主键重复

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号