Java,ShardingSphere,Sharding-JDBC,分库分表的入门程序案例( 二 )

代码案例2:
import com.alibaba.druid.pool.DruidDataSource;import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;import javax.sql.DataSource;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import java.util.HashMap;import java.util.Map;import java.util.Properties;public class ShardingJDBCDemo02 {public static void main(String[] args) {// =====================================================================//// 配置真实数据源Map<String, DataSource> dataSourceMap = new HashMap<>();// 配置第一个数据源DruidDataSource dataSource1 = new DruidDataSource();dataSource1.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource1.setUrl("jdbc:mysql://192.168.1.11:3306/ds1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");dataSource1.setUsername("root");dataSource1.setPassword("root1234");dataSourceMap.put("ds1", dataSource1);// 配置第二个数据源DruidDataSource dataSource2 = new DruidDataSource();dataSource2.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource2.setUrl("jdbc:mysql://192.168.1.11:3306/ds2?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");dataSource2.setUsername("root");dataSource2.setPassword("root1234");dataSourceMap.put("ds2", dataSource2);// =====================================================================//// 配置User表规则// 配置User表规则TableRuleConfiguration userTableRuleConfig = new TableRuleConfiguration("t_user");// 配置分库userTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("id", "ds$->{id%2+1}"));// 配置Order表规则TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order", "ds${1..2}.t_order${0..1}");// 配置分库 + 分表策略orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id%2+1}"));orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("id", "t_order${id%2}"));ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();shardingRuleConfig.getTableRuleConfigs().add(userTableRuleConfig);shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);// =====================================================================//// 获取数据源对象// =====================================================================//DataSource dataSource = null;try {dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());} catch (SQLException e) {e.printStackTrace();}// =====================================================================//// 插入数据// =====================================================================//for (int i = 1; i <= 10; i++) {try {String sql = "insert into t_order(`id`,`user_id`,`code`) values(" + i + "," + i + ",'1')";Connection conn = dataSource.getConnection();Statement stmt = conn.createStatement();stmt.executeUpdate(sql);System.out.println("插入:" + i);} catch (SQLException e) {e.printStackTrace();}}System.out.println("操作完成...");}}



推荐阅读