spring boot + sharding jdbc +druid +mybatis 按照年份分库,按照月份分表实现

一,maven引用相关jar包配置

<!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-boot-starter -->
    <dependency>
      <groupId>org.apache.shardingsphere</groupId>
      <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
      <version>4.1.1</version>
    </dependency>

<dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.1</version>
    </dependency>
 <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.21</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.23</version>
    </dependency>

二,创建数据库ds2020,ds2021 表user01-user12,入下图所示

三,prpperties配置

spring.shardingsphere.datasource.names=ds2020,ds2021
spring.shardingsphere.datasource.ds2020.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds2020.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds2020.url=jdbc:mysql://localhost:3306/ds2020?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.shardingsphere.datasource.ds2020.username=root
spring.shardingsphere.datasource.ds2020.password=123123

spring.shardingsphere.datasource.ds2021.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds2021.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds2021.url=jdbc:mysql://localhost:3306/ds2021?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.shardingsphere.datasource.ds2021.username=root
spring.shardingsphere.datasource.ds2021.password=123123

spring.shardingsphere.sharding.tables.account_info.actual-data-nodes=ds$->{2020..2021}.account_info$->{0..1}
spring.shardingsphere.sharding.tables.account_info.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.account_info.table-strategy.inline.algorithm-expression=account_info$->{id % 2}
spring.shardingsphere.sharding.tables.account_info.key-generator.column=id
spring.shardingsphere.sharding.tables.account_info.key-generator.type=SNOWFLAKE

spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds$->{2020..2021}.user0$->{1..9},ds$->{2020..2021}.user1$->{0..2}
spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.tp.user_server2.config.sharding.PreciseModuloTableShardingAlgorithm
spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=create_time
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE

spring.shardingsphere.sharding.binding-tables=account_info
#spring.shardingsphere.sharding.broadcast-tables=t_config
spring.shardingsphere.props.sql.show=true

spring.shardingsphere.sharding.default-database-strategy.standard.precise-algorithm-class-name=com.tp.user_server2.config.sharding.PreciseModuloDatabaseShardingAlgorithm
spring.shardingsphere.sharding.default-database-strategy.standard.sharding-column=create_time

说明:从上面properties配置中我们可以发现,user和account_info的分表算法配置是不同的,account_info使用的分片表达式即根据id的奇偶情况,分别将数据存储在不同的表里。user使用的是标准精确分类算法

四,数据库分片算法

public class PreciseModuloDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        //对于库的分片collection存放的是所有的库的列表,这里代表ds2020~ds2021
        //配置的分片的sharding-column对应的值
        String timeValue =  TimeUtil.getTimeStr(preciseShardingValue.getValue());
        //分库时配置的sharding-column
        String time = preciseShardingValue.getColumnName();
        //需要分库的逻辑表
        String table = preciseShardingValue.getLogicTableName();
        if(StringUtils.isBlank(timeValue)){
            throw new UnsupportedOperationException("preciseShardingValue is null");
        }
        //按年路由
        for (String each : collection) {
            String value = StringUtils.substring(timeValue,0,4); //获取到年份
            if(each.endsWith(value)){
                // //这里返回回去的就是最终需要查询的库名
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }
}

五,分表分片算法

public class PreciseModuloTableShardingAlgorithm implements PreciseShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        //对于库的分片collection存放的是所有的库的列表,这里代表user01~user12
        //配置的分片的sharding-column对应的值
        String timeValue =  TimeUtil.getTimeStr(preciseShardingValue.getValue());
        //分库时配置的sharding-column
        String time = preciseShardingValue.getColumnName();
        //需要分库的逻辑表
        String table = preciseShardingValue.getLogicTableName();
        if(StringUtils.isBlank(timeValue)){
            throw new UnsupportedOperationException("preciseShardingValue is null");
        }
        //按月路由
        for (String each : collection) {
            String value = StringUtils.substring(timeValue,5,7); //获取到月份
            System.out.println("------------------------------------------------"+value);
            if(each.endsWith(value)){
                //这里返回回去的就是最终需要查询的表名
                return each;
            }
        }
        return null;
    }
}

