视频地址
官网

SpringBoot的pom.xml

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId>
</dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.9</version>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc12</artifactId><version>12.0.1.2</version>
</dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency><!--注意:必须要为以下版本,不然会有问题!!-->
<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.0.0-RC1</version>
</dependency>

注意:其中shardingsphere的版本一定要为4.0.0-RC1,不然就无法使用!!!

SpringBoot中的配置文件

# 设置数据源的名称
spring.shardingsphere.datasource.names=ds0spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/xx?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds0.username=xxxx
spring.shardingsphere.datasource.ds0.password=xxxx# 指定配置表在哪个数据库,表名都是什么
spring.shardingsphere.sharding.tables.t_gggl_log.actual-data-nodes=ds0.t_gggl_log$->{0..1}# 指定配置表的主键是什么,以及生成的策略(SNOWFLAKE指使用雪花算法)
spring.shardingsphere.sharding.tables.t_gggl_log.key-generator.column=id
spring.shardingsphere.sharding.tables.t_gggl_log.key-generator.type=SNOWFLAKE# 指定分片策略 奇数数在t_gggl_log0中,偶数在t_gggl_log1中
spring.shardingsphere.sharding.tables.t_gggl_log.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.t_gggl_log.table-strategy.inline.algorithm-expression=t_gggl_log$->{id % 2}# 设置打印sql
spring.shardingsphere.props.sql.show=truespring.main.allow-bean-definition-overriding=true

遇到的问题:

1.遇到datasource冲突

问题原因

因为DruidDataSourceAutoConfigure创建了一个dataSource。SpringBootConfiguration默认也要创建一个dataSource,所以就冲突了。

***************************
APPLICATION FAILED TO START
***************************Description:The bean 'dataSource', defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class] and overriding is disabled.Action:Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

解决方案

在springboot的配置文件中加上以下配置

spring.main.allow-bean-definition-overriding=true

2.id长度不够

执行插入的时候,插入主键使用雪花算法生成,所有普通的int无法满足长度要求,需要做以下修改:
首先,数据库中的字段要修改字段长度

修改po类中id的类型为Long型

package com.shardingjdbcdemo.model;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName(value = "T_GGGL_LOG")
public class Log {private Long id;private String mc;
}

3.启动加载整个库的表元数据,导致启动不起来

只需要设置大于1个数据源,并且没有设置默认的数据源,就不会加载所有的表元数据

spring.shardingsphere.datasource.names=ds0,ds1spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=oracle.jdbc.OracleDriver
spring.shardingsphere.datasource.ds0.url=jdbc:oracle:thin:@xxx.xxx.xx.x:1521:xxxx
spring.shardingsphere.datasource.ds0.username=xxx
spring.shardingsphere.datasource.ds0.password=xxxspring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=oracle.jdbc.OracleDriver
spring.shardingsphere.datasource.ds1.url=jdbc:oracle:thin:@xxx.xxx.xx.x:1521:xxxx
spring.shardingsphere.datasource.ds1.username=xxx
spring.shardingsphere.datasource.ds1.password=xxx
# 其中的ds1是没有使用到的数据源

