[关闭]
@boothsun 2017-11-15T21:00:11.000000Z 字数 3942 阅读 1779

sharding-JDBC 实现分库

中间件


需求

按照业务线Id分库,每个业务线一个库。

sql

  1. -- 建立112业务线库
  2. create database sharding_112 ;
  3. -- 112业务库里建立 t_order
  4. CREATE TABLE `t_order` (
  5. `order_id` int(11) NOT NULL,
  6. `user_id` int(11) NOT NULL,
  7. `business_id` int(4) DEFAULT NULL,
  8. PRIMARY KEY (`order_id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
  10. -- 建立113业务线库
  11. create database sharding_113 ;
  12. -- 113业务库里建立 t_order
  13. CREATE TABLE `t_order` (
  14. `order_id` int(11) NOT NULL,
  15. `user_id` int(11) NOT NULL,
  16. `business_id` int(4) DEFAULT NULL,
  17. PRIMARY KEY (`order_id`)
  18. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

pom.xml(使用sharding-jdbc 2.0)

  1. <!-- https://mvnrepository.com/artifact/io.shardingjdbc/sharding-jdbc-core -->
  2. <dependency>
  3. <groupId>io.shardingjdbc</groupId>
  4. <artifactId>sharding-jdbc-core</artifactId>
  5. <version>2.0.0.M2</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/com.dangdang/sharding-jdbc-config-spring -->
  8. <dependency>
  9. <groupId>com.dangdang</groupId>
  10. <artifactId>sharding-jdbc-config-spring</artifactId>
  11. <version>1.4.0</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>io.shardingjdbc</groupId>
  15. <artifactId>sharding-jdbc-spring-namespace</artifactId>
  16. <version>2.0.0.M2</version>
  17. </dependency>

Spring相关配置

  1. <bean id="ds_112" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
  2. destroy-method="close">
  3. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  4. <property name="url" value="${jdbc.url_112}"></property>
  5. <property name="username" value="${jdbc.username_112}"></property>
  6. <property name="password" value="${jdbc.password_112}"></property>
  7. <property name="maxActive" value="100"/>
  8. <property name="initialSize" value="10"/>
  9. <property name="maxWait" value="60000"/>
  10. <property name="minIdle" value="5"/>
  11. </bean>
  12. <bean id="ds_113" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
  13. destroy-method="close">
  14. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  15. <property name="url" value="${jdbc.url_113}"></property>
  16. <property name="username" value="${jdbc.username_113}"></property>
  17. <property name="password" value="${jdbc.password_113}"></property>
  18. <property name="maxActive" value="100"/>
  19. <property name="initialSize" value="10"/>
  20. <property name="maxWait" value="60000"/>
  21. <property name="minIdle" value="5"/>
  22. </bean>
  23. <sharding:standard-strategy id="databaseShardingStrategy" sharding-column="business_id" precise-algorithm-class="com.boothsun.util.sharding.PreciseModuloDatabaseShardingAlgorithm" />
  24. <sharding:data-source id="shardingDataSource">
  25. <sharding:sharding-rule data-source-names="ds_112,ds_113">
  26. <sharding:table-rules>
  27. <sharding:table-rule logic-table="t_order" database-strategy-ref="databaseShardingStrategy" />
  28. </sharding:table-rules>
  29. </sharding:sharding-rule>
  30. </sharding:data-source>

sharding-jdbc 相关标签含义 参见官方文档:配置手册

官方demo:github

分库规则类

  1. /**
  2. * 精确匹配
  3. */
  4. public final class PreciseModuloDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {
  5. private static final Map<Integer,String> dataSourceMap = new HashMap<>();
  6. static {
  7. dataSourceMap.put(112,"ds_112");
  8. dataSourceMap.put(113,"ds_113");
  9. }
  10. @Override
  11. public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
  12. return dataSourceMap.get(shardingValue.getValue());
  13. }
  14. }

单测类

  1. @Autowired
  2. OrderMapper orderMapper ;
  3. /**
  4. * 测试插入
  5. * @throws Exception
  6. */
  7. @Test
  8. public void insertSelective() throws Exception {
  9. Order order = new Order();
  10. order.setOrderId(1231);
  11. order.setUserId(222);
  12. order.setBusinessId(113);
  13. Boolean result = orderMapper.insert(order) > 0;
  14. System.out.println(result?"插入成功":"插入失败");
  15. }
  16. /**
  17. * 测试 in 的查询操作
  18. * @throws Exception
  19. */
  20. @Test
  21. public void selectByExample2() throws Exception {
  22. List<Integer> values = new ArrayList<>();
  23. values.add(112);
  24. values.add(113);
  25. OrderExample example = new OrderExample() ;
  26. example.createCriteria().andBusinessIdIn(values);
  27. List<Order> orderList = orderMapper.selectByExample(example) ;
  28. System.out.println(JSONObject.toJSONString(orderList));
  29. }
  30. /**
  31. * 测试between的查询操作
  32. * @throws Exception
  33. */
  34. @Test
  35. public void selectByExample3() throws Exception {
  36. OrderExample example = new OrderExample() ;
  37. example.createCriteria().andBusinessIdBetween(112,113);
  38. List<Order> orderList = orderMapper.selectByExample(example) ;
  39. System.out.println(JSONObject.toJSONString(orderList));
  40. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注