zoukankan      html  css  js  c++  java
  • Spring Boot 整合 SardingSphere (多表(分))

    废话不多说,差别在配置文件上

    文章源码:https://gitee.com/yihong-sword/learn_shardingsphere

    Tips:

    1. 只是将原本s_user的分表配置给copy将表名替换
    2. 其他源码,就不一一copy到这篇文章,参考:https://www.cnblogs.com/yi1036943655/p/15700170.html

    application.properties

    # 应用名称
    spring.application.name=learn_shardingsphere
    # 应用服务 WEB 访问端口
    server.port=8085
    
    # 数据源 sharding01 sharding02
    spring.shardingsphere.datasource.names=sharding0,sharding1
    
    # 第一个数据库
    spring.shardingsphere.datasource.sharding0.type=com.zaxxer.hikari.HikariDataSource
    spring.shardingsphere.datasource.sharding0.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.shardingsphere.datasource.sharding0.jdbc-url=jdbc:mysql://localhost:3306/sharding0?serverTimezone=GMT%2B8&useSSL=false
    spring.shardingsphere.datasource.sharding0.username=root
    spring.shardingsphere.datasource.sharding0.password=root
    
    # 第二个数据库
    spring.shardingsphere.datasource.sharding1.type=com.zaxxer.hikari.HikariDataSource
    spring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.shardingsphere.datasource.sharding1.jdbc-url=jdbc:mysql://localhost:3306/sharding1?serverTimezone=GMT%2B8&useSSL=false
    spring.shardingsphere.datasource.sharding1.username=root
    spring.shardingsphere.datasource.sharding1.password=root
    
    spring.shardingsphere.
    
    # 水平拆分的数据库(表) 配置分库 + 分表策略 行表达式分片策略
    # 分库策略
    spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id
    spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=sharding$->{id % 2}
    
    # 分表策略 其中 s_user 为逻辑表 分表主要取决于s_user.id的行
    spring.shardingsphere.sharding.tables.s_user.actual-data-nodes=sharding$->{0..1}.s_user$->{0..1}
    spring.shardingsphere.sharding.tables.s_user.table-strategy.inline.sharding-column=age
    spring.shardingsphere.sharding.tables.s_user.table-strategy.inline.algorithm-expression=s_user$->{Integer.valueOf(age) % 2}
    
    # 分表策略 其中 s_student 为逻辑表 分表主要取决于s_student.id的行
    spring.shardingsphere.sharding.tables.s_student.actual-data-nodes=sharding$->{0..1}.s_student$->{0..1}
    spring.shardingsphere.sharding.tables.s_student.table-strategy.inline.sharding-column=age
    spring.shardingsphere.sharding.tables.s_student.table-strategy.inline.algorithm-expression=s_student$->{Integer.valueOf(age) % 2}
    
    # 主键生成策略
    # spring.shardingsphere.sharding.tables.s_user.key-generator.column=id
    # spring.shardingsphere.sharding.tables.s_user.key-generator.type=SNOWFLAKE
    
    # 打印执行的数据库以及语句
    spring.shardingsphere.props.sql.show=true
    spring.main.allow-bean-definition-overriding=true
    logging.level.com.sharding.demo.mapper=DEBUG
    
    # mybatis
    mybatis.mapper-locations=classpath:/mappers/*.xml
  • 相关阅读:
    Argparse4j
    Socket通信中的 BeginReceive与EndReceive
    socket 异步通信的一些问题
    static 还是readonly 还是static readonly
    隐藏Android下的虚拟按键
    IIS上架设https网站证书处理备忘
    [转] 移动平台Html5的viewport使用经验
    Air File.load加载问题
    x64位windows 2003 server中“Server 对象 错误 'ASP 0177 : 800700c1' Server.CreateObject 失败”问题
    报 "错误: 无法取消引用int" 的问题解决纪录
  • 原文地址:https://www.cnblogs.com/yi1036943655/p/15733915.html
Copyright © 2011-2022 走看看