https://github.com/silianpan/seal-sharding
Demo project for Sharding-JDBC and Mybaits-Plus
https://github.com/silianpan/seal-sharding
mybatis-plus sharding sharding-jdbc sharding-jdbc-mybaits sharding-sphere spring-boot
Last synced: 6 months ago
JSON representation
Demo project for Sharding-JDBC and Mybaits-Plus
- Host: GitHub
- URL: https://github.com/silianpan/seal-sharding
- Owner: silianpan
- Created: 2020-05-27T09:27:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-06-17T03:14:11.000Z (over 3 years ago)
- Last Synced: 2025-04-11T01:49:48.302Z (6 months ago)
- Topics: mybatis-plus, sharding, sharding-jdbc, sharding-jdbc-mybaits, sharding-sphere, spring-boot
- Language: Java
- Homepage:
- Size: 115 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sharding-Jdbc集成mybatis-plus实现分库分表解决方案
项目地址:
* [Github](https://github.com/silianpan/seal-sharding)
* [Gitee](https://gitee.com/twofloor/seal-sharding)官方文档:
* [Sharding-JDBC](https://shardingsphere.apache.org/document/legacy/3.x/document/cn/overview/)
* [Mybatis-Plus](https://mp.baomidou.com/)## 一、运行测试
* Step 1:新建两个数据库分别为**seal_sharding、seal_sharding1**
* Step 2:分别导入db目录下sql文件
* Step 3:打开项目,配置环境变量**MYSQL_HOST、MYSQL_PORT**以及用户名和密码
* Step 4:运行测试文件(test包下)## 二、租户ID的获取
> 修改config包下MybaitsPlusConfig.java,获取当前登录用户的租户ID,这里默认写1
```java
@Override
public Expression getTenantId() {
try {
// 获取当前登录用户租户ID
String tenantId = "1";
if (!StrUtil.isEmpty(tenantId)) {
return new LongValue(tenantId);
}
throw new Exception(RetBack.errorJson(4001, "该租户不存在"));
} catch (Exception e) {
e.printStackTrace();
}
return new LongValue(-1L);
}
```## 三、分库分表策略
* 分库:根据表字段 **租户ID(tenant_id)** 模2(奇偶数)进行分库
* 分表:根据表字段 **id** 去hascode模2(奇偶数)进行发分表,分布式主键采用**雪花算法(SNOWFLAKE)**,详见:[Sharding-JDBC](https://shardingsphere.apache.org/document/legacy/3.x/document/cn/features/sharding/other-features/key-generator/)## 三、只分表不分库
本项目为两个数据源实现分库和分表,如果只分表不分库,只需要添加一个数据源即可## 四、实现注意点
* [Sharding-JDBC](https://shardingsphere.apache.org/document/legacy/3.x/document/cn/overview/)采用**3.x**版本
* 需要复写MybaitsPlus配置
* 排除自动数据源加载
```java
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class,
DataSourceAutoConfiguration.class})
```## 附、Mybatis-Plus多数据源
如果不使用Sharding-JDBC,而需要**多数据源**,可以采用**Mybaits-Plus多数据源方案**,详见[官方文档](https://mp.baomidou.com/guide/dynamic-datasource.html),后面我会把**示例项目**分享给大家,敬请期待。可以加我互相交流。
**开源不易,且用且珍惜!**
赞助作者,互相交流![]()
![]()