You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/10/30 13:12:00 UTC
[shardingsphere] branch master updated: Add spring namespace jdbc
example template (#13361)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d1fce32 Add spring namespace jdbc example template (#13361)
d1fce32 is described below
commit d1fce321b51e4d83e06bf4cb139e2c3e4de87f71
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Sat Oct 30 21:11:24 2021 +0800
Add spring namespace jdbc example template (#13361)
* add springnamespace jdbc example
* Add spring namespace jdbc example template
* remove unless dependency
---
.../example/engine/ExampleGenerateEngine.java | 14 +-
.../src/main/resources/template/Example.ftl | 15 +-
.../spring-namespace-jdbc/ExampleService.ftl | 224 +++++++++++++++++++++
.../template/spring-namespace-jdbc/data-model.yaml | 22 ++
.../main/resources/template/xml/application.ftl | 82 ++++++++
.../pom.xml | 1 +
.../pom.xml | 22 +-
...oryLocalShardingSpringNamespaceJdbcExample.java | 33 +++
...lShardingSpringNamespaceJdbcExampleService.java | 220 ++++++++++++++++++++
.../spring/namespace/jdbc/entity/Order.java | 70 +++++++
.../spring/namespace/jdbc/entity/OrderItem.java | 70 +++++++
.../src/main/resources/application.xml | 82 ++++++++
12 files changed, 838 insertions(+), 17 deletions(-)
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
index 4736025..5b44745 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
@@ -63,16 +63,16 @@ public final class ExampleGenerateEngine {
CONFIGURATION.setDefaultEncoding("UTF-8");
RENAME_TEMPLATE_MAP.put("Example", "Example.ftl");
- RENAME_TEMPLATE_MAP.put("ExampleService", "ExampleService.ftl");
+ RENAME_TEMPLATE_MAP.put("ExampleService", "spring-namespace-jdbc/ExampleService.ftl");
UN_NAME_TEMPLATE_MAP.put("entity/Order", "entity/Order.java");
UN_NAME_TEMPLATE_MAP.put("entity/OrderItem", "entity/OrderItem.java");
- UN_NAME_TEMPLATE_MAP.put("springboot-starter-mybatis/OrderItemRepository", "repository/OrderItemRepository.java");
- UN_NAME_TEMPLATE_MAP.put("springboot-starter-mybatis/OrderRepository", "repository/OrderRepository.java");
+ //UN_NAME_TEMPLATE_MAP.put("springboot-starter-mybatis/OrderItemRepository", "repository/OrderItemRepository.java");
+ //UN_NAME_TEMPLATE_MAP.put("springboot-starter-mybatis/OrderRepository", "repository/OrderRepository.java");
- RESOURCE_TEMPLATE_MAP.put("mappers/OrderItemMapper", "mappers/OrderItemMapper.xml");
- RESOURCE_TEMPLATE_MAP.put("mappers/OrderMapper", "mappers/OrderMapper.xml");
- RESOURCE_TEMPLATE_MAP.put("properties/application", "application.properties");
+ //RESOURCE_TEMPLATE_MAP.put("mappers/OrderItemMapper", "mappers/OrderItemMapper.xml");
+ //RESOURCE_TEMPLATE_MAP.put("mappers/OrderMapper", "mappers/OrderMapper.xml");
+ RESOURCE_TEMPLATE_MAP.put("xml/application", "application.xml");
} catch (IOException e) {
e.printStackTrace();
}
@@ -131,7 +131,7 @@ public final class ExampleGenerateEngine {
public static void main(String[] args) {
Yaml yaml = new Yaml();
- InputStream in = ExampleGenerateEngine.class.getResourceAsStream("/template/springboot-starter-mybatis/data-model.yaml");
+ InputStream in = ExampleGenerateEngine.class.getResourceAsStream("/template/spring-namespace-jdbc/data-model.yaml");
Map<String, String> dataModel = yaml.loadAs(in, Map.class);
generateJavaCode(dataModel);
generateResourcesFile(dataModel);
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl
index 809e3ca..168b313 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl
@@ -17,21 +17,34 @@
package org.apache.shardingsphere.example.${feature}.${framework?replace('-', '.')};
+<#if framework=="springboot-starter-mybatis">
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
+<#elseif framework=="spring-namespace-jdbc">
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+</#if>
+
+import java.sql.SQLException;
<#assign frameworkName="">
<#list framework?split("-") as framework1>
<#assign frameworkName=frameworkName + framework1?cap_first>
</#list>
+<#if framework=="springboot-starter-mybatis">
@MapperScan("org.apache.shardingsphere.example.${feature}.springboot.starter.mybatis.repository")
@SpringBootApplication
+</#if>
public class ${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}Example {
- public static void main(final String[] args) {
+ public static void main(final String[] args) throws SQLException {
+ <#if framework=="spring-namespace-jdbc">
+ try (ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml")) {
+ <#else>
try (ConfigurableApplicationContext applicationContext = SpringApplication.run(${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}Example.class, args)) {
+ </#if>
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}ExampleService exampleService = applicationContext.getBean(${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}ExampleService.class);
exampleService.run();
}
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/spring-namespace-jdbc/ExampleService.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/spring-namespace-jdbc/ExampleService.ftl
new file mode 100644
index 0000000..96fcab1
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/spring-namespace-jdbc/ExampleService.ftl
@@ -0,0 +1,224 @@
+/*
+ * 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.${feature}.${framework?replace('-', '.')};
+
+import org.apache.shardingsphere.example.${feature}.${framework?replace('-', '.')}.entity.Order;
+import org.apache.shardingsphere.example.${feature}.${framework?replace('-', '.')}.entity.OrderItem;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+<#assign frameworkName="">
+<#list framework?split("-") as framework1>
+ <#assign frameworkName=frameworkName + framework1?cap_first>
+</#list>
+@Service
+public final class ${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}ExampleService {
+
+ @Resource(name = "shardingDataSource")
+ private DataSource dataSource;
+
+ /**
+ * Execute test.
+ *
+ * @throws SQLException
+ */
+ public void run() throws SQLException {
+ try {
+ this.initEnvironment();
+ this.processSuccess();
+ } finally {
+ this.cleanEnvironment();
+ }
+ }
+
+ /**
+ * Initialize the database test environment.
+ * @throws SQLException
+ */
+ private void initEnvironment() throws SQLException {
+ String createOrderTableSql = "CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))";
+ String createOrderItemTableSql = "CREATE TABLE IF NOT EXISTS t_order_item "
+ + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id))";
+ String createAddressTableSql = "CREATE TABLE IF NOT EXISTS t_address "
+ + "(address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT NULL, PRIMARY KEY (address_id))";
+ String truncateOrderTable = "TRUNCATE TABLE t_order";
+ String truncateOrderItemTable = "TRUNCATE TABLE t_order_item";
+ String truncateAddressTableSql = "TRUNCATE TABLE t_address";
+
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(createOrderTableSql);
+ statement.executeUpdate(createOrderItemTableSql);
+ statement.executeUpdate(createAddressTableSql);
+ statement.executeUpdate(createAddressTableSql);
+ statement.executeUpdate(truncateOrderTable);
+ statement.executeUpdate(truncateOrderItemTable);
+ statement.executeUpdate(truncateAddressTableSql);
+ }
+ }
+
+ private void processSuccess() throws SQLException {
+ System.out.println("-------------- Process Success Begin ---------------");
+ List<Long> orderIds = insertData();
+ printData();
+ deleteData(orderIds);
+ printData();
+ System.out.println("-------------- Process Success Finish --------------");
+ }
+
+ private List<Long> insertData() throws SQLException {
+ System.out.println("---------------------------- Insert Data ----------------------------");
+ List<Long> result = new ArrayList<>(10);
+ for (int i = 1; i <= 10; i++) {
+ Order order = insertOrder(i);
+ insertOrderItem(i, order);
+ result.add(order.getOrderId());
+ }
+ return result;
+ }
+
+ private Order insertOrder(final int i) throws SQLException {
+ Order order = new Order();
+ order.setUserId(i);
+ order.setAddressId(i);
+ order.setStatus("INSERT_TEST");
+ String sql = "INSERT INTO t_order (user_id, address_id, status) VALUES (?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+ preparedStatement.setInt(1, order.getUserId());
+ preparedStatement.setLong(2, order.getAddressId());
+ preparedStatement.setString(3, order.getStatus());
+ preparedStatement.executeUpdate();
+ try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+ if (resultSet.next()) {
+ order.setOrderId(resultSet.getLong(1));
+ }
+ }
+ }
+ return order;
+ }
+
+ private void insertOrderItem(final int i, final Order order) throws SQLException {
+ OrderItem orderItem = new OrderItem();
+ orderItem.setOrderId(order.getOrderId());
+ orderItem.setUserId(i);
+ orderItem.setStatus("INSERT_TEST");
+ String sql = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+ preparedStatement.setLong(1, orderItem.getOrderId());
+ preparedStatement.setInt(2, orderItem.getUserId());
+ preparedStatement.setString(3, orderItem.getStatus());
+ preparedStatement.executeUpdate();
+ try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+ if (resultSet.next()) {
+ orderItem.setOrderItemId(resultSet.getLong(1));
+ }
+ }
+ }
+ }
+
+ private void deleteData(final List<Long> orderIds) throws SQLException {
+ System.out.println("---------------------------- Delete Data ----------------------------");
+ for (Long each : orderIds) {
+ String deleteOrderSql = "DELETE FROM t_order WHERE order_id=?";
+ String deleteOrderItemSql = "DELETE FROM t_order_item WHERE order_id=?";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement orderPreparedStatement = connection.prepareStatement(deleteOrderSql);
+ PreparedStatement orderItemPreparedStatement = connection.prepareStatement(deleteOrderItemSql)) {
+ orderPreparedStatement.setLong(1, each);
+ orderItemPreparedStatement.setLong(1, each);
+ orderPreparedStatement.executeUpdate();
+ orderItemPreparedStatement.executeUpdate();
+ }
+ }
+ }
+
+ private void printData() throws SQLException {
+ System.out.println("---------------------------- Print Order Data -----------------------");
+ for (Object each : this.getOrders()) {
+ System.out.println(each);
+ }
+ System.out.println("---------------------------- Print OrderItem Data -------------------");
+ for (Object each : this.getOrderItems()) {
+ System.out.println(each);
+ }
+ }
+
+ protected List<OrderItem> getOrderItems() throws SQLException {
+ String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id";
+ List<OrderItem> result = new LinkedList<>();
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ OrderItem orderItem = new OrderItem();
+ orderItem.setOrderItemId(resultSet.getLong(1));
+ orderItem.setOrderId(resultSet.getLong(2));
+ orderItem.setUserId(resultSet.getInt(3));
+ orderItem.setStatus(resultSet.getString(4));
+ result.add(orderItem);
+ }
+ }
+ return result;
+ }
+
+ protected List<Order> getOrders() throws SQLException {
+ String sql = "SELECT * FROM t_order";
+ List<Order> result = new LinkedList<>();
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ Order order = new Order();
+ order.setOrderId(resultSet.getLong(1));
+ order.setUserId(resultSet.getInt(2));
+ order.setAddressId(resultSet.getLong(3));
+ order.setStatus(resultSet.getString(4));
+ result.add(order);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Restore the environment.
+ * @throws SQLException
+ */
+ private void cleanEnvironment() throws SQLException {
+ String dropOrderSql = "DROP TABLE t_order";
+ String dropOrderItemSql = "DROP TABLE t_order_item";
+ String dropAddressSql = "DROP TABLE t_address";
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(dropOrderSql);
+ statement.executeUpdate(dropOrderItemSql);
+ statement.executeUpdate(dropAddressSql);
+ }
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/spring-namespace-jdbc/data-model.yaml b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/spring-namespace-jdbc/data-model.yaml
new file mode 100644
index 0000000..c58817d
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/spring-namespace-jdbc/data-model.yaml
@@ -0,0 +1,22 @@
+#
+# 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.
+#
+
+mode: memory
+transaction: local
+feature: sharding
+framework: spring-namespace-jdbc
+
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
new file mode 100644
index 0000000..0c2a571
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
+ xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/context
+ http://www.springframework.org/schema/context/spring-context.xsd
+ http://shardingsphere.apache.org/schema/shardingsphere/datasource
+ http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
+ http://shardingsphere.apache.org/schema/shardingsphere/sharding
+ http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
+ ">
+ <context:annotation-config />
+ <context:component-scan base-package="org.apache.shardingsphere.example.${feature}.spring.namespace.jdbc"/>
+
+ <bean id="demo_ds_0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl" value="jdbc:mysql://localhost:3307/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="123456"/>
+ </bean>
+
+ <bean id="demo_ds_1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl" value="jdbc:mysql://localhost:3307/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="123456"/>
+ </bean>
+
+ <sharding:sharding-algorithm id="databaseAlgorithm" type="INLINE">
+ <props>
+ <prop key="algorithm-expression">demo_ds_${r'${user_id % 2}'}</prop>
+ </props>
+ </sharding:sharding-algorithm>
+ <sharding:standard-strategy id="databaseStrategy" sharding-column="user_id" algorithm-ref="databaseAlgorithm" />
+
+ <sharding:key-generate-algorithm id="snowflakeAlgorithm" type="SNOWFLAKE">
+ <props>
+ <prop key="worker-id">123</prop>
+ </props>
+ </sharding:key-generate-algorithm>
+
+ <sharding:key-generate-strategy id="orderKeyGenerator" column="order_id" algorithm-ref="snowflakeAlgorithm" />
+ <sharding:key-generate-strategy id="accountKeyGenerator" column="account_id" algorithm-ref="snowflakeAlgorithm" />
+ <sharding:key-generate-strategy id="itemKeyGenerator" column="order_item_id" algorithm-ref="snowflakeAlgorithm" />
+
+ <sharding:rule id="shardingRule">
+ <sharding:table-rules>
+ <sharding:table-rule logic-table="t_order" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="orderKeyGenerator" />
+ <sharding:table-rule logic-table="t_order_item" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="itemKeyGenerator" />
+ <sharding:table-rule logic-table="t_account" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="accountKeyGenerator" />
+ </sharding:table-rules>
+ <sharding:binding-table-rules>
+ <sharding:binding-table-rule logic-tables="t_order,t_order_item"/>
+ </sharding:binding-table-rules>
+ <sharding:broadcast-table-rules>
+ <sharding:broadcast-table-rule table="t_address"/>
+ </sharding:broadcast-table-rules>
+ </sharding:rule>
+
+ <shardingsphere:data-source id="shardingDataSource" data-source-names="demo_ds_0, demo_ds_1" rule-refs="shardingRule" />
+</beans>
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
index 9902e22..48e3286 100644
--- a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
@@ -33,6 +33,7 @@
<module>shardingsphere-jdbc-memory-local-sharding-jdbc-example</module>
<module>shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example</module>
<module>shardingsphere-jdbc-memory-local-sharding-springboot-starter-jpa-example</module>
+ <module>shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example</module>
</modules>
</project>
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/pom.xml
similarity index 69%
copy from examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
copy to examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/pom.xml
index 9902e22..5ae1499 100644
--- a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/pom.xml
@@ -20,19 +20,23 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
- <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
+ <artifactId>shardingsphere-jdbc-memory-local-sharding-example</artifactId>
<groupId>org.apache.shardingsphere.example</groupId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
- <artifactId>shardingsphere-jdbc-memory-local-sharding-example</artifactId>
+ <artifactId>shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example</artifactId>
<name>${project.artifactId}</name>
- <modules>
- <module>shardingsphere-jdbc-memory-local-sharding-jdbc-example</module>
- <module>shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example</module>
- <module>shardingsphere-jdbc-memory-local-sharding-springboot-starter-jpa-example</module>
- </modules>
-
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>5.1.5.RELEASE</version>
+ </dependency>
+ </dependencies>
</project>
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/MemoryLocalShardingSpringNamespaceJdbcExample.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example [...]
new file mode 100644
index 0000000..65cab5e
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/MemoryLocalShardingSpringNamespaceJdbcExample.java
@@ -0,0 +1,33 @@
+/*
+ * 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.sharding.spring.namespace.jdbc;
+
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import java.sql.SQLException;
+
+public class MemoryLocalShardingSpringNamespaceJdbcExample {
+
+ public static void main(final String[] args) throws SQLException {
+ try (ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml")) {
+ MemoryLocalShardingSpringNamespaceJdbcExampleService exampleService = applicationContext.getBean(MemoryLocalShardingSpringNamespaceJdbcExampleService.class);
+ exampleService.run();
+ }
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/MemoryLocalShardingSpringNamespaceJdbcExampleService.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory- [...]
new file mode 100644
index 0000000..61dc3dd
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/MemoryLocalShardingSpringNamespaceJdbcExampleService.java
@@ -0,0 +1,220 @@
+/*
+ * 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.sharding.spring.namespace.jdbc;
+
+import org.apache.shardingsphere.example.sharding.spring.namespace.jdbc.entity.Order;
+import org.apache.shardingsphere.example.sharding.spring.namespace.jdbc.entity.OrderItem;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+@Service
+public final class MemoryLocalShardingSpringNamespaceJdbcExampleService {
+
+ @Resource(name = "shardingDataSource")
+ private DataSource dataSource;
+
+ /**
+ * Execute test.
+ *
+ * @throws SQLException
+ */
+ public void run() throws SQLException {
+ try {
+ this.initEnvironment();
+ this.processSuccess();
+ } finally {
+ this.cleanEnvironment();
+ }
+ }
+
+ /**
+ * Initialize the database test environment.
+ * @throws SQLException
+ */
+ private void initEnvironment() throws SQLException {
+ String createOrderTableSql = "CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))";
+ String createOrderItemTableSql = "CREATE TABLE IF NOT EXISTS t_order_item "
+ + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id))";
+ String createAddressTableSql = "CREATE TABLE IF NOT EXISTS t_address "
+ + "(address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT NULL, PRIMARY KEY (address_id))";
+ String truncateOrderTable = "TRUNCATE TABLE t_order";
+ String truncateOrderItemTable = "TRUNCATE TABLE t_order_item";
+ String truncateAddressTableSql = "TRUNCATE TABLE t_address";
+
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(createOrderTableSql);
+ statement.executeUpdate(createOrderItemTableSql);
+ statement.executeUpdate(createAddressTableSql);
+ statement.executeUpdate(createAddressTableSql);
+ statement.executeUpdate(truncateOrderTable);
+ statement.executeUpdate(truncateOrderItemTable);
+ statement.executeUpdate(truncateAddressTableSql);
+ }
+ }
+
+ private void processSuccess() throws SQLException {
+ System.out.println("-------------- Process Success Begin ---------------");
+ List<Long> orderIds = insertData();
+ printData();
+ deleteData(orderIds);
+ printData();
+ System.out.println("-------------- Process Success Finish --------------");
+ }
+
+ private List<Long> insertData() throws SQLException {
+ System.out.println("---------------------------- Insert Data ----------------------------");
+ List<Long> result = new ArrayList<>(10);
+ for (int i = 1; i <= 10; i++) {
+ Order order = insertOrder(i);
+ insertOrderItem(i, order);
+ result.add(order.getOrderId());
+ }
+ return result;
+ }
+
+ private Order insertOrder(final int i) throws SQLException {
+ Order order = new Order();
+ order.setUserId(i);
+ order.setAddressId(i);
+ order.setStatus("INSERT_TEST");
+ String sql = "INSERT INTO t_order (user_id, address_id, status) VALUES (?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+ preparedStatement.setInt(1, order.getUserId());
+ preparedStatement.setLong(2, order.getAddressId());
+ preparedStatement.setString(3, order.getStatus());
+ preparedStatement.executeUpdate();
+ try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+ if (resultSet.next()) {
+ order.setOrderId(resultSet.getLong(1));
+ }
+ }
+ }
+ return order;
+ }
+
+ private void insertOrderItem(final int i, final Order order) throws SQLException {
+ OrderItem orderItem = new OrderItem();
+ orderItem.setOrderId(order.getOrderId());
+ orderItem.setUserId(i);
+ orderItem.setStatus("INSERT_TEST");
+ String sql = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+ preparedStatement.setLong(1, orderItem.getOrderId());
+ preparedStatement.setInt(2, orderItem.getUserId());
+ preparedStatement.setString(3, orderItem.getStatus());
+ preparedStatement.executeUpdate();
+ try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+ if (resultSet.next()) {
+ orderItem.setOrderItemId(resultSet.getLong(1));
+ }
+ }
+ }
+ }
+
+ private void deleteData(final List<Long> orderIds) throws SQLException {
+ System.out.println("---------------------------- Delete Data ----------------------------");
+ for (Long each : orderIds) {
+ String deleteOrderSql = "DELETE FROM t_order WHERE order_id=?";
+ String deleteOrderItemSql = "DELETE FROM t_order_item WHERE order_id=?";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement orderPreparedStatement = connection.prepareStatement(deleteOrderSql);
+ PreparedStatement orderItemPreparedStatement = connection.prepareStatement(deleteOrderItemSql)) {
+ orderPreparedStatement.setLong(1, each);
+ orderItemPreparedStatement.setLong(1, each);
+ orderPreparedStatement.executeUpdate();
+ orderItemPreparedStatement.executeUpdate();
+ }
+ }
+ }
+
+ private void printData() throws SQLException {
+ System.out.println("---------------------------- Print Order Data -----------------------");
+ for (Object each : this.getOrders()) {
+ System.out.println(each);
+ }
+ System.out.println("---------------------------- Print OrderItem Data -------------------");
+ for (Object each : this.getOrderItems()) {
+ System.out.println(each);
+ }
+ }
+
+ protected List<OrderItem> getOrderItems() throws SQLException {
+ String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id";
+ List<OrderItem> result = new LinkedList<>();
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ OrderItem orderItem = new OrderItem();
+ orderItem.setOrderItemId(resultSet.getLong(1));
+ orderItem.setOrderId(resultSet.getLong(2));
+ orderItem.setUserId(resultSet.getInt(3));
+ orderItem.setStatus(resultSet.getString(4));
+ result.add(orderItem);
+ }
+ }
+ return result;
+ }
+
+ protected List<Order> getOrders() throws SQLException {
+ String sql = "SELECT * FROM t_order";
+ List<Order> result = new LinkedList<>();
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ Order order = new Order();
+ order.setOrderId(resultSet.getLong(1));
+ order.setUserId(resultSet.getInt(2));
+ order.setAddressId(resultSet.getLong(3));
+ order.setStatus(resultSet.getString(4));
+ result.add(order);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Restore the environment.
+ * @throws SQLException
+ */
+ private void cleanEnvironment() throws SQLException {
+ String dropOrderSql = "DROP TABLE t_order";
+ String dropOrderItemSql = "DROP TABLE t_order_item";
+ String dropAddressSql = "DROP TABLE t_address";
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(dropOrderSql);
+ statement.executeUpdate(dropOrderItemSql);
+ statement.executeUpdate(dropAddressSql);
+ }
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/entity/Order.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local [...]
new file mode 100644
index 0000000..3962086
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/entity/Order.java
@@ -0,0 +1,70 @@
+/*
+ * 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.sharding.spring.namespace.jdbc.entity;
+
+import java.io.Serializable;
+
+public class Order implements Serializable {
+
+ private static final long serialVersionUID = 8306802022239174861L;
+
+ private long orderId;
+
+ private int userId;
+
+ private long addressId;
+
+ private String status;
+
+ public long getOrderId() {
+ return orderId;
+ }
+
+ public void setOrderId(final long orderId) {
+ this.orderId = orderId;
+ }
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(final int userId) {
+ this.userId = userId;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(final String status) {
+ this.status = status;
+ }
+
+ public long getAddressId() {
+ return addressId;
+ }
+
+ public void setAddressId(final long addressId) {
+ this.addressId = addressId;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("order_id: %s, user_id: %s, address_id: %s, status: %s", orderId, userId, addressId, status);
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/entity/OrderItem.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-l [...]
new file mode 100644
index 0000000..52ea09d
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/namespace/jdbc/entity/OrderItem.java
@@ -0,0 +1,70 @@
+/*
+ * 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.sharding.spring.namespace.jdbc.entity;
+
+import java.io.Serializable;
+
+public class OrderItem implements Serializable {
+
+ private static final long serialVersionUID = 1332162822494069342L;
+
+ private long orderItemId;
+
+ private long orderId;
+
+ private int userId;
+
+ private String status;
+
+ public long getOrderItemId() {
+ return orderItemId;
+ }
+
+ public void setOrderItemId(final long orderItemId) {
+ this.orderItemId = orderItemId;
+ }
+
+ public long getOrderId() {
+ return orderId;
+ }
+
+ public void setOrderId(final long orderId) {
+ this.orderId = orderId;
+ }
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(final int userId) {
+ this.userId = userId;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(final String status) {
+ this.status = status;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("order_item_id:%s, order_id: %s, user_id: %s, status: %s", orderItemId, orderId, userId, status);
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/resources/application.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/sha [...]
new file mode 100644
index 0000000..2378789
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-spring-namespace-jdbc-example/src/main/resources/application.xml
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
+ xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/context
+ http://www.springframework.org/schema/context/spring-context.xsd
+ http://shardingsphere.apache.org/schema/shardingsphere/datasource
+ http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
+ http://shardingsphere.apache.org/schema/shardingsphere/sharding
+ http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
+ ">
+ <context:annotation-config />
+ <context:component-scan base-package="org.apache.shardingsphere.example.sharding.spring.namespace.jdbc"/>
+
+ <bean id="demo_ds_0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl" value="jdbc:mysql://localhost:3307/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="123456"/>
+ </bean>
+
+ <bean id="demo_ds_1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl" value="jdbc:mysql://localhost:3307/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="123456"/>
+ </bean>
+
+ <sharding:sharding-algorithm id="databaseAlgorithm" type="INLINE">
+ <props>
+ <prop key="algorithm-expression">demo_ds_${user_id % 2}</prop>
+ </props>
+ </sharding:sharding-algorithm>
+ <sharding:standard-strategy id="databaseStrategy" sharding-column="user_id" algorithm-ref="databaseAlgorithm" />
+
+ <sharding:key-generate-algorithm id="snowflakeAlgorithm" type="SNOWFLAKE">
+ <props>
+ <prop key="worker-id">123</prop>
+ </props>
+ </sharding:key-generate-algorithm>
+
+ <sharding:key-generate-strategy id="orderKeyGenerator" column="order_id" algorithm-ref="snowflakeAlgorithm" />
+ <sharding:key-generate-strategy id="accountKeyGenerator" column="account_id" algorithm-ref="snowflakeAlgorithm" />
+ <sharding:key-generate-strategy id="itemKeyGenerator" column="order_item_id" algorithm-ref="snowflakeAlgorithm" />
+
+ <sharding:rule id="shardingRule">
+ <sharding:table-rules>
+ <sharding:table-rule logic-table="t_order" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="orderKeyGenerator" />
+ <sharding:table-rule logic-table="t_order_item" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="itemKeyGenerator" />
+ <sharding:table-rule logic-table="t_account" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="accountKeyGenerator" />
+ </sharding:table-rules>
+ <sharding:binding-table-rules>
+ <sharding:binding-table-rule logic-tables="t_order,t_order_item"/>
+ </sharding:binding-table-rules>
+ <sharding:broadcast-table-rules>
+ <sharding:broadcast-table-rule table="t_address"/>
+ </sharding:broadcast-table-rules>
+ </sharding:rule>
+
+ <shardingsphere:data-source id="shardingDataSource" data-source-names="demo_ds_0, demo_ds_1" rule-refs="shardingRule" />
+</beans>