ShardingJdbc入门相关推荐

  1. 数据量大了一定要分表,分库分表 Sharding-JDBC 入门与项目实战

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源:juejin.im/post/684490418236581 ...

  2. 数据量大了一定要分表,分库分表Sharding-JDBC入门与项目实战

    作者:六点半起床 juejin.im/post/6844904182365814797 最近项目中不少表的数据量越来越大,并且导致了一些数据库的性能问题.因此想借助一些分库分表的中间件,实现自动化分库 ...

  3. Sharding Sphere ~ Sharding-jdbc分库分表、读写分离

    Sharding Sphere 是什么? 1.一套开源的分布式数据库中间件解决方案 2.有三个产品:Sharding-JDBC 和 Sharding-Proxy 3.定位为关系型数据库中间件,合理在分 ...

  4. 95-分库分表技术之ShardingJDBC

    分库分表技术之ShardingJDBC ShardingJDBC: 回顾上一章的分库分表方式: 分库分表的目的就是将我们的单库的数据控制在合理范围内,从而提高数据库的性能 垂直拆分(按照结构分): 垂 ...

  5. Docker 部署SpringBoot项目不香吗?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:流星007 链接:toutiao.com/i68433912 ...

  6. MySQL分库分表和优化

    第九阶段模块三 分库分表技术之MyCat 1.海量存储问题 1.1 背景描述 随着互联网的发展,数据的量级也是成指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经无 ...

  7. 一文快速入门分库分表中间件 Sharding-JDBC (必修课)

    书接上文 <一文快速入门分库分表(必修课)>,这篇拖了好长的时间,本来计划在一周前就该写完的,结果家庭内部突然人事调整,领导层进行权利交接,随之宣布我正式当爹,紧接着家庭地位滑落至第三名, ...

  8. 分库分表介绍和Sharding-JDBC快速入门

    1.分库分表介绍 垂直分表:可以把一个宽表的字段按访问频次.是否是大字段的原则拆分为多个表,这样既能使业务清晰,还能提升部分性能.拆分后,尽量从业务角度避免联查,否则性能方面将得不偿失. 比如我们可以 ...

  9. sharding分表后主键_分库分表【Sharding-JDBC】入门与项目实战

    最近项目中不少表的数据量越来越大,并且导致了一些数据库的性能问题.因此想借助一些分库分表的中间件,实现自动化分库分表实现.调研下来,发现Sharding-JDBC目前成熟度最高并且应用最广的Java分 ...

最新文章

  1. 区块链概况:从数字货币说起
  2. Linux里find和grep命令
  3. 10年Linux老司机吐血整理的命令大全,拿去吧
  4. 我曾七次鄙视自己的灵魂
  5. 又一任务被Transformer攻陷!NVIDIA开源HORST,用Transformer解决早期动作识别和动作预期任务...
  6. 全球仅4人,刚毕业年薪201万元 !华为最高档“天才少年”回应...
  7. 今晚鼓捣的audio.js一些小经验
  8. Content-type是application/json的作用
  9. 网站性能工具-YSlow的23个规则-网站性能优化
  10. 现代控制理论电子版_SANXINB01开发板verilog教程V3电子版
  11. 13行列式02---余子式与代数余子式、行列式按行(列)展开法则、行列式计算、范德蒙行列式
  12. 计算机考试当场出分,基金从业资格考试当场出成绩吗?
  13. Python爬虫入门教程 89-100 定个小目标,先用Python爬个一亿B站用户
  14. HNOI2015 亚瑟王
  15. [Keil][Verilog][微机原理] 流水灯、存储器、外部中断实验_北京邮电大学计算机原理与应用课程实验报告
  16. 自己用的一些觉得不错的软件
  17. 6.S081-7中断(键盘输入+屏幕输出) - Interrupts
  18. cocoscreator修改鼠标图标样式
  19. [深度文]YoloX部署、优化、训练相关
  20. L1-021 重要的话说三遍 (5 分)

热门文章

  1. 造个轮子,动手实现一个复杂场景的表格组件(UniApp)
  2. 接口测试平台代码实现146: 平台主题-夏日清凉5
  3. 搭建exchange邮件服务器一定要ad域么?_域渗透神器-AD Explorer使用指南
  4. 【weblogic】WTC配置(Weblogic Tuxedo Connector)
  5. 随便聊聊,关于大学,未来的规划
  6. 云存储相关技术及术语的探讨
  7. 计算机专业英语辅导教材,计算机专业英语2全套教材.ppt
  8. 传智健康 第12章_PDF报表生成_JasperReports
  9. w7旗舰版的计算机管理,win7旗舰版系统获取administrator权限的方法
  10. Eigen 求解线性方程组