diff --git a/sharding-jdbc-config-parent/sharding-jdbc-config-spring/src/main/resources/META-INF/namespace/rdb.xsd b/sharding-jdbc-config-parent/sharding-jdbc-config-spring/src/main/resources/META-INF/namespace/rdb.xsd index d59eed3bc915a..3ddc0ae4d0cc4 100644 --- a/sharding-jdbc-config-parent/sharding-jdbc-config-spring/src/main/resources/META-INF/namespace/rdb.xsd +++ b/sharding-jdbc-config-parent/sharding-jdbc-config-spring/src/main/resources/META-INF/namespace/rdb.xsd @@ -8,21 +8,21 @@ - + - + - + - + diff --git a/sharding-jdbc-doc/content/post/configuration.md b/sharding-jdbc-doc/content/post/configuration.md index fe43b2f028e14..7714714a9a784 100644 --- a/sharding-jdbc-doc/content/post/configuration.md +++ b/sharding-jdbc-doc/content/post/configuration.md @@ -81,7 +81,7 @@ dataSource: 数据源配置 tables: 分库分表配置,可配置多个logic_table_name : 逻辑表名 - actualTables: 真实表名,多个表以逗号分隔,支持inline表达式 + actualTables: 真实表名,多个表以逗号分隔,支持inline表达式,指定数据源需要加前缀,不加前缀为默认数据源 databaseStrategy: 分库策略 shardingColumns: 分片列名,多个列以逗号分隔 algorithmClassName: 分库算法全类名,该类需使用默认的构造器或者提供无参数的构造器,与algorithmExpression出现一个即可 @@ -216,9 +216,9 @@ props: 属性配置(可选) | *名称* | 类型 | *数据类型* | *必填*| *说明* | | -------------------- | -------- | --------- | ------| ----- | | logic-table | 属性 | String | 是 | 逻辑表名 | -| sharding-columns | 属性 | String | 是 | 分片列名,多个列以逗号分隔 | -| database-strategy | 属性 | String | 是 | 分库策略,对应``中分库策略id | -| table-strategy | 属性 | String | 是 | 分表策略,对应``中分表策略id | + actual-tables | 属性 | String | 是 | 真实表名,多个表以逗号分隔,支持inline表达式,指定数据源需要加前缀,不加前缀为默认数据源 指定数据源需要加前缀,不加前缀为默认数据源 | +| database-strategy | 属性 | String | 否 | 分库策略,对应``中分库策略id, 如果不填需配置`` | +| table-strategy | 属性 | String | 否 | 分表策略,对应``中分表策略id, 如果不填需配置`` | ### \ @@ -261,14 +261,14 @@ props: 属性配置(可选) ### \ -| *名称* | 类型 | *数据类型* | *必填*| *说明* | -| ------------------------------------ | ------------ | ---------- | -----| -------------------------------- | -| metrics.enable | 属性 | boolean | 否 | 是否开启度量采集,默认为false不开启 | -| metrics.millisecond.period | 属性 | String | 否 | 度量输出周期,单位为秒 | -| metrics.package.name | 属性 | String | 否 | 度量输出在日志中的标识名称 | -| executor.min.idle.size | 属性 | int | 否 | 最小空闲工作线程数量 | -| executor.max.size | 属性 | int | 否 | 最大工作线程数量 | -| executor.max.idle.timeout.millisecond| 属性 | int | 否 | 工作线程空闲时超时时间,默认以秒为单位 | +| *名称* | 类型 | *数据类型* | *必填*| *说明* | +| ------------------------------------ | ------------ | ---------- | -----| ------------------------------------| +| metrics.enable | 属性 | boolean | 否 | 是否开启度量采集,默认为false不开启 | +| metrics.millisecond.period | 属性 | String | 否 | 度量输出周期,单位为毫秒 | +| metrics.package.name | 属性 | String | 否 | 度量输出在日志中的标识名称 | +| executor.min.idle.size | 属性 | int | 否 | 最小空闲工作线程数量 | +| executor.max.size | 属性 | int | 否 | 最大工作线程数量 | +| executor.max.idle.timeout.millisecond| 属性 | int | 否 | 工作线程空闲时超时时间,默认以毫秒为单位 | ### Spring格式特别说明 如需使用inline表达式,需配置`ignore-unresolvable`为`true`,否则placeholder会把inline表达式当成属性key值导致出错. diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/SpringNamespaceMain.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/SpringNamespaceWithAssignedDataSourceMain.java similarity index 63% rename from sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/SpringNamespaceMain.java rename to sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/SpringNamespaceWithAssignedDataSourceMain.java index dba1a33f337c8..31e45356722c6 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/SpringNamespaceMain.java +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/SpringNamespaceWithAssignedDataSourceMain.java @@ -15,21 +15,26 @@ *

