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/11/22 01:23:04 UTC
[shardingsphere] branch master updated: Add encrypt jdbc example and template (#13721)
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 4d4f674 Add encrypt jdbc example and template (#13721)
4d4f674 is described below
commit 4d4f674049cb8acafbdc9ba6c254160c90509d92
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Mon Nov 22 09:21:49 2021 +0800
Add encrypt jdbc example and template (#13721)
---
.../sharding/example/engine/JDBCGenerator.java | 3 +
.../main/resources/dataModel/jdbc/data-model.yaml | 2 +-
.../src/main/resources/template/entity/User.ftl | 60 +++++++
.../main/resources/template/jdbc/Configuration.ftl | 10 +-
.../resources/template/jdbc/ExampleService.ftl | 181 +--------------------
.../template/jdbc/encryptConfiguration.ftl | 39 +++++
.../template/jdbc/encryptExampleService.ftl | 115 +++++++++++++
...{ExampleService.ftl => otherExampleService.ftl} | 50 ------
.../src/main/resources/template/log/logback.ftl} | 33 ++--
.../pom.xml | 1 +
.../pom.xml | 8 +-
.../pom.xml | 17 +-
.../jdbc/MemoryLocalEncryptJdbcConfiguration.java | 75 +++++++++
.../jdbc/MemoryLocalEncryptJdbcExample.java | 32 ++++
.../jdbc/MemoryLocalEncryptJdbcExampleService.java | 151 +++++++++++++++++
.../example/encrypt/jdbc/entity/Order.java | 70 ++++++++
.../example/encrypt/jdbc/entity/OrderItem.java | 70 ++++++++
.../example/encrypt/jdbc/entity/User.java | 60 +++++++
.../src/main/resources/logback.xml} | 33 ++--
19 files changed, 741 insertions(+), 269 deletions(-)
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/JDBCGenerator.java b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/JDBCGenerator.java
index 0be2fcb..f89a2df 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/JDBCGenerator.java
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/JDBCGenerator.java
@@ -36,6 +36,9 @@ public final class JDBCGenerator extends ExampleGenerateEngine {
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("entity/User", "entity/User.java");
+
+ RESOURCE_TEMPLATE_MAP.put("log/logback", "logback.xml");
}
public JDBCGenerator() {
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml
index 792c10b..5bdd944 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml
@@ -17,7 +17,7 @@
mode: memory
transaction: local
-feature: readwrite-splitting
+feature: encrypt
framework: jdbc
host: localhost
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/entity/User.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/entity/User.ftl
new file mode 100644
index 0000000..f7e0660
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/entity/User.ftl
@@ -0,0 +1,60 @@
+/*
+ * 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?replace('-', '.')}.${framework?replace('-', '.')}.entity;
+
+import java.io.Serializable;
+
+public class User implements Serializable {
+
+ private static final long serialVersionUID = 263434701950670170L;
+
+ private int userId;
+
+ private String userName;
+
+ private String pwd;
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(final int userId) {
+ this.userId = userId;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(final String userName) {
+ this.userName = userName;
+ }
+
+ public String getPwd() {
+ return pwd;
+ }
+
+ public void setPwd(final String pwd) {
+ this.pwd = pwd;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("user_id: %d, user_name: %s, pwd: %s", userId, userName, pwd);
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl
index 69eb142..1245145 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl
@@ -32,6 +32,12 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardS
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
</#if>
+<#if feature=="encrypt">
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+</#if>
import javax.sql.DataSource;
import java.sql.SQLException;
@@ -60,7 +66,9 @@ public final class ${mode?cap_first}${transaction?cap_first}${featureName}${fram
<#if feature=="readwrite-splitting">
<#include "readwritesplittingConfiguration.ftl">
</#if>
-
+<#if feature=="encrypt">
+ <#include "encryptConfiguration.ftl">
+</#if>
private DataSource createDataSource(final String dataSourceName) {
HikariDataSource result = new HikariDataSource();
result.setDriverClassName("com.mysql.jdbc.Driver");
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
index eaa0d76..63946b9 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
@@ -18,8 +18,12 @@
package org.apache.shardingsphere.example.${feature?replace('-', '.')}.${framework?replace('-', '.')};
import lombok.AllArgsConstructor;
+<#if feature=="encrypt">
+import org.apache.shardingsphere.example.${feature?replace('-', '.')}.${framework?replace('-', '.')}.entity.User;
+<#else>
import org.apache.shardingsphere.example.${feature?replace('-', '.')}.${framework?replace('-', '.')}.entity.Order;
import org.apache.shardingsphere.example.${feature?replace('-', '.')}.${framework?replace('-', '.')}.entity.OrderItem;
+</#if>
<#if framework?contains("spring")>
import org.springframework.stereotype.Service;
</#if>
@@ -63,177 +67,10 @@ public final class ${mode?cap_first}${transaction?cap_first}${featureName}${fram
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);
- }
- }
+<#if feature=="encrypt">
+ <#include "encryptExampleService.ftl">
+<#else>
+ <#include "otherExampleService.ftl">
+</#if>
}
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptConfiguration.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptConfiguration.ftl
new file mode 100644
index 0000000..23c4eca
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptConfiguration.ftl
@@ -0,0 +1,39 @@
+<#--
+ ~ 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.
+ -->
+
+ /**
+ * Create a DataSource object, which is an object rewritten by ShardingSphere itself
+ * and contains various rules for rewriting the original data storage. When in use, you only need to use this object.
+ * @return
+ * @throws SQLException
+ */
+ public DataSource getDataSource() throws SQLException {
+ return ShardingSphereDataSourceFactory.createDataSource(createDataSource("demo_ds_0"), Collections.singleton(createEncryptRuleConfiguration()), new Properties());
+ }
+
+ private EncryptRuleConfiguration createEncryptRuleConfiguration() {
+ Properties props = new Properties();
+ props.setProperty("aes-key-value", "");
+ EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("user_id", "user_id", null, null, "name_encryptor");
+ EncryptColumnRuleConfiguration columnConfigTest = new EncryptColumnRuleConfiguration("address_id", "address_id", null, null, "pwd_encryptor");
+ EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_order", Arrays.asList(columnConfigAes, columnConfigTest), true);
+ Map<String, ShardingSphereAlgorithmConfiguration> encryptAlgorithmConfigs = new LinkedHashMap<>(2, 1);
+ encryptAlgorithmConfigs.put("name_encryptor", new ShardingSphereAlgorithmConfiguration("AES", props));
+ encryptAlgorithmConfigs.put("pwd_encryptor", new ShardingSphereAlgorithmConfiguration("AES", props));
+ return new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), encryptAlgorithmConfigs);
+ }
+
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl
new file mode 100644
index 0000000..5d9d28d
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl
@@ -0,0 +1,115 @@
+<#--
+ ~ 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.
+ -->
+
+ /**
+ * Initialize the database test environment.
+ * @throws SQLException
+ */
+ private void initEnvironment() throws SQLException {
+ String createUserTableSql = "CREATE TABLE IF NOT EXISTS t_user"
+ + "(user_id INT NOT NULL AUTO_INCREMENT, user_name VARCHAR(200), pwd VARCHAR(200), PRIMARY KEY (user_id))";
+ String truncateUserTable = "TRUNCATE TABLE t_user";
+
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(createUserTableSql);
+ statement.executeUpdate(truncateUserTable);
+ }
+ }
+
+ private void processSuccess() throws SQLException {
+ System.out.println("-------------- Process Success Begin ---------------");
+ List<Long> ids = insertData();
+ printData();
+ deleteData(ids);
+ 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++) {
+ User user = new User();
+ user.setUserId(i);
+ user.setUserName("test_" + i);
+ user.setPwd("pwd" + i);
+ insert(user);
+ result.add((long) user.getUserId());
+ }
+ return result;
+ }
+
+ private long insert(final User user) throws SQLException {
+ String sql = "INSERT INTO t_user (user_id, user_name, pwd) VALUES (?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
+ preparedStatement.setInt(1, user.getUserId());
+ preparedStatement.setString(2, user.getUserName());
+ preparedStatement.setString(3, user.getPwd());
+ preparedStatement.executeUpdate();
+ }
+ return user.getUserId();
+ }
+
+ private void deleteData(final List<Long> orderIds) throws SQLException {
+ System.out.println("---------------------------- Delete Data ----------------------------");
+ String sql = "DELETE FROM t_user WHERE user_id=?";
+ for (Long each : orderIds) {
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
+ preparedStatement.setLong(1, each);
+ preparedStatement.executeUpdate();
+ }
+ }
+ }
+
+ private void printData() throws SQLException {
+ System.out.println("---------------------------- Print User Data -----------------------");
+ for (Object each : this.getUsers()) {
+ System.out.println(each);
+ }
+ }
+
+ protected List<User> getUsers() throws SQLException {
+ List<User> result = new LinkedList<>();
+ String sql = "SELECT * FROM t_user";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ User user = new User();
+ user.setUserId(resultSet.getInt("user_id"));
+ user.setUserName(resultSet.getString("user_name"));
+ user.setPwd(resultSet.getString("pwd"));
+ result.add(user);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Restore the environment.
+ * @throws SQLException
+ */
+ private void cleanEnvironment() throws SQLException {
+ String dropUserSql = "DROP TABLE t_user";
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(dropUserSql);
+ }
+ }
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/otherExampleService.ftl
similarity index 85%
copy from examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
copy to examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/otherExampleService.ftl
index eaa0d76..e68b044 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/otherExampleService.ftl
@@ -14,55 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-package org.apache.shardingsphere.example.${feature?replace('-', '.')}.${framework?replace('-', '.')};
-
-import lombok.AllArgsConstructor;
-import org.apache.shardingsphere.example.${feature?replace('-', '.')}.${framework?replace('-', '.')}.entity.Order;
-import org.apache.shardingsphere.example.${feature?replace('-', '.')}.${framework?replace('-', '.')}.entity.OrderItem;
-<#if framework?contains("spring")>
-import org.springframework.stereotype.Service;
-</#if>
-
-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>
-<#assign featureName="">
-<#list feature?split("-") as feature1>
- <#assign featureName=featureName + feature1?cap_first>
-</#list>
-<#if framework?contains("spring")>
-@Service
-</#if>
-@AllArgsConstructor
-public final class ${mode?cap_first}${transaction?cap_first}${featureName}${frameworkName}ExampleService {
-
- 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.
@@ -236,4 +187,3 @@ public final class ${mode?cap_first}${transaction?cap_first}${featureName}${fram
statement.executeUpdate(dropAddressSql);
}
}
-}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/log/logback.ftl
similarity index 51%
copy from examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
copy to examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/log/logback.ftl
index 83f067a..47fc78d 100644
--- a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/log/logback.ftl
@@ -16,20 +16,19 @@
~ 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-example</artifactId>
- <groupId>org.apache.shardingsphere.example</groupId>
- <version>5.0.1-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
- <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
- <name>${project.artifactId}</name>
- <modules>
- <module>shardingsphere-jdbc-memory-local-sharding-example</module>
- <module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
- </modules>
-</project>
+<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>${r"${log.context.name}"}</contextName>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder charset="${r"${log.charset}"}">
+ <pattern>${r"${log.pattern}"}</pattern>
+ </encoder>
+ </appender>
+ <root>
+ <level value="INFO" />
+ <appender-ref ref="STDOUT" />
+ </root>
+</configuration>
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
index 83f067a..ef58733 100644
--- a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
@@ -31,5 +31,6 @@
<modules>
<module>shardingsphere-jdbc-memory-local-sharding-example</module>
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
+ <module>shardingsphere-jdbc-memory-local-encrypt-example</module>
</modules>
</project>
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/pom.xml
similarity index 81%
copy from examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
copy to examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/pom.xml
index 83f067a..b760b0d 100644
--- a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/pom.xml
@@ -20,16 +20,16 @@
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-example</artifactId>
+ <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
<groupId>org.apache.shardingsphere.example</groupId>
<version>5.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
- <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
+ <artifactId>shardingsphere-jdbc-memory-local-encrypt-example</artifactId>
<name>${project.artifactId}</name>
+
<modules>
- <module>shardingsphere-jdbc-memory-local-sharding-example</module>
- <module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
+ <module>shardingsphere-jdbc-memory-local-encrypt-jdbc-example</module>
</modules>
</project>
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/pom.xml
similarity index 77%
copy from examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
copy to examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/pom.xml
index 83f067a..d077da7 100644
--- a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/pom.xml
@@ -20,16 +20,19 @@
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-example</artifactId>
+ <artifactId>shardingsphere-jdbc-memory-local-encrypt-example</artifactId>
<groupId>org.apache.shardingsphere.example</groupId>
<version>5.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
- <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
+ <artifactId>shardingsphere-jdbc-memory-local-encrypt-jdbc-example</artifactId>
<name>${project.artifactId}</name>
- <modules>
- <module>shardingsphere-jdbc-memory-local-sharding-example</module>
- <module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
- </modules>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core</artifactId>
+ </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-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/MemoryLocalEncryptJdbcConfiguration.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shard [...]
new file mode 100644
index 0000000..ef7e87b
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/MemoryLocalEncryptJdbcConfiguration.java
@@ -0,0 +1,75 @@
+/*
+ * 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.encrypt.jdbc;
+
+import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
+
+public final class MemoryLocalEncryptJdbcConfiguration {
+
+ private static final String HOST = "localhost";
+
+ private static final int PORT = 3306;
+
+ private static final String USER_NAME = "root";
+
+ private static final String PASSWORD = "123456";
+
+ /**
+ * Create a DataSource object, which is an object rewritten by ShardingSphere itself
+ * and contains various rules for rewriting the original data storage. When in use, you only need to use this object.
+ * @return
+ * @throws SQLException
+ */
+ public DataSource getDataSource() throws SQLException {
+ return ShardingSphereDataSourceFactory.createDataSource(createDataSource("demo_ds_0"), Collections.singleton(createEncryptRuleConfiguration()), new Properties());
+ }
+
+ private EncryptRuleConfiguration createEncryptRuleConfiguration() {
+ Properties props = new Properties();
+ props.setProperty("aes-key-value", "");
+ EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("user_id", "user_id", null, null, "name_encryptor");
+ EncryptColumnRuleConfiguration columnConfigTest = new EncryptColumnRuleConfiguration("address_id", "address_id", null, null, "pwd_encryptor");
+ EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_order", Arrays.asList(columnConfigAes, columnConfigTest), true);
+ Map<String, ShardingSphereAlgorithmConfiguration> encryptAlgorithmConfigs = new LinkedHashMap<>(2, 1);
+ encryptAlgorithmConfigs.put("name_encryptor", new ShardingSphereAlgorithmConfiguration("AES", props));
+ encryptAlgorithmConfigs.put("pwd_encryptor", new ShardingSphereAlgorithmConfiguration("AES", props));
+ return new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), encryptAlgorithmConfigs);
+ }
+
+ private DataSource createDataSource(final String dataSourceName) {
+ HikariDataSource result = new HikariDataSource();
+ result.setDriverClassName("com.mysql.jdbc.Driver");
+ result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8", HOST, PORT, dataSourceName));
+ result.setUsername(USER_NAME);
+ result.setPassword(PASSWORD);
+ return result;
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/MemoryLocalEncryptJdbcExample.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsph [...]
new file mode 100644
index 0000000..eb09130
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/MemoryLocalEncryptJdbcExample.java
@@ -0,0 +1,32 @@
+/*
+ * 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.encrypt.jdbc;
+
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class MemoryLocalEncryptJdbcExample {
+
+ public static void main(final String[] args) throws SQLException {
+ MemoryLocalEncryptJdbcConfiguration shardingConfiguration = new MemoryLocalEncryptJdbcConfiguration();
+ DataSource dataSource = shardingConfiguration.getDataSource();
+ MemoryLocalEncryptJdbcExampleService exampleService = new MemoryLocalEncryptJdbcExampleService(dataSource);
+ exampleService.run();
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/MemoryLocalEncryptJdbcExampleService.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shar [...]
new file mode 100644
index 0000000..73dbc05
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/MemoryLocalEncryptJdbcExampleService.java
@@ -0,0 +1,151 @@
+/*
+ * 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.encrypt.jdbc;
+
+import lombok.AllArgsConstructor;
+import org.apache.shardingsphere.example.encrypt.jdbc.entity.User;
+
+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;
+
+@AllArgsConstructor
+public final class MemoryLocalEncryptJdbcExampleService {
+
+ 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 createUserTableSql = "CREATE TABLE IF NOT EXISTS t_user"
+ + "(user_id INT NOT NULL AUTO_INCREMENT, user_name VARCHAR(200), pwd VARCHAR(200), PRIMARY KEY (user_id))";
+ String truncateUserTable = "TRUNCATE TABLE t_user";
+
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(createUserTableSql);
+ statement.executeUpdate(truncateUserTable);
+ }
+ }
+
+ private void processSuccess() throws SQLException {
+ System.out.println("-------------- Process Success Begin ---------------");
+ List<Long> ids = insertData();
+ printData();
+ deleteData(ids);
+ 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++) {
+ User user = new User();
+ user.setUserId(i);
+ user.setUserName("test_" + i);
+ user.setPwd("pwd" + i);
+ insert(user);
+ result.add((long) user.getUserId());
+ }
+ return result;
+ }
+
+ private long insert(final User user) throws SQLException {
+ String sql = "INSERT INTO t_user (user_id, user_name, pwd) VALUES (?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
+ preparedStatement.setInt(1, user.getUserId());
+ preparedStatement.setString(2, user.getUserName());
+ preparedStatement.setString(3, user.getPwd());
+ preparedStatement.executeUpdate();
+ }
+ return user.getUserId();
+ }
+
+ private void deleteData(final List<Long> orderIds) throws SQLException {
+ System.out.println("---------------------------- Delete Data ----------------------------");
+ String sql = "DELETE FROM t_user WHERE user_id=?";
+ for (Long each : orderIds) {
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
+ preparedStatement.setLong(1, each);
+ preparedStatement.executeUpdate();
+ }
+ }
+ }
+
+ private void printData() throws SQLException {
+ System.out.println("---------------------------- Print User Data -----------------------");
+ for (Object each : this.getUsers()) {
+ System.out.println(each);
+ }
+ }
+
+ protected List<User> getUsers() throws SQLException {
+ List<User> result = new LinkedList<>();
+ String sql = "SELECT * FROM t_user";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ User user = new User();
+ user.setUserId(resultSet.getInt("user_id"));
+ user.setUserName(resultSet.getString("user_name"));
+ user.setPwd(resultSet.getString("pwd"));
+ result.add(user);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Restore the environment.
+ * @throws SQLException
+ */
+ private void cleanEnvironment() throws SQLException {
+ String dropUserSql = "DROP TABLE t_user";
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(dropUserSql);
+ }
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/entity/Order.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-l [...]
new file mode 100644
index 0000000..cfa6ec2
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/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.encrypt.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-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/entity/OrderItem.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memo [...]
new file mode 100644
index 0000000..ac4a78c
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/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.encrypt.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-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/entity/User.java b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-lo [...]
new file mode 100644
index 0000000..34066d9
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/java/org/apache/shardingsphere/example/encrypt/jdbc/entity/User.java
@@ -0,0 +1,60 @@
+/*
+ * 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.encrypt.jdbc.entity;
+
+import java.io.Serializable;
+
+public class User implements Serializable {
+
+ private static final long serialVersionUID = 263434701950670170L;
+
+ private int userId;
+
+ private String userName;
+
+ private String pwd;
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(final int userId) {
+ this.userId = userId;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(final String userName) {
+ this.userName = userName;
+ }
+
+ public String getPwd() {
+ return pwd;
+ }
+
+ public void setPwd(final String pwd) {
+ this.pwd = pwd;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("user_id: %d, user_name: %s, pwd: %s", userId, userName, pwd);
+ }
+}
diff --git a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/resources/logback.xml
similarity index 51%
copy from examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
copy to examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/resources/logback.xml
index 83f067a..154abba 100644
--- a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-encrypt-example/shardingsphere-jdbc-memory-local-encrypt-jdbc-example/src/main/resources/logback.xml
@@ -16,20 +16,19 @@
~ 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-example</artifactId>
- <groupId>org.apache.shardingsphere.example</groupId>
- <version>5.0.1-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
- <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
- <name>${project.artifactId}</name>
- <modules>
- <module>shardingsphere-jdbc-memory-local-sharding-example</module>
- <module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
- </modules>
-</project>
+<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>