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

  • 相关阅读:
    【arc072f】AtCoder Regular Contest 072 F
    maven settings解决下载不了依赖包问题
    git 命令提交本地代码到新创建的仓库
    JAVA 利用切面、注解 动态判断请求信息中字段是否需要为空
    JAVA 根据身份证号码解析出生日期、性别、年龄
    利用JAVA正则快速获取URL的文件名
    datalist
    Mybatis map接收list参数
    bootstrap-table 列宽动态拖拽改变宽度
    JAVA 枚举类遍历与switch使用
  • 原文地址:https://www.cnblogs.com/houchen/p/13342062.html
Copyright © 2011-2022 走看看