看到了当当开源的sharding-jdbc组件,它可以在几乎不修改代码的情况下完成分库分表的实现。摘抄其中一段介绍:
sharding-jdbc直接封装jdbc api,可以理解为增强版的jdbc驱动,旧代码迁移成本几乎为零:
先做一个最简单的试用,不做分库,仅做分表。选择数据表bead_information,首先复制成三个表:bead_information_0、bead_information_1、bead_information_2
测试实现过程
前提:已经实现srping+mybatis对单库单表做增删改查的项目。
1、修改pom.xml增加dependency
<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文件,实现分库分表的配置
<?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)
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、配置完成,可以实现增删改查测试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Springboot项目因为kackson版本问题启动报错解决方案
Java多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask详解
网友评论