使用sharding-jdbc實(shí)現(xiàn)水平分表的示例代碼
在mysql中新建數(shù)據(jù)庫sharding_db,新增兩張結(jié)構(gòu)一樣的表student_1和student_2。
CREATE TABLE `student_1` ( `ID` bigint(20) NOT NULL , `NAME` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL , `AGE` int(11) NOT NULL , `GENDER` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL , PRIMARY KEY (`ID`) );
此處未指定主鍵自增,因?yàn)閮蓮埍淼膇d不能重復(fù),所以只能從后端傳入id。
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Druid連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.20</version> </dependency> <!-- Mysql驅(qū)動(dòng)依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- MybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!-- Sharding-JDBC --> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.0-RC1</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
編寫配置文件
spring.main.allow-bean-definition-overriding=true # 配置Sharding-JDBC的分片策略 # 配置數(shù)據(jù)源,給數(shù)據(jù)源起名g1,g2...此處可配置多數(shù)據(jù)源 spring.shardingsphere.datasource.names=g1 # 配置數(shù)據(jù)源具體內(nèi)容:連接池,驅(qū)動(dòng),地址,用戶名,密碼 # 由于上面配置數(shù)據(jù)源只有g(shù)1因此下面只配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/sharding_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.g1.username=root spring.shardingsphere.datasource.g1.password=123456 # 配置表的分布,表的策略 spring.shardingsphere.sharding.tables.student.actual-data-nodes=g1.student_$->{1..2} # 指定student表 主鍵gid 生成策略為 SNOWFLAKE spring.shardingsphere.sharding.tables.student.key-generator.column=id spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE # 指定分片策略 約定id值是偶數(shù)添加到student_1表,如果id是奇數(shù)添加到student_2表 spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_$->{id % 2 + 1} # 打開sql輸出日志 spring.shardingsphere.props.sql.show=true
或者是yml格式
spring: main: allow-bean-definition-overriding: true shardingsphere: datasource: g1: driver-class-name: com.mysql.cj.jdbc.Driver password: 123456 type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/sharding_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC username: root names: g1 props: sql: show: true sharding: tables: student: actual-data-nodes: g1.student_$->{1..2} key-generator: column: id type: SNOWFLAKE table-strategy: inline: algorithm-expression: student_$->{id % 2 + 1} sharding-column: id
編寫實(shí)體類
@Data public class Student { private Long id; private String name; private int age; private String gender; }
編寫mapper接口
@Repository public interface StudentMapper extends BaseMapper<Student> { }
編寫測試類
@SpringBootTest class ShardingJdbcDemoApplicationTests { @Autowired private StudentMapper studentMapper; @Test public void test01() { for (int i = 0; i < 10; i++) { Student student = new Student(); student.setName("wuwl"); student.setAge(27); student.setGender("男"); studentMapper.insert(student); } } }
執(zhí)行測試
執(zhí)行成功,主鍵通過雪花算法在后端生成,傳入到數(shù)據(jù)庫中,根據(jù)奇偶性進(jìn)行分表。
student_1表數(shù)據(jù):
student_2表數(shù)據(jù):
兩張表的數(shù)據(jù)分別有5條,但這只是因?yàn)檠┗ㄋ惴ㄉ傻膇d奇數(shù)偶數(shù)各5個(gè),不是1:1的關(guān)系,需要注意。
主鍵生成后,根據(jù)策略插入到對(duì)應(yīng)的表中,從打印出來的sql可以證明這一點(diǎn)。
通過mapper接口的selectById方法進(jìn)行查詢時(shí),會(huì)先根據(jù)主鍵策略判斷在哪個(gè)庫,再直接去那個(gè)庫根據(jù)主鍵查詢。而如果是通過其它條件查詢,或者是多個(gè)id的selectById方法查詢,又是如何的呢?
@Test public void test03() { List<Long> list = new ArrayList<>(); list.add(1362282042768609282l); list.add(1362282040277192705l); List<Student> studentList = studentMapper.selectBatchIds(list); System.out.println(studentList); }
取了兩張表的id進(jìn)行查詢。
執(zhí)行同樣的sql,在兩張表中都查詢一遍,再組合結(jié)果。
如果所有的id,都來自同一張表,那是否會(huì)去多個(gè)表中重復(fù)查詢呢?
只執(zhí)行了一遍。所以,在執(zhí)行查詢時(shí),sharding會(huì)先判斷是否可以確定需要的數(shù)據(jù)來自那張表,如果能,則直接去那一張表中查詢數(shù)據(jù)即可,而如果不能確定,則會(huì)多個(gè)表重復(fù)查詢,以確定查詢結(jié)果的完整性。
到此這篇關(guān)于使用sharding-jdbc實(shí)現(xiàn)水平分表的示例代碼的文章就介紹到這了,更多相關(guān)sharding-jdbc 水平分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- sharding-jdbc5.0.0實(shí)現(xiàn)分表實(shí)踐
- 利用Sharding-Jdbc進(jìn)行分庫分表的操作代碼
- 使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表的示例代碼
- springboot實(shí)現(xiàn)以代碼的方式配置sharding-jdbc水平分表
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)自定義分庫分表的實(shí)踐
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)分庫分表與讀寫分離的示例
- Java使用Sharding-JDBC分庫分表進(jìn)行操作
- Sharding-Jdbc 自定義復(fù)合分片的實(shí)現(xiàn)(分庫分表)
- 利用Sharding-Jdbc組件實(shí)現(xiàn)分表
相關(guān)文章
mybatis升級(jí)mybatis-plus時(shí)踩到的一些坑
這篇文章主要給大家介紹了關(guān)于mybatis升級(jí)mybatis-plus時(shí)踩到的一些坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Java中冒泡排序的原生實(shí)現(xiàn)方法(正序與逆序)
這篇文章主要給大家介紹了關(guān)于Java中冒泡排序的原生實(shí)現(xiàn)方法(正序與逆序)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java實(shí)現(xiàn)byte[]轉(zhuǎn)List的示例代碼
byte,即字節(jié),由8位的二進(jìn)制組成。在Java中,byte類型的數(shù)據(jù)是8位帶符號(hào)的二進(jìn)制數(shù)。List?是一個(gè)接口,它繼承于Collection的接口。它代表著有序的隊(duì)列。本文將介紹如何通過java實(shí)現(xiàn)byte[]轉(zhuǎn)List,需要的可以參考一下2022-01-01詳解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超級(jí)詳細(xì)版)
本篇文章主要介紹了手把手Maven搭建SpringMVC+Spring+MyBatis框架(超級(jí)詳細(xì)版),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12elasticsearch bucket 之rare terms聚合使用詳解
這篇文章主要為大家介紹了elasticsearch bucket 之rare terms聚合使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11java中生成任意之間數(shù)的隨機(jī)數(shù)詳解
這篇文章主要介紹了java中生成任意之間數(shù)的隨機(jī)數(shù)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09mybatis取別名typeAliases標(biāo)簽的位置放錯(cuò)導(dǎo)致報(bào)錯(cuò)的解決
這篇文章主要介紹了mybatis取別名typeAliases標(biāo)簽的位置放錯(cuò)導(dǎo)致報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java實(shí)現(xiàn)emqx設(shè)備上下線監(jiān)聽詳解
這篇文章主要為大家介紹了java實(shí)現(xiàn)emqx設(shè)備上下線監(jiān)聽詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化
這篇文章主要介紹了設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化方法,需要的朋友可以參考下2014-10-10Mybatis-Plus中的selectByMap使用實(shí)例
Mybatis-Plus來對(duì)數(shù)據(jù)庫進(jìn)行增刪改查時(shí),將里面的函數(shù)試了個(gè)遍,接下來我就將使用selectByMap函數(shù)的簡單測試實(shí)例寫出來,方便沒有使用過的朋友們快速上手,感興趣的可以了解一下2021-11-11