集成shardsphere报错Type is required.

Type is required完整错误信息如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘orderController’: Unsatisfied dependency expressed through field ‘orderService’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘orderService’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘contractOrderMapper’ defined in file [/Users/admin/Downloads/mexc/mxc-contract-order/order-rest/target/classes/com/mxc/contract/order/mapper/ContractOrderMapper.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.lang.IllegalArgumentException: Type is required.

  • 针对这个错误,我一直以为是类型错误,其实要首先检查一下是不是数据库名称配置错误。我的错误就是因为数据库名称写错导致的。
  • 如果不是数据库名称错误且数据源type用的是durid可以尝试换一下HikariDataSource,一般情况下跟这个关系不大。

集成shardingsphere用到的配置

spring.shardingsphere.datasource.names=ds0

spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:localhost:3306/mxc-contract?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.ds0.username=testUser
spring.shardingsphere.datasource.ds0.password=123456

spring.shardingsphere.sharding.tables.product_order.key-generator.props.worker.id=1

spring.shardingsphere.sharding.tables.product_order.key-generator.column=id
spring.shardingsphere.sharding.tables.product_order.key-generator.type=SNOWFLAKE
  • 上边的配置集成了雪花算法,可以拿来即用的。

关于版本号,我的引用如下:

<!-- mybatis-plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.0</version>
            </dependency>
            <!-- druid -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.6</version>
            </dependency>
            <!-- 整合redis -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <version>1.4.7.RELEASE</version>
            </dependency>
            <!--Kafka-->
            <dependency>
                <groupId>org.springframework.kafka</groupId>
                <artifactId>spring-kafka</artifactId>
                <version>2.8.7</version>
            </dependency>

            <dependency>
                <groupId>org.apache.shardingsphere</groupId>
                <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
                <version>4.1.1</version>
            </dependency>

猜你喜欢

转载自blog.csdn.net/u011277745/article/details/125956836