ShardingSphere:SpringBoot+MyBatis+ShardingSphere实现分表分库

SpringBoot+MyBatis+ShardingSphere实现分表分库

ERROR 23316 --- [reate-872736196] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://localhost:3306/testdb0?characterEncoding=utf-8, errorCode 0, state 01S00

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

报错原因:原因是为驱动包里面的时区和数据的时区不一致的引起的,SpringBoot整合MySQL的依赖包版本过高,在高版本的MySQL依赖中数据库和系统时区差异所造成的。系统为SQL默认美国时间,而我们中国要比他们迟8小时,因此将时区设置为当前系统时区即可,采用+8:00格式

解决办法:在数据库连接语句中加入相应信息&serverTimezone=GMT

url=jdbc:mysql://localhost:3306/testdb0?characterEncoding=utf8&serverTimezone=GMT

2023-07-29 10:02:18.732 ERROR 21984 --- [reate-925976643] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://localhost:3306/testdb0?characterEncoding=utf-8?serverTimezone=UTC, errorCode 0, state S1009

java.sql.SQLException: Unsupported character encoding 'utf-8?serverTimezone=UTC'

报错原因:由“character encoding 'utf-8”可分析应该是连接配置上出了问题

解决办法:1、把原来的“ ? ”改为“  & ”,2、把原来的“ utf-8 ” 改为 “ utf8 ”

url=jdbc:mysql://localhost:3306/testdb0?characterEncoding=utf8&serverTimezone=GMT

2023-07-29 10:18:02.201 ERROR 15004 --- [reate-355977028] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://localhost:3306/testdb1?characterEncoding=utf8&serverTimezone=GMT, errorCode 1049, state 42000

java.sql.SQLSyntaxErrorException: Unknown database 'testdb1'

报错原因:针对报错中提示的com.alibaba的jar包推测,如果是jar包有问题

解决办法:com.alibaba的jar包升级为1.2.6版本

2023-07-29 22:10:34.932 ERROR 17032 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration': Initialization of bean failed; nested exception is org.apache.shardingsphere.underlying.common.exception.ShardingSphereException: Can't find datasource type!

报错原因:没有引入数据库连接池组件com.alibaba

2023-07-29 22:37:05.074 ERROR 3524 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.FileNotFoundException: class path resource [mybatis-config.xml] cannot be opened because it does not exist

报错原因:mybatis-config.xml配置文件不存在

2023-07-29 23:36:55.089 ERROR 7152 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not specified with together

报错原因:在配置文件中指定了:config-location,然后我们又配置了关于configuration的信息,导致的报错。大体意思就是,二者只能选择一种,要么指定config-location我们自己的xml配置文件,要么我们就使用Mybatis的start提供的配置。
如果两者都存在,那么他就会包冲突。

解决方案:把config-location删掉   

