资源简介

spring4.0.2+mybatis3.2.4+sharding-jdbc1.4.2实现分库分表、主从及事物支持

资源截图

代码片段和文件信息

package com.yufei.sharding_jdbc.algorithm;
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;
import com.google.common.collect.Range;
import java.util.Collection;
import java.util.linkedHashSet;
/**
 * @Author:yufei
 * @Description:
 * @Date: 10:32 2017/12/1
 */
public class UserDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm {

    /**
     * sql 中关键字 匹配符为 =的时候,表的路由函数
     * @param availableTargetNames
     * @param shardingValue
     * @return
     */
    public String doEqualSharding(Collection availableTargetNames ShardingValue shardingValue) {
        for (String each : availableTargetNames) {
            if (each.endsWith(shardingValue.getValue() % 2 + ““)) {
                return each;
            }
        }
        throw new IllegalArgumentException();
    }

    /**
     * sql 中关键字 匹配符为 in 的时候,表的路由函数
     * @param availableTargetNames
     * @param shardingValue
     * @return
     */
    public Collection doInSharding(Collection availableTargetNames ShardingValue shardingValue) {
        Collection result = new linkedHashSet(availableTargetNames.size());
        for (Integer value : shardingValue.getValues()) {
            for (String tableName : availableTargetNames) {
                if (tableName.endsWith(value % 2 + ““)) {
                    result.add(tableName);
                }
            }
        }
        return result;
    }

    /**
     * sql 中关键字 匹配符为 between的时候,表的路由函数
     * @param availableTargetNames
     * @param shardingValue
     * @return
     */
    public Collection doBetweenSharding(Collection availableTargetNames
                                                ShardingValue shardingValue) {
        Collection result = new linkedHashSet(availableTargetNames.size());
        Range range = (Range) shardingValue.getValueRange();
        for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
            for (String each : availableTargetNames) {
                if (each.endsWith(i % 2 + ““)) {
                    result.add(each);
                }
            }
        }
        return result;
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\
     文件        3350  2017-12-01 11:36  sharding-jdbc-demo\pom.xml
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\yufei\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\algorithm\
     文件        2532  2017-12-01 11:31  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\algorithm\UserDatabaseShardingAlgorithm.java
     文件        2337  2017-12-01 11:30  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\algorithm\UserTableShardingAlgorithm.java
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\entity\
     文件        1025  2017-12-01 11:34  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\entity\User.java
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\mapper\
     文件         310  2017-12-01 10:56  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\mapper\UserMapper.java
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\service\
     文件         460  2017-12-01 11:22  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\service\IUserService.java
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\service\impl\
     文件        1836  2017-12-01 11:23  sharding-jdbc-demo\src\main\java\com\yufei\sharding_jdbc\service\impl\UserServiceImpl.java
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\resources\
     文件         883  2017-12-01 10:43  sharding-jdbc-demo\src\main\resources\jdbc.properties
     文件        2752  2016-03-06 15:46  sharding-jdbc-demo\src\main\resources\log4j.xml
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\main\resources\mapper\
     文件        1335  2017-12-01 11:11  sharding-jdbc-demo\src\main\resources\mapper\UserMapper.xml
     文件        1460  2017-12-01 11:43  sharding-jdbc-demo\src\main\resources\sharding.sql
     文件        7213  2017-12-01 10:42  sharding-jdbc-demo\src\main\resources\spring-database.xml
     文件        2732  2017-12-01 11:05  sharding-jdbc-demo\src\main\resources\spring-sharding.xml
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\test\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\test\java\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\test\java\com\
     目录           0  2017-12-01 11:44  sharding-jdbc-demo\src\test\java\com\yufei\
............此处省略2个文件信息

评论

共有 条评论