springboot+shardingjdbc+mybatis+oracle与mysql坑

2023-11-25 16:30

本文主要是介绍springboot+shardingjdbc+mybatis+oracle与mysql坑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随着公司的业务增长,从一个工厂单表对应的数据量到达为2个亿数据量,现在引入4个工厂数据估计数量到达10个亿数据量,考虑后期数据量导致数据表崩溃。想引入现在比较流行的分库分表shardingjdbc技术,由于只分表不库的功能,按照厂site进行分成四张表。

步骤如下;

第一步 引入包

<!--Oracle驱动 11.2.0.3  --><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.1.0.6.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.23</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version></dependency><!-- sharding-sphere --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.1.1</version></dependency>

第二步 配置yml文件

spring:#配置Sharding-jdbcshardingsphere:datasource:names: ds1,ds2ds1:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: oracle.jdbc.driver.OracleDriverurl: jdbc:oracle:thin:@172.127.17.249:1521:d1rptdb1username: rootpassword: 123456ds2:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: oracle.jdbc.driver.OracleDriverurl: jdbc:oracle:thin:@172.127.17.249:1521:d1rptdb1username: rootpassword: 123456sharding:props:sql.show: truetables:T_ORDER:  #t_user表actual-data-nodes: ds${1..2}.T_ORDER${1..2}    #数据节点,均匀分布table-strategy:  #分表策略inline: #行表达式sharding-column: ORDER_IDalgorithm-expression: T_ORDER$->{ORDER_ID % 2+1}  #按模运算分配# 默认数据源,未分片的表默认执行库default-database-strategy:inline:sharding-column:  ORDER_IDalgorithm-expression: ds$->{ORDER_ID % 2+1}props:sql:show: true

注意点:由于目前业务需求只考虑分表,不考虑分库。我第一反应的只要一个数据源就可以没有必要搞两个或者两个以数据源。但是问题来了:如果只在yml配置一个数据源启动报:

13:43:37.213 [main] INFO ShardingSphere-metadata - Loading 1 logic tables' meta data.
13:43:48.733 [main] INFO ShardingSphere-metadata - Loading 527 tables' meta data.
java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在

而且为什么出现Loading 527 tables' meta data  527表。 反反复复看官文文档:https://shardingsphere.apache.org/document/4.1.1/cn/manual/sharding-jdbc/configuration/config-spring-boot/配置也没有错,由于自己在本地电脑上重新安装mysql数据库,同样的代码