ERROR 3420 --- [nio-8084-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

Cannot find local variable 'hashMapMenu'

[org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist

springcloud使用hystrix断路器错误
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.carfi.springcustormerservice.SpringcustormerserviceApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
原因: spring2.x.x>=2.0.0已经去掉了org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class,如果导入的Jar包需要依赖这个就会报错

项目依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cloud.demo.user</groupId>
    <artifactId>springcloud-user-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-user-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <!-- mybatis-springboot-starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- sharding-jdbc依赖 -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

server.port=8084
spring.application.name=user-demo
#×¢²áÖÐÐÄ·ÃÎʺͶ˿Ú
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
#×¢²áµ½consulµÄ·þÎñÃû³Æ
spring.cloud.consul.discovery.serviceName=${spring.application.name}

#mybatisµÄÏà¹ØÅäÖÃ
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*.xml

###############################ShardingSphereʵÏÖ·Ö¿â·Ö±í###############################
#Ö¸¶¨mybatisÐÅÏ¢
#mybatis.config-location=classpath:mybatis-config.xml
#´òÓ¡sql
spring.shardingsphere.props.sql.show=true
#Êý¾ÝÔ´ÅäÖÃ
spring.shardingsphere.datasource.names=testdb0,testdb1
#Êý¾ÝÔ´testdb0ÅäÖÃ
spring.shardingsphere.datasource.testdb0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.testdb0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.testdb0.url=jdbc:mysql://localhost:3306/testdb0?characterEncoding=utf8&serverTimezone=GMT
spring.shardingsphere.datasource.testdb0.username=root
spring.shardingsphere.datasource.testdb0.password=root
#Êý¾ÝÔ´testdb1ÅäÖÃ
spring.shardingsphere.datasource.testdb1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.testdb1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.testdb1.url=jdbc:mysql://localhost:3306/testbd1?characterEncoding=utf8&serverTimezone=GMT
spring.shardingsphere.datasource.testdb1.username=root
spring.shardingsphere.datasource.testdb1.password=root
#·Ö¿â²ßÂÔ
#¸ù¾ÝÄêÁä·Ö¿â
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
#配置分库策略,主键取模0在0库,1在1库
#分片算法行表达式
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=testdb$->{age % 2}
#·Ö±í²ßÂÔ
#¸ù¾Ýid·Ö±í
#配置分表策略,主键取模,0在0表,1在1表
spring.shardingsphere.sharding.tables.user.actual-data-nodes=testdb$->{0..1}.user$->{0..1}
#分片列名称
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
#分片算法行表达式
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user$->{id % 2}
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
###############################ShardingSphereʵÏÖ·Ö¿â·Ö±í###############################

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以为你提供一些关于使用SpringBoot ShardingJDBC MyBatis进行分库的建议,例如:1. 使用ShardingSphere框架定义数据库分片策略;2. 使用MyBatis实现MyBatis SQL脚本;3. 使用SpringBoot注解的方式定义数据源和ShardingJDBC的配置。 ### 回答2: 使用SpringBoot ShardingJDBC和MyBatis可以很方便地实现分库功能。首先,ShardingJDBC是一个轻量级的数据库中间件,可以将数据分散到不同的数据库实例中,从而实现分库的效果。其次,MyBatis是一个流行的持久层框架,可以通过XML或注解的方式与数据库进行交互。 在使用SpringBoot ShardingJDBC和MyBatis分库时,首先需要配置ShardingJDBC的数据源和分片规则。可以通过编写一个配置类来配置分库的规则,例如可以根据某个字段的取值来确定数据应该分散到哪个库或中。配置完成后,就可以在MyBatis的Mapper接口中直接使用分库的数据源,从而实现对不同数据库或的访问。 在编写Mapper接口时,可以使用MyBatis提供的注解或XML方式来编写SQL语句。在SQL语句中,可以使用ShardingJDBC提供的分片键来实现对特定库或的访问。例如,在需要查询特定的数据时,可以使用ShardingJDBC提供的Hint注解将查询操作路由到相应的上。 总的来说,使用SpringBoot ShardingJDBC和MyBatis可以实现简单、高效的分库功能。通过配置ShardingJDBC的分片规则和使用MyBatis编写SQL语句,可以将数据分散到不同的数据库实例和中,从而实现了水平扩展和负载均衡的效果。这种方式能够帮助我们提高数据库的性能和容量,从而更好地应对大规模的数据存储需求。 ### 回答3: 使用SpringBoot ShardingJDBC MyBatis可以轻松实现分库。 首先,ShardingJDBC是一个分库的开源框架,它可以通过数据库中间件实现数据的分散存储。而SpringBoot是一个快速构建项目的框架,可以帮助开发者轻松集成各种组件。 使用SpringBoot ShardingJDBC MyBatis进行分库,首先需要配置ShardingJDBC的数据源、分片策略以及分策略。可以通过配置文件或者编程方式来完成配置。配置数据源时,可以指定多个数据库的连接信息,并使用分片策略将数据分配到不同的数据库中。配置分策略时,可以指定不同的分规则,将数据根据一定的规则分散存储在不同的中。 在具体的业务逻辑中,可以使用MyBatis来操作数据库。MyBatis是一个简化数据库访问的持久层框架,通过编写SQL语句和映射文件,可以轻松实现数据库的增删改查操作。 在访问数据库时,ShardingJDBC会根据配置的分片策略和分策略,自动将数据路由到指定的数据库和中。开发者不需要关心数据的具体存储位置,只需要使用MyBatis的API进行数据操作即可。 使用SpringBoot ShardingJDBC MyBatis进行分库,可以提高数据库的读写性能,增加数据的存储容量,并且可以实现数据的动态扩容和迁移。此外,由于SpringBootMyBatis的高度集成,开发者可以更加方便地进行开发和维护。 总之,使用SpringBoot ShardingJDBC MyBatis进行分库可以帮助开发者更好地管理数据,提升系统的性能和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值