zoukankan      html  css  js  c++  java
  • sharding-jdbc实现垂直分库

    对 t_user的操作在数据库 user_db中进行

    1、需求分析

    clipboard

    2、创建数据库和表(略)

    clipboard

    3、编写代码

    (1)创建实体类User 和mapper

    @Data
    @TableName("t_user")
    public class User {
    
        private Long userId;
    
        private String username;
    
        private String ustatus;
    }
    
    
    public interface UserMapper extends BaseMapper<User> {
    }

    (2)配置垂直分库

    #sharding-jdbc 分片策略
    # 数据源名称,多数据源以逗号分隔
    # 水平分库,所以要配置多数据源
    spring.shardingsphere.datasource.names=m1,m2,m0
    
    #一个实体类对应两张表
    spring.main.allow-bean-definition-overriding=true
    
    # 第一个数据源
    # 数据库连接池
    spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
    #数据库驱动类名
    spring.shardingsphere.datasource.m1.driver-class-name= com.mysql.cj.jdbc.Driver
    # 数据库 URL 连接
    spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8
    # 数据库用户名
    spring.shardingsphere.datasource.m1.username= root
    # 数据库密码
    spring.shardingsphere.datasource.m1.password=houchen
    
    
    # 第二个数据源
    # 数据库连接池
    spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
    #数据库驱动类名
    spring.shardingsphere.datasource.m2.driver-class-name= com.mysql.cj.jdbc.Driver
    # 数据库 URL 连接
    spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
    # 数据库用户名
    spring.shardingsphere.datasource.m2.username= root
    # 数据库密码
    spring.shardingsphere.datasource.m2.password=houchen
    
    
    # 第三个数据源
    # 数据库连接池
    spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
    #数据库驱动类名
    spring.shardingsphere.datasource.m0.driver-class-name= com.mysql.cj.jdbc.Driver
    # 数据库 URL 连接
    spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
    # 数据库用户名
    spring.shardingsphere.datasource.m0.username= root
    # 数据库密码
    spring.shardingsphere.datasource.m0.password=houchen
    
    
    #配置user_db数据库里面 t_user 专库专表
    spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m$->{0}.t_user
    
    #指定 t_user 表中主键的生成策略  SNOWFLAKE:雪花算法
    spring.shardingsphere.sharding.tables.t_user.key-generator.column = user_Id
    spring.shardingsphere.sharding.tables.t_user.key-generator.type =SNOWFLAKE
    
    
    #打开sql的输出日志
    spring.shardingsphere.props.sql.show = true

    (3)测试

    //测试垂直分库
    @Test
    public void testUserDB(){
        User user =new User();
        user.setUsername("luccy");
        user.setUstatus("c");
        userMapper.insert(user);
    }

    clipboard

  • 相关阅读:
    mongodb 条件查询
    node 创建静态web服务器(下)(处理异步获取数据的两种方式)
    node 创建静态web服务器(上)
    node.js 从文件流中读写数据及管道流
    node.js 中的 fs (文件)模块
    vue-router 嵌套路由
    (转载)js调用打印机 打印整体或部分
    web react面试题
    vue 面试题
    vue 父页面中含子页面滑动,滑动结束,底部组件进行滑动
  • 原文地址:https://www.cnblogs.com/houchen/p/13342062.html
Copyright © 2011-2022 走看看