Type is requied 错误
- shardingsphere不能写成sharing-sphere
- 将datasource写成了database
- names写成name
- driver-class-name不能写成driverClassName
- 测试jdbc-url,url开头都支持,url协议错误,jdbc:mysql://localhost:3306漏掉了//
- 数据源选中,com.zaxxer.hikari.HikariDataSource数据源下面可以是jdbcUrl或者url
- driver-class-name: com.mysql.cj.jdbc.Driver是mysql驱动8.0版本以上带的,如果你选的mysql驱动版本低,那么可是项目无法启动,我遇到的问题是一直提示未设置url
- 用到2022.2.1版本idea,idea提示爆红,不影响项目正常运行,配置shardingsphere配置光靠idea提示不行,得多看官方文档
- 使用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的配置文件作用
- 配置了两个数据源,只使用到了一个
- 将orders表进行了水平分片处理,一个表根据主键的是奇数还是偶数,放入1表或者0表