六:测试

@RestController
@RequestMapping("/user0")
public class UserController {
    @Autowired
    private IUserService userService;

    @ResponseBody
    @GetMapping(value = "/saveUser")
    public String saveAccount(){

        User user = new User();
        user.setAccountName("张三");
        user.setCreateTime(new Date());
        userService.save(user);

        return "1";
    }
}

七:测试结果

git代码存放位置:https://gitee.com/taopeng98/spring_cloud_distributed   分库分表实现在user_server2模块中

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以为你提供一些关于使用SpringBoot ShardingJDBC MyBatis进行分库的建议,例如:1. 使用ShardingSphere框架定义数据库分片策略;2. 使用MyBatis实现MyBatis SQL脚本;3. 使用SpringBoot注解的方式定义数据源和ShardingJDBC的配置。 ### 回答2: 使用SpringBoot ShardingJDBCMyBatis可以很方便地实现分库功能。首先,ShardingJDBC是一个轻量级的数据库中间件,可以将数据分散到不同的数据库实例中,从而实现分库的效果。其次,MyBatis是一个流行的持久层框架,可以通过XML或注解的方式与数据库进行交互。 在使用SpringBoot ShardingJDBCMyBatis分库时,首先需要配置ShardingJDBC的数据源和分片规则。可以通过编写一个配置类来配置分库的规则,例如可以根据某个字段的取值来确定数据应该分散到哪个库或中。配置完成后,就可以在MyBatis的Mapper接口中直接使用分库的数据源,从而实现对不同数据库或的访问。 在编写Mapper接口时,可以使用MyBatis提供的注解或XML方式来编写SQL语句。在SQL语句中,可以使用ShardingJDBC提供的分片键来实现对特定库或的访问。例如,在需要查询特定的数据时,可以使用ShardingJDBC提供的Hint注解将查询操作路由到相应的上。 总的来说,使用SpringBoot ShardingJDBCMyBatis可以实现简单、高效的分库功能。通过配置ShardingJDBC的分片规则和使用MyBatis编写SQL语句,可以将数据分散到不同的数据库实例和中,从而实现了水平扩展和负载均衡的效果。这种方式能够帮助我们提高数据库的性能和容量,从而更好地应对大规模的数据存储需求。 ### 回答3: 使用SpringBoot ShardingJDBC MyBatis可以轻松实现分库。 首先,ShardingJDBC是一个分库的开源框架,它可以通过数据库中间件实现数据的分散存储。而SpringBoot是一个快速构建项目的框架,可以帮助开发者轻松集成各种组件。 使用SpringBoot ShardingJDBC MyBatis进行分库,首先需要配置ShardingJDBC的数据源、分片策略以及分策略。可以通过配置文件或者编程方式来完成配置。配置数据源时,可以指定多个数据库的连接信息,并使用分片策略将数据分配到不同的数据库中。配置分策略时,可以指定不同的分规则,将数据根据一定的规则分散存储在不同的中。 在具体的业务逻辑中,可以使用MyBatis来操作数据库。MyBatis是一个简化数据库访问的持久层框架,通过编写SQL语句和映射文件,可以轻松实现数据库的增删改查操作。 在访问数据库时,ShardingJDBC会根据配置的分片策略和分策略,自动将数据路由到指定的数据库和中。开发者不需要关心数据的具体存储位置,只需要使用MyBatis的API进行数据操作即可。 使用SpringBoot ShardingJDBC MyBatis进行分库,可以提高数据库的读写性能,增加数据的存储容量,并且可以实现数据的动态扩容和迁移。此外,由于SpringBoot和MyBatis的高度集成,开发者可以更加方便地进行开发和维护。 总之,使用SpringBoot ShardingJDBC MyBatis进行分库可以帮助开发者更好地管理数据,提升系统的性能和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值