• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
org0610
帮助2

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表

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgabeka
系列文章
更多 icon
同类精品
更多 icon
继续加载