Commit 42747cab authored by cherrylzhao's avatar cherrylzhao
Browse files

simplify broadcast-table for raw-jdbc.

parent 1f581623
Loading
Loading
Loading
Loading
+2 −5
Original line number Diff line number Diff line
@@ -21,21 +21,18 @@

package org.apache.shardingsphere.example.broadcast.table.raw.jdbc;

import org.apache.shardingsphere.example.broadcast.table.raw.jdbc.factory.DataSourceFactory;
import org.apache.shardingsphere.example.broadcast.table.raw.jdbc.config.ShardingDatabasesConfigurationPrecise;
import org.apache.shardingsphere.example.common.jdbc.repository.CountryRepositroyImpl;
import org.apache.shardingsphere.example.common.jdbc.service.CountryServiceImpl;
import org.apache.shardingsphere.example.common.service.CommonService;
import org.apache.shardingsphere.example.type.ShardingType;

import javax.sql.DataSource;
import java.sql.SQLException;

public class JavaConfigurationExample {

    private static ShardingType shardingType = ShardingType.SHARDING_DATABASES;

    public static void main(final String[] args) throws SQLException {
        DataSource dataSource = DataSourceFactory.newInstance(shardingType);
        DataSource dataSource = new ShardingDatabasesConfigurationPrecise().getDataSource();
        CommonService countryService = getCountryService(dataSource);
        countryService.initEnvironment();
        countryService.processSuccess();
+7 −5
Original line number Diff line number Diff line
@@ -17,13 +17,13 @@

package org.apache.shardingsphere.example.broadcast.table.raw.jdbc;

import org.apache.shardingsphere.example.broadcast.table.raw.jdbc.factory.YamlDataSourceFactory;
import org.apache.shardingsphere.example.common.jdbc.repository.CountryRepositroyImpl;
import org.apache.shardingsphere.example.common.jdbc.service.CountryServiceImpl;
import org.apache.shardingsphere.example.common.service.CommonService;
import org.apache.shardingsphere.example.type.ShardingType;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

@@ -32,16 +32,18 @@ import java.sql.SQLException;
 */
public class YamlConfigurationExample {

    private static ShardingType shardingType = ShardingType.SHARDING_DATABASES;

    public static void main(final String[] args) throws SQLException, IOException {
        DataSource dataSource = YamlDataSourceFactory.newInstance(shardingType);
        DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile());
        CommonService countryService = getCountryService(dataSource);
        countryService.initEnvironment();
        countryService.processSuccess();
        countryService.cleanEnvironment();
    }
    
    private static File getFile() {
        return new File(Thread.currentThread().getClass().getResource("/META-INF/sharding-databases.yaml").getFile());
    }

    private static CommonService getCountryService(final DataSource dataSource) {
        return new CountryServiceImpl(new CountryRepositroyImpl(dataSource));
    }
+0 −36
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.example.broadcast.table.raw.jdbc.factory;

import org.apache.shardingsphere.example.broadcast.table.raw.jdbc.config.ShardingDatabasesConfigurationPrecise;
import org.apache.shardingsphere.example.type.ShardingType;

import javax.sql.DataSource;
import java.sql.SQLException;

public class DataSourceFactory {

    public static DataSource newInstance(final ShardingType shardingType) throws SQLException {
        switch (shardingType) {
            case SHARDING_DATABASES:
                return new ShardingDatabasesConfigurationPrecise().getDataSource();
            default:
                throw new UnsupportedOperationException(shardingType.name());
        }
    }
}
+0 −42
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.example.broadcast.table.raw.jdbc.factory;

import org.apache.shardingsphere.example.type.ShardingType;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

public class YamlDataSourceFactory {

    public static DataSource newInstance(final ShardingType shardingType) throws SQLException, IOException {
        switch (shardingType) {
            case SHARDING_DATABASES:
                return YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases.yaml"));
            default:
                throw new UnsupportedOperationException(shardingType.name());
        }
    }

    private static File getFile(final String fileName) {
        return new File(Thread.currentThread().getClass().getResource(fileName).getFile());
    }
}