• 大小: 25KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: 其他
  • 标签: shardi  

资源简介

springboot整合sharding-db按月分表分库

资源截图

代码片段和文件信息

package com.plat.db;

import java.sql.SQLException;
import java.util.linkedHashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.google.common.base.Preconditions;
import com.plat.db.rule.PlatYamlShardingRuleConfiguration;
import com.plat.db.utils.PropertyUtil;

import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import io.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration;
import io.shardingsphere.shardingjdbc.util.DataSourceUtil;

@Configuration
@EnableConfigurationProperties({PlatYamlShardingRuleConfiguration.class})
@ConditionalOnProperty(prefix=“com.plat.sharding“ name=“enabled“ havingValue=“true“ matchIfMissing=false)
@AutoConfigureAfter({DruidDataSourceAutoConfigure.class SpringBootConfiguration.class})
public class PlatShardingAutoConfiguration implements EnvironmentAware {
    
    
    private final Map dataSourceMap = new linkedHashMap<>();
    
    @Autowired
    private PlatYamlShardingRuleConfiguration shardingRule;
    
    /**
     * Get data source bean.
     * 
     * @return data source bean
     * @throws SQLException SQL exception
     */
    @Bean
    public DataSource dataSource() throws SQLException {
    
     return ShardingDataSourceFactory.createDataSource(dataSourceMap shardingRule.build() shardingRule.getConfigMap() shardingRule.getProps());
    }
    
    @Override
    public final void setEnvironment(final Environment environment) {
        setDataSourceMap(environment);
    }
    
    @SuppressWarnings(“unchecked“)
    private void setDataSourceMap(final Environment environment) {
        String prefix = “sharding.jdbc.datasource.“;
        String dataSources = environment.getProperty(prefix + “names“);
        for (String each : dataSources.split(““)) {
            try {
                Mapject> dataSourceProps = PropertyUtil.handle(environment prefix + each Map.class);
                Preconditions.checkState(!dataSourceProps.isEmpty() “Wrong datasource properties!“);
                DataSource dataSource = DataSourceUtil.getDataSource(dataSourceProps.get(“type“).toString() dataSourceProps);
                dataSourceMap.put(each dataSource);
            } catch (final ReflectiveOperationException ex) {
                th

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-01-28 02:26  sharding-db\
     文件        6957  2019-01-28 02:26  sharding-db\README.md
     文件        4001  2019-01-28 02:26  sharding-db\pom.xml
     目录           0  2019-01-28 02:26  sharding-db\src\
     目录           0  2019-01-28 02:26  sharding-db\src\main\
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\plat\
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\
     文件        3099  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\PlatShardingAutoConfiguration.java
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\algorithm\
     文件        1850  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\algorithm\InilePreciseAlgorithm.java
     文件        2546  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\algorithm\InileRangAlgorithm.java
     文件        2904  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\algorithm\TimerPreciseAlgorithm.java
     文件        3742  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\algorithm\TimerRangAlgorithm.java
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\keygen\
     文件        4096  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\keygen\SnowFlakeKeyGenerator.java
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\
     文件        1670  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\PlatYamlShardingRuleConfiguration.java
     文件        2930  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\PlatYamlShardingStrategyConfiguration.java
     文件        1478  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\PlatYamlTableRuleConfiguration.java
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\strategy\
     文件         234  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\strategy\PlatCoustomClassStrategyConfiguration.java
     文件         203  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\strategy\PlatInlineStrategyConfiguration.java
     文件         180  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\rule\strategy\PlatTimerStrategyConfiguration.java
     目录           0  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\utils\
     文件       20026  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\utils\DateUtil.java
     文件        2954  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\utils\NumberUtil.java
     文件        4106  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\utils\PropertyUtil.java
     文件        5155  2019-01-28 02:26  sharding-db\src\main\java\com\plat\db\utils\StringParserUtil.java
     目录           0  2019-01-28 02:26  sharding-db\src\main\resources\
............此处省略3个文件信息

评论

共有 条评论