My framework springmvc+mybits
What needs to be done:
pom import
<!-- 引入sharding-jdbc核心模块 --> <dependency> <groupId>io.shardingjdbc</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>io.shardingjdbc</groupId> <artifactId>sharding-jdbc-core-spring-namespace</artifactId> <version>2.0.3</version> </dependency>
spring configuration
<!-- dbcp数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value ="${jdbc.password}"/> <!-- The maximum number of connections used in the connection pool --> < property name ="maxActive" value ="50" /> <!-- Initialize the connection size --> < property name ="initialSize" value ="5" / > <!-- Get the maximum waiting time for connection --> < property name ="maxWait" value ="30000" /> <!-- Maximum idle connection pool --> < property name ="maxPoolPreparedStatementPerConnectionSize" value ="20" /><!-- Connection pool minimum idle --> < property name ="minIdle" value ="3" /> <!-- Automatically remove useless connections --> < property name ="removeAbandoned" value ="true" /> <!-- Wait time to clear useless connections-- > < property name ="removeAbandonedTimeout" value ="180" /> <!-- Connection Properties --> < property name ="connectionProperties" value ="clientEncoding=UTF-8" /> <property name="filters" value="stat"/> <!--<property name="proxyFilters">--> <!--<list>--> <!--<ref bean="stat-filter"/>--> <!--<ref bean="log-filter"/>--> <!--</list>--> <!--</property>--> </bean>
<!--分表策略,sharding-column这里根据order_id(这列属性不能是String,只能是整型)分表,OrderSingleKeyTableShardingAlgorithm是分表算法--> <sharding:standard-strategy id="tableShardingStrategy" sharding-column="order_id" precise-algorithm-class="com.os.buqu.algorithm.OrderSingleKeyTableShardingAlgorithm"/>
<sharding:data-source id="shardingDataSource"> <!--因为我只有一张表。所以就只需要配置一个dataSource--> <sharding:sharding-rule data-source-names="dataSource" default-data-source-name="dataSource"> <sharding:table-rules> <!--logic-tables逻辑表名--> <!--actual-data-nodes真实表名--> <sharding:table-rule logic-table="demo_order" actual-data-nodes="dataSource.demo_order_0,dataSource.demo_order_1" table-strategy-ref="tableShardingStrategy"/> </sharding:table-rules> <sharding:binding-table-rules> <!--logic-tables逻辑表名--> <sharding:binding-table-rule logic-tables="demo_order"/> </sharding:binding-table-rules> </sharding:sharding-rule> </sharding:data-source>
<!-- mybatis的配置文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="shardingDataSource"/> <property name="configLocation" value="classpath:config/mybatis-config.xml"/> <property name="mapperLocations" value="classpath*:mapper/**/*Mapper.xml"/> </bean> <!-- spring与mybatis整合配置,扫描所有dao和所有mapper文件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.os.buqu.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="shardingDataSource"/> </bean>
分表策略类
import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue; import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm; import java.util.Collection;
public final class OrderSingleKeyTableShardingAlgorithm implements PreciseShardingAlgorithm<Integer> { public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) { for (String each : availableTargetNames) { //根据订单号的尾数分表。0-9 String value=shardingValue.getValue().toString(); if (each.endsWith(value.substring(value.length()-1,value.length())+"")) { return each; } } throw new UnsupportedOperationException(); } }