shardingsphere-jdbc配置常见问题,Type is required

Type is requied 错误

在这里插入图片描述

  1. shardingsphere不能写成sharing-sphere
  2. 将datasource写成了database
  3. names写成name
  4. driver-class-name不能写成driverClassName
  5. 测试jdbc-url,url开头都支持,url协议错误,jdbc:mysql://localhost:3306漏掉了//
  6. 数据源选中,com.zaxxer.hikari.HikariDataSource数据源下面可以是jdbcUrl或者url
  7. driver-class-name: com.mysql.cj.jdbc.Driver是mysql驱动8.0版本以上带的,如果你选的mysql驱动版本低,那么可是项目无法启动,我遇到的问题是一直提示未设置url
  8. 用到2022.2.1版本idea,idea提示爆红,不影响项目正常运行,配置shardingsphere配置光靠idea提示不行,得多看官方文档
  9. 使用xml写sql语句时,配置了生成主键的算法策略时,就不需要再添加主键列进sql语句,否则会报错Sharding value must implements Comparable

官网:https://shardingsphere.apache.org/index_zh.html
pdf文档地址:https://shardingsphere.apache.org/pdf/shardingsphere_docs_cn.pdf

简单shardingsphere配置介绍

server:
  port: 80
spring:
  shardingsphere:
    datasource:
      names: m1,m2 #逻辑数据源名称
      m1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver #com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db_1?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 1234
      m2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver #com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db_2?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 1234
    rules:
      sharding:
        tables:
          orders: #逻辑表
            actual-data-nodes: m1.orders_$->{0..1} #实际表db_1.orders_0,db_1orders_1
            table-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: orders_inline #数据分片策略
            key-generate-strategy:
              column: id
              key-generator-name: orders_key #主键生成策略,注sql中不能出现主键字段插入
        key-generators:
          orders_key:
            type: SNOWFLAKE
            props:
              algorithm-expression: orders_$->{id % 2}
        sharding-algorithms:
          orders_inline:
            type: INLINE
            props:
              algorithm-expression: orders_$->{id % 2}
    props:
      sql-show: true #展示sql
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.study.demo.pojo
  configuration:
    map-underscore-to-camel-case: true #开启驼峰映射

上面shardingsphere的配置文件作用

  1. 配置了两个数据源,只使用到了一个
  2. 将orders表进行了水平分片处理,一个表根据主键的是奇数还是偶数,放入1表或者0表
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值