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

Sharding-jdbc 分表不分库注意事项

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

Sharding-jdbc 分表不分库注意事项

前言:在根据现在官网的配置老是报错,

就如数据库连接spring.shardingsphere.datasource.m1.url

现在官网是spring.shardingsphere.datasource.m1.jdbc-url

后者始终报错,我也不知道为什么,所以以此记录

1.依赖

        
            com.alibaba
            druid-spring-boot-starter
            1.1.20
        
        
        
            org.apache.shardingsphere
            sharding-jdbc-spring-boot-starter
            4.0.0-RC1
        

2.配置文件(用的是mysql 8.0)


server.port=10086

# 配置真实数据源
spring.shardingsphere.datasource.names=m1

# 一个实体对应一个数据库多张表必须设置这个
spring.main.allow-bean-definition-overriding=true

# 配置第 1 个数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://192.168.0.76:3306/btb_coffee?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root

# 配置表规则
spring.shardingsphere.sharding.tables.demotable.actual-data-nodes=m1.demotable_$->{1..2}

# 指定demotable表里面主键id 生成策略  雪花算法SNOWFLAKE
spring.shardingsphere.sharding.tables.demotable.key-generator.column=id
spring.shardingsphere.sharding.tables.demotable.key-generator.type=SNOWFLAKE

# 指定表分片策略  约定id值偶数添加到demotable_1表,如果cid是奇数添加到demotable_2表
spring.shardingsphere.sharding.tables.demotable.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.demotable.table-strategy.inline.algorithm-expression=demotable_$->{id % 2 + 1}

# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318809.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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