看到了當當開源的sharding-jdbc組件,它可以在幾乎不修改代碼的情況下完成分庫分表的實現。摘抄其中一段介紹:
sharding-jdbc直接封裝jdbc api,可以理解為增強版的jdbc驅動,舊代碼遷移成本幾乎為零:
- 可適用于任何基于java的orm框架,如:jpa, hibernate, mybatis, spring jdbc template或直接使用jdbc。
- 可基于任何第三方的數據庫連接池,如:dbcp, c3p0, bonecp, druid等。
- 理論上可支持任意實現jdbc規范的數據庫。雖然目前僅支持mysql,但已有支持oracle,sqlserver,db2等數據庫的計劃。
先做一個最簡單的試用,不做分庫,僅做分表。選擇數據表bead_information,首先復制成三個表:bead_information_0、bead_information_1、bead_information_2
測試實現過程
前提:已經實現srping+mybatis對單庫單表做增刪改查的項目。
1、修改pom.xml增加dependency
1
2
3
4
5
6
7
8
9
10
|
<dependency> <groupid>com.dangdang</groupid> <artifactid>sharding-jdbc-core</artifactid> <version> 1.4 . 2 </version> </dependency> <dependency> <groupid>com.dangdang</groupid> <artifactid>sharding-jdbc-config-spring</artifactid> <version> 1.4 . 0 </version> </dependency> |
2、新建一個sharding-jdbc.xml文件,實現分庫分表的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:context= "http://www.springframework.org/schema/context" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:rdb= "http://www.dangdang.com/schema/ddframe/rdb" xsi:schemalocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context.xsd http: //www.dangdang.com/schema/ddframe/rdb http: //www.dangdang.com/schema/ddframe/rdb/rdb.xsd"> <!-- 配置數據源 --> <bean name= "datasource" class = "com.alibaba.druid.pool.druiddatasource" init-method= "init" destroy-method= "close" > <property name= "url" value= "jdbc:mysql://localhost:3306/beadhouse" /> <property name= "username" value= "root" /> <property name= "password" value= "123456" /> </bean> <rdb:strategy id= "tableshardingstrategy" sharding-columns= "id" algorithm- class = "com.springdemo.utill.membersinglekeytableshardingalgorithm" /> <rdb:data-source id= "shardingdatasource" > <rdb:sharding-rule data-sources= "datasource" > <rdb:table-rules> <rdb:table-rule logic-table= "bead_information" actual-tables= "bead_information_${0..2}" table-strategy= "tableshardingstrategy" /> </rdb:table-rules> </rdb:sharding-rule> </rdb:data-source> <bean id= "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" > <property name= "datasource" ref= "shardingdatasource" /> </bean> </beans> |
3、將文件引入spring配置文件中。
需要修改幾個地方,把sqlsessionfactory和transactionmanager原來關聯的datasource統一修改為shardingdatasource(這一步作用就是把數據源全部托管給sharding去管理)
4、實現分表(分庫)邏輯,我們的分表邏輯類需要實現singlekeytableshardingalgorithm接口的三個方法dobetweensharding、doequalsharding、doinsharding
(取模除數需要按照自己需求改變,我這里分3個表,所以除以3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import java.util.collection; import java.util.linkedhashset; import com.dangdang.ddframe.rdb.sharding.api.shardingvalue; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.singlekeytableshardingalgorithm; import com.google.common.collect.range; public class membersinglekeytableshardingalgorithm implements singlekeytableshardingalgorithm<integer> { @override public collection<string> dobetweensharding(collection<string> tablenames, shardingvalue<integer> shardingvalue) { collection<string> result = new linkedhashset<string>(tablenames.size()); range<integer> range = (range<integer>) shardingvalue.getvaluerange(); for (integer i = range.lowerendpoint(); i <= range.upperendpoint(); i++) { integer modvalue = i % 3 ; string modstr = modvalue < 3 ? "" + modvalue : modvalue.tostring(); for (string each : tablenames) { if (each.endswith(modstr)) { result.add(each); } } } return result; } @override public string doequalsharding(collection<string> tablenames, shardingvalue<integer> shardingvalue) { integer modvalue = shardingvalue.getvalue() % 3 ; string modstr = modvalue < 3 ? "" + modvalue : modvalue.tostring(); for (string each : tablenames) { if (each.endswith(modstr)) { return each; } } throw new illegalargumentexception(); } @override public collection<string> doinsharding(collection<string> tablenames, shardingvalue<integer> shardingvalue) { collection<string> result = new linkedhashset<string>(tablenames.size()); for (integer value : shardingvalue.getvalues()) { integer modvalue = value % 3 ; string modstr = modvalue < 3 ? "" + modvalue : modvalue.tostring(); for (string tablename : tablenames) { if (tablename.endswith(modstr)) { result.add(tablename); } } } return result; } } |
5、配置完成,可以實現增刪改查測試。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/huangheng01/p/9366325.html