package com.shardingjdb.shardingjdbc.Controller;import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;public class Test {public static void main(String[] args) {Map<String, DataSource> dataSourceMap = new HashMap<>();// 配置第一个数据源DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");//druidDataSource.setUrl("jdbc:mysql://10.108.243.87:3306/order");druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/order?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC");druidDataSource.setUsername("root");druidDataSource.setPassword("123456");dataSourceMap.put("ds0",druidDataSource);//在EDU_LDA库中创建了三张表:T_ORDER,T_ORDER1,T_ORDER2// 配置orders表规则  ds0.t_user${0..1}orders_${1..2}TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order","ds0.t_order${1..2}");// 配置分库+分表策略//orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id","ds${customer_id%2+1}"));orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("ORDER_ID","t_order$->{ORDER_ID % 2+1}"));// 配置分片规则ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();shardingRuleConfiguration.getTableRuleConfigs().add(orderTableRuleConfig);// 获取数据源对象try {DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap,shardingRuleConfiguration,new Properties());Connection connection = dataSource.getConnection();PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order(ORDER_ID, USER_ID, STATUS) values(?,?,?)");for (int i = 420; i <430 ; i++) {preparedStatement.setInt(1,i);preparedStatement.setInt(2,i);preparedStatement.setInt(3,i);preparedStatement.execute();}} catch (Exception exception) {exception.printStackTrace();}}
}

mysql是进行起到分表的作用,我就纳闷啊 为什么mysql一个数据源可以 而oralce一个数据源不可以总是报表或者视图不存在,

启动项目报错如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'artificialNodeHistoryController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'artificialNodeHistoryServiceImpl': Unsatisfied dependency expressed through field 'olFirstruneqflagMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'olFirstruneqflagMapper' defined in file [D:\code\ims-switchline-service\ims-switchline-service-svc\target\classes\com\csot\ims\mapper\OlFirstruneqflagMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDataSource' defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'shardingDataSource' threw exception; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:551) at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java:40002) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:41008) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) at com.csot.ims.Application.main(Application.java:30) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'artificialNodeHistoryServiceImpl': Unsatisfied dependency expressed through field 'olFirstruneqflagMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'olFirstruneqflagMapper' defined in file [D:\code\ims-switchline-service\ims-switchline-service-svc\target\classes\com\csot\ims\mapper\OlFirstruneqflagMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDataSource' defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'shardingDataSource' threw exception; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在

由于我在oracle中搞两个相同的数据源就可以启动项目,我不知道是我自己那里配置问题还是shardingjdbc支持oracle一个bug呢,总之解决自己困惑好几天的问题今天终于解决还是挺开心的。 本来想不能用shardingjdb分表,想用oracel的分区表来实现,但是领导对应这个做法不太满意,怕10个亿扛不住,由于我又不断的在测试环境测压数据量是否能扛住10亿的数据。

总结上面问题:如果你也出现上面问题是否考虑配置两个数据源,虽然只用到一个数据源那就配置两个相同的数据源。

第三步 创建数据库表:

 CREATE TABLE "EDU_LDA"."T_ORDER" (	"ORDER_ID" NUMBER(*,0) NOT NULL ENABLE, "USER_ID" NUMBER(*,0) NOT NULL ENABLE, "STATUS" NUMBER(*,0), PRIMARY KEY ("ORDER_ID")USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)TABLESPACE "BD_LDA_DAT"  ENABLE) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGINGSTORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)TABLESPACE "BD_LDA_DAT" 

分别创建三个表:T_ORDER 、T_ORDER​​​​​​​1、T_ORDER2

 第四步:写sql 插入语句

  <!-- 保存order信息--><insert id="insertOrder" parameterType="com.csot.ims.entity.Order">INSERT INTO T_ORDER (ORDER_ID, USER_ID, STATUS)VALUES(#{orderId}, #{userId},#{status})</insert>

代码:
 

  @ApiOperation("testshardingjdbc")@GetMapping("/testshardingjdbc")public RestResponse testshardingjdbc(@Valid @NotBlank @ApiParam("InstanceNo") String instanceNo) {try {Order order = new Order();order.setStatus(6);order.setUserId(6);order.setOrderId(24);orderService.insertOrder(order);//orderService.saveFactoryList();return RestResponse.ok("");} catch (Exception e) {log.error("=="+e);return RestResponse.failed(500, "testshardingjdbc信息发生异常" + e.getMessage());}}

调用接口后

 赶紧试试吧 祝你也成功!

 

这篇关于springboot+shardingjdbc+mybatis+oracle与mysql坑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/424131

相关文章

Spring Boot 2.7.8 集成 Thymeleaf的最佳实践与常见问题讨论

《SpringBoot2.7.8集成Thymeleaf的最佳实践与常见问题讨论》本文详细介绍了如何将SpringBoot2.7.8与Thymeleaf集成,从项目依赖到配置文件设置,再到控制器... 目录前言一、如何构建SpringBoot应用1、项目依赖 (pom.XML)2、控制器类3、Thymelea

SpringBoot项目jar依赖问题报错解析

《SpringBoot项目jar依赖问题报错解析》本文主要介绍了SpringBoot项目中常见的依赖错误类型、报错内容及解决方法,依赖冲突包括类找不到、方法找不到、类型转换异常等,本文给大家介绍的非常... 目录常见依赖错误类型及报错内容1. 依赖冲突类错误(1) ClassNotFoundExceptio

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

SQL Server 中的表进行行转列场景示例

《SQLServer中的表进行行转列场景示例》本文详细介绍了SQLServer行转列(Pivot)的三种常用写法,包括固定列名、条件聚合和动态列名,文章还提供了实际示例、动态列数处理、性能优化建议... 目录一、常见场景示例二、写法 1:PIVOT(固定列名)三、写法 2:条件聚合(CASE WHEN)四、

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1