项目 学习sharding-jdbc 分库分表扩展框架
先丢代码地址
https://gitee.com/a247292980/sharding-jdbc
再丢pom.xml的dependency
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.5.RELEASE</spring.version> <mybatis.version>3.2.4</mybatis.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>com.dangdang</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> </dependencies>
最后丢代码结构图
我讲shardingJdbc.java这个文件的东西,留意好我的注释
public class ShardingJdbc { /** * main方法 */ public static void main(String[] args) { // 设置数据源,不分库的话,只设置一个 Map<String, DataSource> dataSourceMap = new HashMap<String, DataSource>(2); dataSourceMap.put("sharding_0", createDataSource("sharding_0")); dataSourceMap.put("sharding_1", createDataSource("sharding_1")); DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap); //分表分库的表,第一个参数是逻辑表名,第二个是实际表名,第三个是实际库 TableRule orderTableRule = new TableRule("t_order", Arrays.asList("t_order_0", "t_order_1"), dataSourceRule); TableRule orderItemTableRule = new TableRule("t_order_item", Arrays.asList("t_order_item_0", "t_order_item_1"), dataSourceRule); /** * DatabaseShardingStrategy 分库策略 * 参数一:根据哪个字段分库 * 参数二:分库路由函数 * * TableShardingStrategy 分表策略 * 参数一:根据哪个字段分表 * 参数二:分表路由函数 * * user_id选择哪个库 * order_id选择那个表 * * ModuloDataBaseShardingAlgorithm * ModuloTableShardingAlgorithm * 被2整除是0,反之是1 * */ ShardingRule shardingRule = new ShardingRule(dataSourceRule, Arrays.asList(orderTableRule, orderItemTableRule) , Arrays.asList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))) , new DatabaseShardingStrategy("user_id", new ModuloDataBaseShardingAlgorithm()) , new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())); DataSource dataSource = new ShardingDataSource(shardingRule); String sql = "SELECT i.* FROM t_order o JOIN t_order_item i " + "ON o.order_id=i.order_id " + "WHERE o.user_id= ? AND o.order_id = ?"; try { Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); // preparedStatement.setInt(1, 10); // preparedStatement.setInt(2, 1001); // 先根据分库规则去了sharding_1 preparedStatement.setInt(1, 11); // 再根据分表规则去了t_order_0,t_order_item_0 preparedStatement.setInt(2, 1000); ResultSet result = preparedStatement.executeQuery(); while (result.next()) { System.out.println("1--------" + result.getInt(1)); System.out.println("2--------" + result.getInt(2)); System.out.println("3--------" + result.getInt(3)); } } catch (SQLException e) { e.printStackTrace(); } } /** * @param dataSourceName * @return dataSource * @DESCRIPTION 创建数据源 */ private static DataSource createDataSource(String dataSourceName) { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName)); dataSource.setUsername("root"); dataSource.setPassword("123456789"); return dataSource; } }
另两个放的是具体分库逻辑,很简单,有兴趣的下源码跑一下即可