# 分库分表 **Repository Path**: ycfrank/sharding ## Basic Information - **Project Name**: 分库分表 - **Description**: sharding-jdbc-spring-boot-starter 是 ShardingSphere 早期提供的启动器 shardingsphere-jdbc-core-spring-boot-starter 是 ShardingSphere 5.x 版本之后提供的启动器 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-07-20 - **Last Updated**: 2023-09-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 两个项目的依赖虽然不同,但是都在的父项目下 ```xml org.apache.shardingsphere shardingsphere X.X.X ``` `sharding-jdbc-spring-boot-starter` 这个依赖包,没有过多的额外依赖 `shardingsphere-jdbc-core-spring-boot-starter` 相对于添加许多额外的功能,如发现、事务 sharding-jdbc-spring-boot-starter 是 ShardingSphere 早期提供的启动器,用于在 Spring Boot 项目中快速集成 ShardingSphere。这个启动器包含了 ShardingSphere 的 JDBC 和 Spring Boot 的自动配置。在使用 sharding-jdbc-spring-boot-starter 时,用户只需要配置一些必要的参数,例如数据源、分片规则等,就可以快速地搭建起一个具有分片、读写分离、高可用等特性的数据库架构。 shardingsphere-jdbc-core-spring-boot-starter 是 ShardingSphere 5.x 版本之后提供的启动器,仅包含了 ShardingSphere 的 JDBC。与 sharding-jdbc-spring-boot-starter 不同,这个启动器没有内置的自动配置,需要用户手动配置 Spring Boot 的相关参数。在使用 shardingsphere-jdbc-core-spring-boot-starter 时,用户需要配置数据源、分片规则、权限规则等,以实现数据库的分片、读写分离、高可用等特性。 sharding-jdbc-spring-boot-starter 可以看作是 shardingsphere-jdbc-core-spring-boot-starter 的一个封装,对于使用早期版本的 ShardingSphere 用户来说,可以使用 sharding-jdbc-spring-boot-starter 来快速集成 ShardingSphere 到 Spring Boot 项目中。而新版本的 ShardingSphere 用户则可以直接使用 shardingsphere-jdbc-core-spring-boot-starter,根据自己的需要进行手动配置。 两者针对表逻辑的规则的配置略有不同 sharding-jdbc-spring-boot-starter` ```yml spring: shardingsphere: sharding: tables: user: actualDataNodes: db1.user_$->{1..2} keyGenerator: column: id type: SNOWFLAKE tableStrategy: inline: shardingColumn: id algorithmExpression: user_$->{id % 2 + 1} ``` `shardingsphere-jdbc-core-spring-boot-starter` 的配置逻辑如下,外包一层 rules ```yaml spring: shardingsphere: # 分片规则配置 shardingsphere 这个整体的配置 rules: sharding: # 分片算法配置 sharding-algorithms: database-inline: # 分片算法类型 type: INLINE props: # 分片算法的行表达式(算法自行定义,此处为方便演示效果) algorithm-expression: db$->{order_id > 4?1:0} table-inline: # 分片算法类型 type: INLINE props: # 分片算法的行表达式 algorithm-expression: t_order_$->{order_id % 4} tables: # 逻辑表名称 t_order: # 行表达式标识符可以使用 ${...} 或 $->{...},但前者与 Spring 本身的属性文件占位符冲突,因此在 Spring 环境中使用行表达式标识符建议使用 $->{...} actual-data-nodes: db${0..1}.t_order_${0..3} # 分库策略 database-strategy: standard: # 分片列名称 sharding-column: order_id # 分片算法名称 sharding-algorithm-name: database-inline # 分表策略 table-strategy: standard: # 分片列名称 sharding-column: order_id # 分片算法名称 sharding-algorithm-name: table-inline ```