SpringBoot+MybatisPlus实现sharding-jdbc分库分表的示例代码
介绍
学习sharding-jdbc的分库分表功能,以分库,分表,分库分表三种方式来实现,此文章只展示分库分表,为简洁其余两种分篇写
1.环境搭建
代码结构
数据库搭建(2个mysql数据库),并在2个库下都添加多个相同结构的表
pom.xml依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.0-RC1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> </dependencies>
application.yml (配置文件分离,结构更清晰)
spring: main: allow-bean-definition-overriding: true # 需要配置否则加载数据源报错 是否允许定义重名的bean对象覆盖原有的bean profiles: active: databasetable mybatis-plus: mapperLocations: classpath*:mapper/*Mapper.xml
application-databasetable.yml
# 数据源命名 spring.shardingsphere.datasource.names=book2021,book2022 # datasource1 spring.shardingsphere.datasource.book2021.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.book2021.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.book2021.url=jdbc:mysql://192.168.31.212:3306/book2021?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true spring.shardingsphere.datasource.book2021.username=root spring.shardingsphere.datasource.book2021.password=123 # datasource2 spring.shardingsphere.datasource.book2022.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.book2022.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.book2022.url=jdbc:mysql://192.168.31.212:3307/book2022?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true spring.shardingsphere.datasource.book2022.username=root spring.shardingsphere.datasource.book2022.password=123 #库名.表名 spring.shardingsphere.sharding.tables.book.actual-data-nodes=book202${1..2}.book_info_202${1..2}0${1..9},book202${1..2}.book_info_202${1..2}1${0..2} # 数据库分片字段 spring.shardingsphere.sharding.tables.book.database-strategy.standard.sharding-column=create_time # 数据库分片策略 spring.shardingsphere.sharding.tables.book.database-strategy.standard.preciseAlgorithmClassName=cn.xwl.xshardingjdbc.config.database.PreciseDatabaseShardingAlgorithm spring.shardingsphere.sharding.tables.book.database-strategy.standard.rangeAlgorithmClassName=cn.xwl.xshardingjdbc.config.database.RangeDatabaseShardingAlgorithm # 数据表分片字段 spring.shardingsphere.sharding.tables.book.table-strategy.standard.sharding-column=create_time # 数据表分表策略 spring.shardingsphere.sharding.tables.book.table-strategy.standard.preciseAlgorithmClassName=cn.xwl.xshardingjdbc.config.table.PreciseTableShardingAlgorithm spring.shardingsphere.sharding.tables.book.table-strategy.standard.rangeAlgorithmClassName=cn.xwl.xshardingjdbc.config.table.RangeTableShardingAlgorithm # 打印sql spring.shardingsphere.props.sql.show=true
2.代码编写
同分库,分表两篇文中的一致,这里不粘贴了,项目地址 本测试Demo的Gitee地址
到此这篇关于SpringBoot+MybatisPlus实现sharding-jdbc分库分表的示例代码的文章就介绍到这了,更多相关SpringBoot MybatisPlus sharding-jdbc分库分表内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
MyEclipse2018中安装Mybatis generator插件的实现步骤
这篇文章主要介绍了MyEclipse2018中安装Mybatis generator插件的实现步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-02-02Spring Data JPA实现持久化存储数据到数据库的示例代码
Spring Data JPA是Spring基于JPA规范的基础上封装的⼀套 JPA 应⽤框架,可使开发者⽤极简的代码即可实现对数据库的访问和操作。本文我们来了解如何用Spring Data JPA框架实现数据持久化存储到数据库,感兴趣的可以了解一下2022-04-04Spring BeanFactory和FactoryBean有哪些区别
这篇文章主要介绍了Spring BeanFactory 与 FactoryBean 的区别详情,BeanFactory 和 FactoryBean 的区别却是一个很重要的知识点,在本文中将结合源码进行分析讲解,需要的小伙伴可以参考一下2023-02-02spring aop之@AfterReturning不生效问题及解决
这篇文章主要介绍了spring aop之@AfterReturning不生效问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-05-05
最新评论