*/ -package com.dangdang.ddframe.rdb.sharding.example.config; +package com.dangdang.ddframe.rdb.sharding.example.config.spring; -import java.sql.SQLException; -import com.dangdang.ddframe.rdb.sharding.example.config.service.FooService; +import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.ConfigService; +import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.OrderService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -public final class SpringNamespaceMain { +import java.sql.SQLException; + +public final class SpringNamespaceWithAssignedDataSourceMain { public static void main(final String[] args) throws SQLException { - ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml"); - FooService service = applicationContext.getBean(FooService.class); + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/applicationContextWithAssignedDataSource.xml"); + OrderService service = applicationContext.getBean(OrderService.class); service.insert(); service.select(); service.delete(); service.select(); + + ConfigService configService = applicationContext.getBean(ConfigService.class); + configService.select(); } } diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/SpringNamespaceWithDefaultDataSourceMain.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/SpringNamespaceWithDefaultDataSourceMain.java new file mode 100644 index 0000000000000..5f9d4545089bc --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/SpringNamespaceWithDefaultDataSourceMain.java @@ -0,0 +1,37 @@ +/** + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed 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 com.dangdang.ddframe.rdb.sharding.example.config.spring; + +import java.sql.SQLException; + +import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.ConfigService; +import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.OrderService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public final class SpringNamespaceWithDefaultDataSourceMain { + + public static void main(final String[] args) throws SQLException { + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/applicationContextWithDefaultDataSource.xml"); + OrderService orderService = applicationContext.getBean(OrderService.class); + orderService.insert(); + orderService.select(); + orderService.delete(); + orderService.select(); + } +} diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/algorithm/SingleKeyModuloTableShardingAlgorithm.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/algorithm/SingleKeyModuloTableShardingAlgorithm.java similarity index 97% rename from sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/algorithm/SingleKeyModuloTableShardingAlgorithm.java rename to sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/algorithm/SingleKeyModuloTableShardingAlgorithm.java index 388a7a24e46d4..d8f4df3d03ca3 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/algorithm/SingleKeyModuloTableShardingAlgorithm.java +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/algorithm/SingleKeyModuloTableShardingAlgorithm.java @@ -16,7 +16,7 @@ *

*/ -package com.dangdang.ddframe.rdb.sharding.example.config.algorithm; +package com.dangdang.ddframe.rdb.sharding.example.config.spring.algorithm; import com.dangdang.ddframe.rdb.sharding.api.ShardingValue; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm; diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/ConfigRepository.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/ConfigRepository.java new file mode 100644 index 0000000000000..781415e43ded5 --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/ConfigRepository.java @@ -0,0 +1,23 @@ +/** + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed 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 com.dangdang.ddframe.rdb.sharding.example.config.spring.repository; + +public interface ConfigRepository { + + void select(); +} diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/ConfigRepositoryImpl.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/ConfigRepositoryImpl.java new file mode 100644 index 0000000000000..93dc0154e8a2a --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/ConfigRepositoryImpl.java @@ -0,0 +1,49 @@ +/** + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed 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 com.dangdang.ddframe.rdb.sharding.example.config.spring.repository; + +import com.dangdang.ddframe.rdb.sharding.spring.datasource.SpringShardingDataSource; +import org.springframework.stereotype.Repository; + +import javax.annotation.Resource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +@Repository +public class ConfigRepositoryImpl implements ConfigRepository { + + @Resource + private SpringShardingDataSource shardingDataSource; + + @Override + public void select() { + String sql = "SELECT c.* FROM t_config c"; + try ( + Connection conn = shardingDataSource.getConnection(); + PreparedStatement preparedStatement = conn.prepareStatement(sql)) { + try (ResultSet rs = preparedStatement.executeQuery()) { + while(rs.next()) { + System.out.println("configName:" + rs.getString(2) + ",configValue:" + rs.getString(3)); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } +} diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/service/FooService.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/OrderRepository.java similarity index 86% rename from sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/service/FooService.java rename to sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/OrderRepository.java index 3d8f9beb9dc6d..9ffcdd576bdd0 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/service/FooService.java +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/OrderRepository.java @@ -15,9 +15,9 @@ *

*/ -package com.dangdang.ddframe.rdb.sharding.example.config.service; +package com.dangdang.ddframe.rdb.sharding.example.config.spring.repository; -public interface FooService { +public interface OrderRepository { void insert(); diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/repository/FooRepositoryImpl.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/OrderRepositoryImpl.java similarity index 95% rename from sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/repository/FooRepositoryImpl.java rename to sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/OrderRepositoryImpl.java index 8ea68382126c4..055bbe7f91354 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/repository/FooRepositoryImpl.java +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/repository/OrderRepositoryImpl.java @@ -15,7 +15,7 @@ *

*/ -package com.dangdang.ddframe.rdb.sharding.example.config.repository; +package com.dangdang.ddframe.rdb.sharding.example.config.spring.repository; import com.dangdang.ddframe.rdb.sharding.spring.datasource.SpringShardingDataSource; import org.springframework.stereotype.Repository; @@ -25,7 +25,7 @@ import java.sql.ResultSet; @Repository -public class FooRepositoryImpl implements FooRepository { +public class OrderRepositoryImpl implements OrderRepository { @Resource private SpringShardingDataSource shardingDataSource; @@ -76,7 +76,7 @@ public void delete() { } @Override - public void select(){ + public void select() { 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 conn = shardingDataSource.getConnection(); diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/ConfigService.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/ConfigService.java new file mode 100644 index 0000000000000..27c1c86c30387 --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/ConfigService.java @@ -0,0 +1,23 @@ +/** + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed 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 com.dangdang.ddframe.rdb.sharding.example.config.spring.service; + +public interface ConfigService { + + void select(); +} diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/ConfigServiceImpl.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/ConfigServiceImpl.java new file mode 100644 index 0000000000000..b139cb491139e --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/ConfigServiceImpl.java @@ -0,0 +1,36 @@ +/** + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed 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 com.dangdang.ddframe.rdb.sharding.example.config.spring.service; + +import com.dangdang.ddframe.rdb.sharding.example.config.spring.repository.ConfigRepository; +import com.dangdang.ddframe.rdb.sharding.example.config.spring.repository.OrderRepository; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Service +public class ConfigServiceImpl implements ConfigService { + + @Resource + private ConfigRepository configRepository; + + @Override + public void select() { + configRepository.select(); + } +} diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/repository/FooRepository.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/OrderService.java similarity index 86% rename from sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/repository/FooRepository.java rename to sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/OrderService.java index b441bea678dd8..8f184b90b2d47 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/repository/FooRepository.java +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/OrderService.java @@ -15,9 +15,9 @@ *

*/ -package com.dangdang.ddframe.rdb.sharding.example.config.repository; +package com.dangdang.ddframe.rdb.sharding.example.config.spring.service; -public interface FooRepository { +public interface OrderService { void insert(); diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/service/FooServiceImpl.java b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/OrderServiceImpl.java similarity index 72% rename from sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/service/FooServiceImpl.java rename to sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/OrderServiceImpl.java index 3e4a731c25c66..37a73f9d94748 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/service/FooServiceImpl.java +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/service/OrderServiceImpl.java @@ -15,23 +15,23 @@ *

*/ -package com.dangdang.ddframe.rdb.sharding.example.config.service; +package com.dangdang.ddframe.rdb.sharding.example.config.spring.service; -import com.dangdang.ddframe.rdb.sharding.example.config.repository.FooRepository; +import com.dangdang.ddframe.rdb.sharding.example.config.spring.repository.OrderRepository; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service -public class FooServiceImpl implements FooService { +public class OrderServiceImpl implements OrderService { @Resource - private FooRepository fooRepository; + private OrderRepository orderRepository; @Override public void insert() { try { - fooRepository.insert(); + orderRepository.insert(); } catch (Exception ex) { ex.printStackTrace(); } @@ -39,11 +39,11 @@ public void insert() { @Override public void delete() { - fooRepository.delete(); + orderRepository.delete(); } @Override public void select() { - fooRepository.select(); + orderRepository.select(); } } diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContextWithAssignedDataSource.xml b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContextWithAssignedDataSource.xml new file mode 100644 index 0000000000000..652cb8eec3e79 --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContextWithAssignedDataSource.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + false + + + diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContext.xml b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContextWithDefaultDataSource.xml similarity index 96% rename from sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContext.xml rename to sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContextWithDefaultDataSource.xml index acd72af627812..21bc60468a6b8 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContext.xml +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/META-INF/applicationContextWithDefaultDataSource.xml @@ -10,7 +10,7 @@ http://www.dangdang.com/schema/ddframe/rdb http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd "> - + @@ -30,7 +30,7 @@ - + diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/all_schema.sql b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/all_schema.sql index 352a2a2a9dd48..0a79ed72cf4f7 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/all_schema.sql +++ b/sharding-jdbc-example/sharding-jdbc-example-config-spring/src/main/resources/all_schema.sql @@ -1,5 +1,8 @@ CREATE SCHEMA IF NOT EXISTS `dbtbl_0`; CREATE SCHEMA IF NOT EXISTS `dbtbl_1`; +CREATE SCHEMA IF NOT EXISTS `dbtbl_config`; + +CREATE TABLE IF NOT EXISTS `dbtbl_config`.`t_config`(`id` INT NOT NULL, `name` VARCHAR(20), `value` VARCHAR(50)); CREATE TABLE IF NOT EXISTS `dbtbl_0`.`t_order_0` (`order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_id`)); CREATE TABLE IF NOT EXISTS `dbtbl_0`.`t_order_1` (`order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_id`)); @@ -18,3 +21,5 @@ CREATE TABLE IF NOT EXISTS `dbtbl_1`.`t_order_item_0` (`order_item_id` INT NOT N CREATE TABLE IF NOT EXISTS `dbtbl_1`.`t_order_item_1` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_item_id`)); CREATE TABLE IF NOT EXISTS `dbtbl_1`.`t_order_item_2` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_item_id`)); CREATE TABLE IF NOT EXISTS `dbtbl_1`.`t_order_item_3` (`order_item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(10) NULL, PRIMARY KEY (`order_item_id`)); + +INSERT INTO `dbtbl_config`.`t_config` VALUES (1, 'config_key', 'config_value'); diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/YamlWithAssignedDataSourceMain.java b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/YamlWithAssignedDataSourceMain.java new file mode 100644 index 0000000000000..8d98791bcf2c3 --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/YamlWithAssignedDataSourceMain.java @@ -0,0 +1,82 @@ +/** + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed 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 com.dangdang.ddframe.rdb.sharding.example.config.spring; + +import java.io.File; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import javax.sql.DataSource; + +import com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource; + +public final class YamlWithAssignedDataSourceMain { + + public static void main(final String[] args) throws Exception { + YamlShardingDataSource dataSource = new YamlShardingDataSource( + new File(YamlWithAssignedDataSourceMain.class.getResource("/META-INF/withAssignedDataSource.yaml").getFile())); + printSimpleSelect(dataSource); + printJoinSelect(dataSource); + printGroupBy(dataSource); + } + + private static void printSimpleSelect(DataSource dataSource) throws SQLException { + String sql = "SELECT c.* FROM t_config c"; + try ( + Connection conn = dataSource.getConnection(); + PreparedStatement preparedStatement = conn.prepareStatement(sql)) { + try (ResultSet rs = preparedStatement.executeQuery()) { + while(rs.next()) { + System.out.println(rs.getInt(1)); + System.out.println(rs.getString(2)); + System.out.println(rs.getString(3)); + } + } + } + } + + private static void printJoinSelect(DataSource dataSource) throws SQLException { + 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 conn = dataSource.getConnection(); + PreparedStatement preparedStatement = conn.prepareStatement(sql)) { + preparedStatement.setInt(1, 10); + preparedStatement.setInt(2, 1001); + try (ResultSet rs = preparedStatement.executeQuery()) { + while(rs.next()) { + System.out.println(rs.getInt(1)); + System.out.println(rs.getInt(2)); + System.out.println(rs.getString(3)); + } + } + } + } + + private static void printGroupBy(DataSource dataSource) throws SQLException { + String sql = "SELECT o.user_id, COUNT(*) FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id GROUP BY o.user_id"; + try ( + Connection conn = dataSource.getConnection(); + PreparedStatement preparedStatement = conn.prepareStatement(sql)) { + ResultSet rs = preparedStatement.executeQuery(); + while (rs.next()) { + System.out.println("user_id: " + rs.getInt(1)+ ", count: " + rs.getInt(2)); + } + } + } +} diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/YamlConfigMain.java b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/YamlWithDefaultDataSourceMain.java similarity index 86% rename from sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/YamlConfigMain.java rename to sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/YamlWithDefaultDataSourceMain.java index 6bba603362a0d..1c747de3a7bb1 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/YamlConfigMain.java +++ b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/java/com/dangdang/ddframe/rdb/sharding/example/config/spring/YamlWithDefaultDataSourceMain.java @@ -15,27 +15,27 @@ *

*/ -package com.dangdang.ddframe.rdb.sharding.example.config; +package com.dangdang.ddframe.rdb.sharding.example.config.spring; +import com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource; + +import javax.sql.DataSource; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import javax.sql.DataSource; - -import com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource; -public final class YamlConfigMain { +public final class YamlWithDefaultDataSourceMain { public static void main(final String[] args) throws Exception { YamlShardingDataSource dataSource = new YamlShardingDataSource( - new File(YamlConfigMain.class.getResource("/META-INF/config.yaml").getFile())); - printSimpleSelect(dataSource); + new File(YamlWithDefaultDataSourceMain.class.getResource("/META-INF/withDefaultDataSource.yaml").getFile())); + printJoinSelect(dataSource); printGroupBy(dataSource); } - private static void printSimpleSelect(DataSource dataSource) throws SQLException { + private static void printJoinSelect(DataSource dataSource) throws SQLException { 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 conn = dataSource.getConnection(); diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/withAssignedDataSource.yaml b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/withAssignedDataSource.yaml new file mode 100644 index 0000000000000..f10a8a9f1ac6e --- /dev/null +++ b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/withAssignedDataSource.yaml @@ -0,0 +1,48 @@ +dataSource: + ds_0: !!org.apache.commons.dbcp.BasicDataSource + driverClassName: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/ds_0 + username: root + password: + ds_1: !!org.apache.commons.dbcp.BasicDataSource + driverClassName: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/ds_1 + username: root + password: + ds_config: !!org.apache.commons.dbcp.BasicDataSource + driverClassName: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/ds_config + username: root + password: + +tables: + t_config: + actualTables: ds_config.t_config + + t_order: + actualTables: ds_${0..1}.t_order_${0..1} + tableStrategy: + shardingColumns: order_id + algorithmExpression: t_order_${order_id.longValue() % 2} + + t_order_item: + actualTables: ds_${0..1}.t_order_item_${0..1} + #绑定表中其余的表的策略与第一张表的策略相同 + tableStrategy: + shardingColumns: order_id + algorithmExpression: t_order_item_${order_id.longValue() % 2} + +bindingTables: + - tableNames: t_order,t_order_item + +#默认数据库分片策略 +defaultDatabaseStrategy: + shardingColumns: user_id + algorithmExpression: ds_${user_id.longValue() % 2} + +defaultTableStrategy: + shardingColumns: none + algorithmClassName: com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm + +props: + metrics.enable: false diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/config.yaml b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/withDefaultDataSource.yaml similarity index 97% rename from sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/config.yaml rename to sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/withDefaultDataSource.yaml index f76a229ef67c8..e233cf2593aca 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/config.yaml +++ b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/META-INF/withDefaultDataSource.yaml @@ -33,4 +33,4 @@ defaultDatabaseStrategy: algorithmExpression: ds_${user_id.longValue() % 2} props: - metrics.enable: false \ No newline at end of file + metrics.enable: false diff --git a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/all_schema.sql b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/all_schema.sql index a523afe4133c7..24424bfd616e1 100644 --- a/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/all_schema.sql +++ b/sharding-jdbc-example/sharding-jdbc-example-config-yaml/src/main/resources/all_schema.sql @@ -1,6 +1,9 @@ +CREATE SCHEMA IF NOT EXISTS `ds_config`; CREATE SCHEMA IF NOT EXISTS `ds_0`; CREATE SCHEMA IF NOT EXISTS `ds_1`; +CREATE TABLE IF NOT EXISTS `ds_config`.`t_config`(`id` INT NOT NULL, `name` VARCHAR(20), `value` VARCHAR(50)); + CREATE TABLE IF NOT EXISTS `ds_0`.`t_order_0` (`order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(50), PRIMARY KEY (`order_id`)); CREATE TABLE IF NOT EXISTS `ds_0`.`t_order_1` (`order_id` INT NOT NULL, `user_id` INT NOT NULL, `status` VARCHAR(50), PRIMARY KEY (`order_id`)); CREATE TABLE IF NOT EXISTS `ds_0`.`t_order_item_0` (`item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, PRIMARY KEY (`item_id`)); @@ -11,6 +14,8 @@ CREATE TABLE IF NOT EXISTS `ds_1`.`t_order_1` (`order_id` INT NOT NULL, `user_id CREATE TABLE IF NOT EXISTS `ds_1`.`t_order_item_0` (`item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, PRIMARY KEY (`item_id`)); CREATE TABLE IF NOT EXISTS `ds_1`.`t_order_item_1` (`item_id` INT NOT NULL, `order_id` INT NOT NULL, `user_id` INT NOT NULL, PRIMARY KEY (`item_id`)); +INSERT INTO `ds_config`.`t_config` VALUES (1, 'config_key', 'config_value'); + INSERT INTO `ds_0`.`t_order_0` VALUES (1000, 10, 'INIT'); INSERT INTO `ds_0`.`t_order_0` VALUES (1002, 10, 'INIT'); INSERT INTO `ds_0`.`t_order_0` VALUES (1004, 10, 'INIT');