springboot 集成sharding-jdbc

pom.xml

springboot 2.6.3的版本

XML 复制代码
      <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
            <version>5.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.yaml</groupId>
                    <artifactId>snakeyaml</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.33</version>
        </dependency>

application.yml

XML 复制代码
server:
  port: 8080
spring:
  shardingsphere:
    mode:
      type: Standalone
      repository:
        type: JDBC
    props:
      # 禁用执行SQL用于获取表元数据
      sql-show: true
      # 禁用执行SQL用于获取数据库元数据
    #      check-table-metadata-enabled: false
    datasource:
      # 配置真实数据源 相当于ds0,ds1
      names: ds0
      ds0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3306/test
        username: root
        password: root

    rules:
      sharding:
        tables:
          #这里以student表为例
          user_info:
            # 表名的分片规则: # 由数据源名 + 表名组成(参考 Inline 语法规则)
            actual-data-nodes: ds0.user_info_$->{1..2}
            # 分布式序列策略
            key-generate-strategy:
              # 自增列名称,缺省表示不使用自增主键生成器
              column: id
              # 分布式序列算法名称
              key-generator-name: snowflake
            # 配置分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一:standard/complex/hint/none
#            database-strategy:
#              # 用于单分片键的标准分片场景
#              standard:
#                # 分片列名称 这里指定age作为分库键
#                sharding-column: age
#                # 分片算法名称
#                sharding-algorithm-name: student_age_inline
            # 配置分表策略,分库键class_id,分库策略student_class_id_inline
            table-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: user_inline
        # 配置分片算法
        sharding-algorithms:
          # 分库策略:根据age取余2
#          student_age_inline:
#            type: INLINE
#            props:
#              algorithm-expression: ds$->{age % 2}
          # 分表策略:根据classid取余2
          user_inline:
            type: INLINE
            props:
              algorithm-expression: user_info_$->{id % 2 +1}
        key-generators:
          # 配置主键生成算法-雪花算法
          snowflake:
            type: SNOWFLAKE





mybatis-plus:
   mapper-locations: classpath:mapper/*.xml
   configuration:
     map-underscore-to-camel-case: true
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

接口案例

java 复制代码
/**
	springboot  2.6.3 集成   sharding-jdbc 5.2.1 
  springboot web  依赖排除 snakeyaml 原来版本 升级新版本 否则解析有问题
  使用和平时相同
*/  
@RequestMapping(value = "/test",method = {RequestMethod.GET})
    @ResponseBody
    public ResponseEntity<String> index2() {
        Snowflake snowflake = IdUtil.getSnowflake();
        User user = new User();
        user.setId(snowflake.nextId());
        user.setName("test"+user.getId());
        user.setSex("male");
        user.setTsDate(new Date());
        userMapper.insert(user);

        return ResponseEntity.ok("success");
    }
相关推荐
丙氨酸長鏈26 分钟前
[Bukkit插件开发]手持发射器箭矢机枪 教学文档 面向Python/C#开发者入门Java与Bukkit API
java·python·c#
Nontee28 分钟前
设计模式:模板方法与策略,从“每个字都认识“到能说清它们在干嘛
java·数据库·设计模式
我是唐青枫30 分钟前
Java ReentrantLock 实战详解:比 synchronized 更灵活的可重入锁
java·开发语言
晨曦中的暮雨1 小时前
Virtual Thread 优化 Spring AI 阻塞式 LLM I/O:对照压测报告
java·并发·个人项目
万少2 小时前
DeepSeek-V4-Flash 正式版上线了,但这 3 个坑我帮你提前踩了
前端·javascript·后端
明月_清风2 小时前
🚀 Palantir Foundry 本体论实战:当 Ontology 从"知识图谱"进化为"企业操作系统"
前端·后端
明月_清风2 小时前
从概念到代码:用 Ontology 构建你的第一个知识图谱
前端·后端
AKA__Zas2 小时前
芝士算法(前缀和2.0)
java·数据结构·算法·leetcode·哈希算法·学习方法
caishenzhibiao2 小时前
降雨带波段点差 同花顺期货通指标
java·c语言·c#
Python私教2 小时前
Django 6.1 邮件配置大改:旧项目如何平稳升级?
后端·python·django