spring简单分库分表

数据源获取工具

接口
package com.dlq.blog.datasource.interfaces;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 数据源获取接口
 * @author donglq
 * @date 2017/10/4 23:20
 */
public interface IDatasourceGetter {

    JdbcTemplate getJdbcTemplate(String tablename);

}

实现
package com.dlq.blog.datasource;

import com.dlq.blog.datasource.interfaces.IDatasourceGetter;
import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

/**
 * 数据源获取实现
 * @author donglq
 * @date 2017/10/4 23:20
 */
public class DatasourceGetterImpl implements IDatasourceGetter{

    /**数据源**/
    private static JdbcTemplate[] templates;

    /**默认数据源**/
    private static JdbcTemplate defaultTemplate;

    private final static DatasourceIndexFunction DATASOURCE_INDEX_FUNCTION = new DatasourceIndexFunction();

    /**
     * 初始化
     * @param datasources 多数据源,有序
     * @param defaultdataSource
     */
    public DatasourceGetterImpl(DataSource[] datasources, DataSource defaultdataSource) {
        Preconditions.checkNotNull(datasources);
        Preconditions.checkNotNull(defaultdataSource);
        this.defaultTemplate = new JdbcTemplate(defaultdataSource);
        this.templates = new JdbcTemplate[datasources.length];
        for (int i = 0; i < datasources.length; i++) {
            this.templates[i] = new JdbcTemplate(datasources[i]);
        }
    }

    /**
     * 数据源获取类
     * 根据表名获取JdbcTemplate
     */
    private static class DatasourceIndexFunction implements Function {
        @Override
        public JdbcTemplate apply(String input) {
            if (Strings.isNullOrEmpty(input)) {
                return defaultTemplate;
            }
            try {
                int digit = Integer.parseInt(CharMatcher.DIGIT.retainFrom(input));
                return templates[digit % templates.length];
            } catch (NumberFormatException e) {
            }
            return defaultTemplate;
        }
    }

    /**
     * 缓存表名对应的库JdbcTemplate
     */
    private static final LoadingCache cache = CacheBuilder.newBuilder().maximumSize(1024).build(
            new CacheLoader() {
                @Override
                public JdbcTemplate load(String key) throws Exception {
                    return DATASOURCE_INDEX_FUNCTION.apply(key);
                }
            }
    );

    /**
     * 通过表名获取JdbcTemplate
     * @param tablename
     * @return
     */
    @Override
    public JdbcTemplate getJdbcTemplate(String tablename) {
        try {
            return cache.get(tablename);
        } catch (ExecutionException e) {
        }
        return defaultTemplate;
    }

}

测试

配置文件



    
    
        
    
    
    
        
        
        
        
    
    
    
        
        
        
        
    
    
        
        
        
        
    
    
        
        
        
        
    
    
        
        
        
        
    
    
    
        
            
                
                
                
                
            
        
        
    

测试方法
@Resource
IDatasourceGetter datasourceGetter;

public List getByIdcard(String idcard) {
    //获取表明
    String tableName = new Function() {
        @Override
        public String apply(String s) {
            String suffix = "";
            //共8张表,用身份证前6位计算位于哪张表
            int tableIndex = Integer.valueOf(s.substring(0, 6)) % 8;
            DecimalFormat df = new DecimalFormat();
            df.applyPattern("_00");
            suffix = df.format(tableIndex);
            return "user" + suffix;
        }
    }.apply(idcard);
    List users = datasourceGetter.getJdbcTemplate(tableName).query("select * from " + tableName + " where idcard='" + idcard + "'", new RowMapper() {
        @Nullable
        @Override
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            System.out.println("idcard: " + rs.getString("idcard"));
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setFirstname(rs.getString("firstname"));
            user.setLastname(rs.getString("lastname"));
            user.setIdcard(rs.getString("idcard"));
            user.setAddress(rs.getString("address"));
            return user;
        }
    });
    return users;
}

你可能感兴趣的:(spring简单分库分表)