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/12/23 15:14:15 UTC
[shardingsphere] branch master updated: Add spring boot starter jdbc example and template (#14274)
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 c497cb4 Add spring boot starter jdbc example and template (#14274)
c497cb4 is described below
commit c497cb4d2be1770c64d1a46040c6064d66ad245b
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Thu Dec 23 23:13:29 2021 +0800
Add spring boot starter jdbc example and template (#14274)
---
.../resources/template/properties/application.ftl | 21 +-
.../template/properties/dbDiscoveryApplication.ftl | 50 +++++
.../template/properties/encryptApplication.ftl | 3 +
.../properties/readwriteSplittingApplication.ftl | 3 +
.../template/properties/shadowApplication.ftl | 3 +
.../template/properties/shardingApplication.ftl | 4 +
.../pom.xml | 48 +++++
...calDbDiscoverySpringBootStarterJdbcExample.java | 34 +++
...scoverySpringBootStarterJdbcExampleService.java | 227 +++++++++++++++++++++
.../spring/boot/starter/jdbc/entity/Order.java | 70 +++++++
.../spring/boot/starter/jdbc/entity/OrderItem.java | 70 +++++++
.../src/main/resources/application.properties | 50 +++++
.../src/main/resources/logback.xml} | 27 ++-
13 files changed, 587 insertions(+), 23 deletions(-)
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/application.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/application.ftl
index 19662fa..add3d2f 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/application.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/application.ftl
@@ -14,16 +14,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-
<#if framework?contains("mybatis")>
-mybatis.mapper-locations=classpath*:mappers/*Mapper.xml
+mybatis.mapper-locations=classpath*:mappers/*Mapper.xml
<#elseif framework?contains("jpa")>
+
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.show_sql=false
-
</#if>
+<#if feature!="db-discovery">
+
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
@@ -37,23 +38,15 @@ spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3306/demo_
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=root
spring.shardingsphere.datasource.ds-1.max-active=16
-
+</#if>
<#if feature=="sharding">
-spring.shardingsphere.schema.name=sharding
-
-spring.shardingsphere.datasource.names=ds-0,ds-1
-
<#include "shardingApplication.ftl">
<#elseif feature=="readwrite-splitting">
-spring.shardingsphere.datasource.names=ds-0,ds-1,ds-2
-
<#include "readwriteSplittingApplication.ftl">
<#elseif feature=="encrypt">
-spring.shardingsphere.datasource.names=ds-0
-
<#include "encryptApplication.ftl">
<#elseif feature=="shadow">
-spring.shardingsphere.datasource.names=ds-0,ds-1
-
<#include "shadowApplication.ftl">
+<#elseif feature=="db-discovery">
+ <#include "dbDiscoveryApplication.ftl">
</#if>
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/dbDiscoveryApplication.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/dbDiscoveryApplication.ftl
new file mode 100644
index 0000000..f95d188
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/dbDiscoveryApplication.ftl
@@ -0,0 +1,50 @@
+<#--
+ ~ 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.
+ -->
+
+spring.shardingsphere.datasource.names=ds-0,ds-0-replica-0,ds-0-replica-1
+
+spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
+spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
+spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://172.72.0.15:3306/ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-0.username=root
+spring.shardingsphere.datasource.ds-0.password=root
+spring.shardingsphere.datasource.ds-0.max-active=16
+
+spring.shardingsphere.datasource.ds-0-replica-0.type=com.zaxxer.hikari.HikariDataSource
+spring.shardingsphere.datasource.ds-0-replica-0.driver-class-name=com.mysql.jdbc.Driver
+spring.shardingsphere.datasource.ds-0-replica-0.jdbc-url=jdbc:mysql://172.72.0.16:3306/ds_0_replica_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-0-replica-0.username=root
+spring.shardingsphere.datasource.ds-0-replica-0.password=root
+spring.shardingsphere.datasource.ds-0-replica-0.max-active=16
+
+spring.shardingsphere.datasource.ds-0-replica-1.type=com.zaxxer.hikari.HikariDataSource
+spring.shardingsphere.datasource.ds-0-replica-1.driver-class-name=com.mysql.jdbc.Driver
+spring.shardingsphere.datasource.ds-0-replica-1.jdbc-url=jdbc:mysql://172.72.0.17:3306/ds_0_replica_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-0-replica-1.username=root
+spring.shardingsphere.datasource.ds-0-replica-1.password=root
+spring.shardingsphere.datasource.ds-0-replica-1.max-active=16
+
+spring.shardingsphere.rules.database-discovery.data-sources.rule.data-source-names=ds-0,ds-0-replica-0,ds-0-replica-1
+spring.shardingsphere.rules.database-discovery.data-sources.rule.discovery-heartbeat-name=mgr-heartbeat
+spring.shardingsphere.rules.database-discovery.data-sources.rule.discovery-type-name=mgr
+spring.shardingsphere.rules.database-discovery.discovery-heartbeats.mgr-heartbeat.props.keep-alive-cron=0/5 * * * * ?
+spring.shardingsphere.rules.database-discovery.discovery-types.mgr.type=MGR
+spring.shardingsphere.rules.database-discovery.discovery-types.mgr.props.keep-alive-cron=0/5 * * * * ?
+spring.shardingsphere.rules.database-discovery.discovery-types.mgr.props.group-name=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
+
+
+
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/encryptApplication.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/encryptApplication.ftl
index 5a10026..e9bb4f2 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/encryptApplication.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/encryptApplication.ftl
@@ -14,6 +14,9 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
+
+spring.shardingsphere.datasource.names=ds-0
+
spring.shardingsphere.rules.encrypt.encryptors.name-encryptor.type=AES
spring.shardingsphere.rules.encrypt.encryptors.name-encryptor.props.aes-key-value=123456abc
spring.shardingsphere.rules.encrypt.encryptors.pwd-encryptor.type=assistedTest
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/readwriteSplittingApplication.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/readwriteSplittingApplication.ftl
index 5ab4d40..0b4045d 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/readwriteSplittingApplication.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/readwriteSplittingApplication.ftl
@@ -14,6 +14,9 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
+
+spring.shardingsphere.datasource.names=ds-0,ds-1,ds-2
+
spring.shardingsphere.datasource.ds-2.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_2?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-2.driver-class-name=com.mysql.jdbc.Driver
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shadowApplication.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shadowApplication.ftl
index 020d007..6d86c12 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shadowApplication.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shadowApplication.ftl
@@ -14,6 +14,9 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
+
+spring.shardingsphere.datasource.names=ds-0,ds-1
+
spring.shardingsphere.rules.shadow.enable=true
spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.source-data-source-name=ds-0
spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.shadow-data-source-name=ds-1
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shardingApplication.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shardingApplication.ftl
index bcedd9b..f4267a0 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shardingApplication.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/shardingApplication.ftl
@@ -14,6 +14,10 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
+
+spring.shardingsphere.schema.name=sharding
+spring.shardingsphere.datasource.names=ds-0,ds-1
+
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=database-inline
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere- [...]
new file mode 100644
index 0000000..597a3e8
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/pom.xml
@@ -0,0 +1,48 @@
+<?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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ 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-db-discovery-example</artifactId>
+ <groupId>org.apache.shardingsphere.example</groupId>
+ <version>${revision}</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example</artifactId>
+ <name>${project.artifactId}</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot</artifactId>
+ <version>2.2.0.RELEASE</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-autoconfigure</artifactId>
+ <version>2.2.0.RELEASE</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/jdbc/MemoryLocalDbDiscoverySpringBootStarterJdbcExample.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsph [...]
new file mode 100644
index 0000000..7ed2a4c
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/jdbc/MemoryLocalDbDiscoverySpringBootStarterJdbcExample.java
@@ -0,0 +1,34 @@
+/*
+ * 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.db.discovery.spring.boot.starter.jdbc;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ConfigurableApplicationContext;
+import java.sql.SQLException;
+
+@SpringBootApplication
+public class MemoryLocalDbDiscoverySpringBootStarterJdbcExample {
+
+ public static void main(final String[] args) throws SQLException {
+ try (ConfigurableApplicationContext applicationContext = SpringApplication.run(MemoryLocalDbDiscoverySpringBootStarterJdbcExample.class, args)) {
+ MemoryLocalDbDiscoverySpringBootStarterJdbcExampleService exampleService = applicationContext.getBean(MemoryLocalDbDiscoverySpringBootStarterJdbcExampleService.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-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/jdbc/MemoryLocalDbDiscoverySpringBootStarterJdbcExampleService.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shar [...]
new file mode 100644
index 0000000..9f5b4ec
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/jdbc/MemoryLocalDbDiscoverySpringBootStarterJdbcExampleService.java
@@ -0,0 +1,227 @@
+/*
+ * 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.db.discovery.spring.boot.starter.jdbc;
+
+import lombok.AllArgsConstructor;
+import org.apache.shardingsphere.example.db.discovery.spring.boot.starter.jdbc.entity.Order;
+import org.apache.shardingsphere.example.db.discovery.spring.boot.starter.jdbc.entity.OrderItem;
+import org.springframework.stereotype.Service;
+
+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
+@AllArgsConstructor
+public final class MemoryLocalDbDiscoverySpringBootStarterJdbcExampleService {
+
+ private final 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 void processFailure() throws SQLException {
+ System.out.println("-------------- Process Failure Begin ---------------");
+ insertData();
+ System.out.println("-------------- Process Failure Finish --------------");
+ throw new RuntimeException("Exception occur for transaction test.");
+ }
+
+ 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-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/jdbc/entity/Order.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere [...]
new file mode 100644
index 0000000..8a09f72
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/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.db.discovery.spring.boot.starter.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-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/jdbc/entity/OrderItem.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsp [...]
new file mode 100644
index 0000000..20ff0cce
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/boot/starter/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.db.discovery.spring.boot.starter.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-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/resources/application.properties b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db [...]
new file mode 100644
index 0000000..5266b16
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/resources/application.properties
@@ -0,0 +1,50 @@
+#
+# 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.
+#
+
+spring.shardingsphere.datasource.names=ds-0,ds-0-replica-0,ds-0-replica-1
+
+spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
+spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
+spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://172.72.0.15:3306/ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-0.username=root
+spring.shardingsphere.datasource.ds-0.password=root
+spring.shardingsphere.datasource.ds-0.max-active=16
+
+spring.shardingsphere.datasource.ds-0-replica-0.type=com.zaxxer.hikari.HikariDataSource
+spring.shardingsphere.datasource.ds-0-replica-0.driver-class-name=com.mysql.jdbc.Driver
+spring.shardingsphere.datasource.ds-0-replica-0.jdbc-url=jdbc:mysql://172.72.0.16:3306/ds_0_replica_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-0-replica-0.username=root
+spring.shardingsphere.datasource.ds-0-replica-0.password=root
+spring.shardingsphere.datasource.ds-0-replica-0.max-active=16
+
+spring.shardingsphere.datasource.ds-0-replica-1.type=com.zaxxer.hikari.HikariDataSource
+spring.shardingsphere.datasource.ds-0-replica-1.driver-class-name=com.mysql.jdbc.Driver
+spring.shardingsphere.datasource.ds-0-replica-1.jdbc-url=jdbc:mysql://172.72.0.17:3306/ds_0_replica_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-0-replica-1.username=root
+spring.shardingsphere.datasource.ds-0-replica-1.password=root
+spring.shardingsphere.datasource.ds-0-replica-1.max-active=16
+
+spring.shardingsphere.rules.database-discovery.data-sources.rule.data-source-names=ds-0,ds-0-replica-0,ds-0-replica-1
+spring.shardingsphere.rules.database-discovery.data-sources.rule.discovery-heartbeat-name=mgr-heartbeat
+spring.shardingsphere.rules.database-discovery.data-sources.rule.discovery-type-name=mgr
+spring.shardingsphere.rules.database-discovery.discovery-heartbeats.mgr-heartbeat.props.keep-alive-cron=0/5 * * * * ?
+spring.shardingsphere.rules.database-discovery.discovery-types.mgr.type=MGR
+spring.shardingsphere.rules.database-discovery.discovery-types.mgr.props.keep-alive-cron=0/5 * * * * ?
+spring.shardingsphere.rules.database-discovery.discovery-types.mgr.props.group-name=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
+
+
+
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/encryptApplication.ftl b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/resources/logback.xml
similarity index 53%
copy from examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/encryptApplication.ftl
copy to examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/resources/logback.xml
index 5a10026..154abba 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/properties/encryptApplication.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example/src/main/resources/logback.xml
@@ -1,4 +1,5 @@
-<#--
+<?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.
@@ -14,12 +15,20 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
-spring.shardingsphere.rules.encrypt.encryptors.name-encryptor.type=AES
-spring.shardingsphere.rules.encrypt.encryptors.name-encryptor.props.aes-key-value=123456abc
-spring.shardingsphere.rules.encrypt.encryptors.pwd-encryptor.type=assistedTest
-spring.shardingsphere.rules.encrypt.tables.t_user.columns.user_name.cipher-column=user_name
-spring.shardingsphere.rules.encrypt.tables.t_user.columns.user_name.encryptor-name=name-encryptor
-spring.shardingsphere.rules.encrypt.tables.t_user.columns.pwd.cipher-column=pwd
-spring.shardingsphere.rules.encrypt.tables.t_user.columns.pwd.encryptor-name=pwd-encryptor
-spring.shardingsphere.rules.encrypt.tables.t_user.columns.pwd.assisted-query-column=assisted_query_pwd
+<configuration>
+ <property name="log.context.name" value="shardingsphere-example" />
+ <property name="log.charset" value="UTF-8" />
+ <property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] %msg %n" />
+ <contextName>${log.context.name}</contextName>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder charset="${log.charset}">
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ </appender>
+ <root>
+ <level value="INFO" />
+ <appender-ref ref="STDOUT" />
+ </root>
+</configuration>