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 a972301 Add springboot-starter-jdbc template (#13050)
a972301 is described below
commit a972301a40f627b766b146d405cf763241c1d198
Author: Guocheng Tang <[email protected]>
AuthorDate: Fri Oct 15 10:08:49 2021 +0800
Add springboot-starter-jdbc template (#13050)
* Add spring-boot-jdbc example module
* update jdbc template.
* Add springboot-starter-jdbc template
* move springboot starter jdbc module
---
.../example/engine/ExampleGenerateEngine.java | 14 +++--
.../src/main/resources/template/jdbc/Example.ftl | 7 +--
.../resources/template/jdbc/ExampleService.ftl | 24 ++++++--
.../resources/{ => template/jdbc}/datamodel.yaml | 0
.../{jdbc => springboot-starter-jdbc}/Example.ftl | 25 ++++----
.../ExampleService.ftl | 34 ++++++++---
.../springboot-starter-jdbc}/datamodel.yaml | 2 +-
.../pom.xml | 6 ++
.../jdbc/MemoryLocalShardingJdbcExample.java | 7 +--
.../MemoryLocalShardingJdbcExampleService.java | 24 ++++++--
.../pom.xml | 29 +++++++--
...LocalShardingSpringbootStarterJdbcExample.java} | 21 ++++---
...ardingSpringbootStarterJdbcExampleService.java} | 30 +++++++---
.../src/main/resources/application.properties | 69 ++++++++++++++++++++++
14 files changed, 224 insertions(+), 68 deletions(-)
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
index ba6df4c..0bfe8b4 100644
---
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
+++
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
@@ -40,16 +40,20 @@ public final class ExampleGenerateEngine {
private static final String OUTPUT_PATH =
"./examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-${mode}-example"
+
"/shardingsphere-jdbc-${mode}-${transaction}-example/shardingsphere-jdbc-${mode}-${transaction}-${feature}-example"
- +
"/shardingsphere-jdbc-${mode}-${transaction}-${feature}-${framework}-example/src/main/java/org/apache/shardingsphere/example/${feature}/${framework}";
+ +
"/shardingsphere-jdbc-${mode}-${transaction}-${feature}-${framework}-example/src/main/java/org/apache/shardingsphere/example/${feature}/${framework?replace('-',
'/')}";
- private static final String FILE_NAME_PREFIX =
"${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}";
+ private static final String FILE_NAME_PREFIX =
"${mode?cap_first}${transaction?cap_first}${feature?cap_first}" +
+ "<#assign frameworkName=\"\">" +
+ "<#list framework?split(\"-\") as framework1>" +
+ "<#assign frameworkName=frameworkName + framework1?cap_first>" +
+ "</#list>${frameworkName}";
private static final Map<String, String> TEMPLATE_MAP = new HashMap(4, 1);
static {
try {
CONFIGURATION.setDirectoryForTemplateLoading(new
File(ExampleGenerateEngine.class.getClassLoader().getResource("").getFile()));
CONFIGURATION.setDefaultEncoding("UTF-8");
- TEMPLATE_MAP.put("Configuration", "Configuration.ftl");
+ //TEMPLATE_MAP.put("Configuration", "Configuration.ftl");
TEMPLATE_MAP.put("Example", "Example.ftl");
TEMPLATE_MAP.put("ExampleService", "ExampleService.ftl");
} catch (IOException e) {
@@ -92,12 +96,12 @@ public final class ExampleGenerateEngine {
public static void main(String[] args) {
Yaml yaml = new Yaml();
- InputStream in =
ExampleGenerateEngine.class.getResourceAsStream("/datamodel.yaml");
+ InputStream in =
ExampleGenerateEngine.class.getResourceAsStream("/template/springboot-starter-jdbc/datamodel.yaml");
Map<String, String> map = yaml.loadAs(in, Map.class);
String fileName = processString(map, FILE_NAME_PREFIX);
String outputPath = processString(map, OUTPUT_PATH);
for (String key : TEMPLATE_MAP.keySet()) {
- processFile(map, "/template/jdbc/" + TEMPLATE_MAP.get(key),
outputPath + "/" + fileName + key + ".java");
+ processFile(map, "/template/springboot-starter-jdbc/" +
TEMPLATE_MAP.get(key), outputPath + "/" + fileName + key + ".java");
}
}
}
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Example.ftl
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Example.ftl
index 9808e2a..ebe52c3 100644
---
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Example.ftl
+++
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Example.ftl
@@ -26,11 +26,6 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}Configuration
shardingConfiguration = new
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}Configuration();
DataSource dataSource = shardingConfiguration.getDataSource();
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}ExampleService
exampleService = new
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}ExampleService(dataSource);
- try {
- exampleService.initEnvironment();
- exampleService.processSuccess();
- } finally {
- exampleService.cleanEnvironment();
- }
+ exampleService.run();
}
}
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 9cd9e99..55eefb3 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
@@ -38,10 +38,24 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
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
*/
- public void initEnvironment() 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))";
@@ -64,7 +78,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
initAddressData(dataSource);
}
- public void processSuccess() throws SQLException {
+ private void processSuccess() throws SQLException {
System.out.println("-------------- Process Success Begin
---------------");
List<Long> orderIds = insertData();
printData();
@@ -73,7 +87,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
System.out.println("-------------- Process Success Finish
--------------");
}
- public void processFailure() throws SQLException {
+ private void processFailure() throws SQLException {
System.out.println("-------------- Process Failure Begin
---------------");
insertData();
System.out.println("-------------- Process Failure Finish
--------------");
@@ -148,7 +162,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
}
}
- public void printData() throws SQLException {
+ private void printData() throws SQLException {
System.out.println("---------------------------- Print Order Data
-----------------------");
for (Object each : this.getOrders()) {
System.out.println(each);
@@ -214,7 +228,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
* Restore the environment.
* @throws SQLException
*/
- public void cleanEnvironment() 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";
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/datamodel.yaml
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/datamodel.yaml
similarity index 100%
copy from
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/datamodel.yaml
copy to
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/datamodel.yaml
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Example.ftl
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/springboot-starter-jdbc/Example.ftl
similarity index 51%
copy from
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Example.ftl
copy to
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/springboot-starter-jdbc/Example.ftl
index 9808e2a..b58ef26 100644
---
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Example.ftl
+++
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/springboot-starter-jdbc/Example.ftl
@@ -15,22 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.example.${feature}.${framework};
+package org.apache.shardingsphere.example.${feature}.${framework?replace('-',
'.')};
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ConfigurableApplicationContext;
-import javax.sql.DataSource;
import java.sql.SQLException;
-public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}Example
{
+<#assign frameworkName="">
+<#list framework?split("-") as framework1>
+ <#assign frameworkName=frameworkName + framework1?cap_first>
+</#list>
+@SpringBootApplication
+public class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}Example
{
public static void main(String[] args) throws SQLException {
-
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}Configuration
shardingConfiguration = new
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}Configuration();
- DataSource dataSource = shardingConfiguration.getDataSource();
-
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}ExampleService
exampleService = new
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}ExampleService(dataSource);
- try {
- exampleService.initEnvironment();
- exampleService.processSuccess();
- } finally {
- exampleService.cleanEnvironment();
+ try (ConfigurableApplicationContext applicationContext =
SpringApplication.run(${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}Example.class,
args)) {
+
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}ExampleService
exampleService =
applicationContext.getBean(${mode?cap_first}${transaction?cap_first}${feature?cap_first}${frameworkName}ExampleService.class);
+ exampleService.run();
}
}
}
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/springboot-starter-jdbc/ExampleService.ftl
similarity index 92%
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/springboot-starter-jdbc/ExampleService.ftl
index 9cd9e99..fc81972 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/springboot-starter-jdbc/ExampleService.ftl
@@ -15,12 +15,13 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.example.${feature}.${framework};
+package org.apache.shardingsphere.example.${feature}.${framework?replace('-',
'.')};
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.example.core.api.entity.Address;
import org.apache.shardingsphere.example.core.api.entity.Order;
import org.apache.shardingsphere.example.core.api.entity.OrderItem;
+import org.springframework.stereotype.Service;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -32,16 +33,35 @@ import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
+<#assign frameworkName="">
+<#list framework?split("-") as framework1>
+ <#assign frameworkName=frameworkName + framework1?cap_first>
+</#list>
+@Service
@AllArgsConstructor
-public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${framework?cap_first}ExampleService
{
+public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}${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.
* @throws SQLException
*/
- public void initEnvironment() 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))";
@@ -64,7 +84,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
initAddressData(dataSource);
}
- public void processSuccess() throws SQLException {
+ private void processSuccess() throws SQLException {
System.out.println("-------------- Process Success Begin
---------------");
List<Long> orderIds = insertData();
printData();
@@ -73,7 +93,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
System.out.println("-------------- Process Success Finish
--------------");
}
- public void processFailure() throws SQLException {
+ private void processFailure() throws SQLException {
System.out.println("-------------- Process Failure Begin
---------------");
insertData();
System.out.println("-------------- Process Failure Finish
--------------");
@@ -148,7 +168,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
}
}
- public void printData() throws SQLException {
+ private void printData() throws SQLException {
System.out.println("---------------------------- Print Order Data
-----------------------");
for (Object each : this.getOrders()) {
System.out.println(each);
@@ -214,7 +234,7 @@ public final class
${mode?cap_first}${transaction?cap_first}${feature?cap_first}
* Restore the environment.
* @throws SQLException
*/
- public void cleanEnvironment() 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";
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/datamodel.yaml
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/springboot-starter-jdbc/datamodel.yaml
similarity index 96%
rename from
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/datamodel.yaml
rename to
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/springboot-starter-jdbc/datamodel.yaml
index 66fe5f1..58405a2 100644
---
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/datamodel.yaml
+++
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/springboot-starter-jdbc/datamodel.yaml
@@ -18,7 +18,7 @@
mode: memory
transaction: local
feature: sharding
-framework: jdbc
+framework: springboot-starter-jdbc
host: localhost
port: 3306
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
index e485d94..8d666c4 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
@@ -28,4 +28,10 @@
<packaging>pom</packaging>
<artifactId>shardingsphere-jdbc-memory-local-sharding-example</artifactId>
<name>${project.artifactId}</name>
+
+ <modules>
+ <module>shardingsphere-jdbc-memory-local-sharding-jdbc-example</module>
+
<module>shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example</module>
+ </modules>
+
</project>
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExample.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardin
[...]
index d5df961..0127e92 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExample.java
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExample.java
@@ -26,11 +26,6 @@ public final class MemoryLocalShardingJdbcExample {
MemoryLocalShardingJdbcConfiguration shardingConfiguration = new
MemoryLocalShardingJdbcConfiguration();
DataSource dataSource = shardingConfiguration.getDataSource();
MemoryLocalShardingJdbcExampleService exampleService = new
MemoryLocalShardingJdbcExampleService(dataSource);
- try {
- exampleService.initEnvironment();
- exampleService.processSuccess();
- } finally {
- exampleService.cleanEnvironment();
- }
+ exampleService.run();
}
}
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExampleService.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/
[...]
index 4a39d30..03a9fed 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExampleService.java
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExampleService.java
@@ -38,10 +38,24 @@ public final class MemoryLocalShardingJdbcExampleService {
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
*/
- public void initEnvironment() 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))";
@@ -64,7 +78,7 @@ public final class MemoryLocalShardingJdbcExampleService {
initAddressData(dataSource);
}
- public void processSuccess() throws SQLException {
+ private void processSuccess() throws SQLException {
System.out.println("-------------- Process Success Begin
---------------");
List<Long> orderIds = insertData();
printData();
@@ -73,7 +87,7 @@ public final class MemoryLocalShardingJdbcExampleService {
System.out.println("-------------- Process Success Finish
--------------");
}
- public void processFailure() throws SQLException {
+ private void processFailure() throws SQLException {
System.out.println("-------------- Process Failure Begin
---------------");
insertData();
System.out.println("-------------- Process Failure Finish
--------------");
@@ -148,7 +162,7 @@ public final class MemoryLocalShardingJdbcExampleService {
}
}
- public void printData() throws SQLException {
+ private void printData() throws SQLException {
System.out.println("---------------------------- Print Order Data
-----------------------");
for (Object each : this.getOrders()) {
System.out.println(each);
@@ -214,7 +228,7 @@ public final class MemoryLocalShardingJdbcExampleService {
* Restore the environment.
* @throws SQLException
*/
- public void cleanEnvironment() 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";
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/pom.xml
similarity index 55%
copy from
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
copy to
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/pom.xml
index e485d94..4ee0152 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/pom.xml
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/pom.xml
@@ -20,12 +20,33 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
- <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
+
<artifactId>shardingsphere-jdbc-memory-local-sharding-example</artifactId>
<groupId>org.apache.shardingsphere.example</groupId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
- <artifactId>shardingsphere-jdbc-memory-local-sharding-example</artifactId>
+
<artifactId>shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example</artifactId>
<name>${project.artifactId}</name>
-</project>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere.example</groupId>
+ <artifactId>example-raw-jdbc</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <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-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExample.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardin
[...]
similarity index 56%
copy from
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExample.java
copy to
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/springboot/starter/jdbc/MemoryLocalShardingSpringbootStarterJdbcExample.java
index d5df961..c983273 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExample.java
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/springboot/starter/jdbc/MemoryLocalShardingSpringbootStarterJdbcExample.java
@@ -15,22 +15,21 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.example.sharding.jdbc;
+package org.apache.shardingsphere.example.sharding.springboot.starter.jdbc;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ConfigurableApplicationContext;
-import javax.sql.DataSource;
import java.sql.SQLException;
-public final class MemoryLocalShardingJdbcExample {
+@SpringBootApplication
+public class MemoryLocalShardingSpringbootStarterJdbcExample {
public static void main(String[] args) throws SQLException {
- MemoryLocalShardingJdbcConfiguration shardingConfiguration = new
MemoryLocalShardingJdbcConfiguration();
- DataSource dataSource = shardingConfiguration.getDataSource();
- MemoryLocalShardingJdbcExampleService exampleService = new
MemoryLocalShardingJdbcExampleService(dataSource);
- try {
- exampleService.initEnvironment();
- exampleService.processSuccess();
- } finally {
- exampleService.cleanEnvironment();
+ try (ConfigurableApplicationContext applicationContext =
SpringApplication.run(MemoryLocalShardingSpringbootStarterJdbcExample.class,
args)) {
+ MemoryLocalShardingSpringbootStarterJdbcExampleService
exampleService =
applicationContext.getBean(MemoryLocalShardingSpringbootStarterJdbcExampleService.class);
+ exampleService.run();
}
}
}
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExampleService.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/
[...]
similarity index 93%
copy from
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExampleService.java
copy to
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/springboot/starter/jdbc/MemoryLocalShardingSpringbootStarterJdbcExampleService.java
index 4a39d30..81bb879 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/jdbc/MemoryLocalShardingJdbcExampleService.java
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/src/main/java/org/apache/shardingsphere/example/sharding/springboot/starter/jdbc/MemoryLocalShardingSpringbootStarterJdbcExampleService.java
@@ -15,12 +15,13 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.example.sharding.jdbc;
+package org.apache.shardingsphere.example.sharding.springboot.starter.jdbc;
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.example.core.api.entity.Address;
import org.apache.shardingsphere.example.core.api.entity.Order;
import org.apache.shardingsphere.example.core.api.entity.OrderItem;
+import org.springframework.stereotype.Service;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -32,16 +33,31 @@ import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
+@Service
@AllArgsConstructor
-public final class MemoryLocalShardingJdbcExampleService {
+public final class MemoryLocalShardingSpringbootStarterJdbcExampleService {
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
*/
- public void initEnvironment() 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))";
@@ -64,7 +80,7 @@ public final class MemoryLocalShardingJdbcExampleService {
initAddressData(dataSource);
}
- public void processSuccess() throws SQLException {
+ private void processSuccess() throws SQLException {
System.out.println("-------------- Process Success Begin
---------------");
List<Long> orderIds = insertData();
printData();
@@ -73,7 +89,7 @@ public final class MemoryLocalShardingJdbcExampleService {
System.out.println("-------------- Process Success Finish
--------------");
}
- public void processFailure() throws SQLException {
+ private void processFailure() throws SQLException {
System.out.println("-------------- Process Failure Begin
---------------");
insertData();
System.out.println("-------------- Process Failure Finish
--------------");
@@ -148,7 +164,7 @@ public final class MemoryLocalShardingJdbcExampleService {
}
}
- public void printData() throws SQLException {
+ private void printData() throws SQLException {
System.out.println("---------------------------- Print Order Data
-----------------------");
for (Object each : this.getOrders()) {
System.out.println(each);
@@ -214,7 +230,7 @@ public final class MemoryLocalShardingJdbcExampleService {
* Restore the environment.
* @throws SQLException
*/
- public void cleanEnvironment() 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";
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-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-sharding-ex
[...]
new file mode 100644
index 0000000..a1df690
--- /dev/null
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-sharding-example/shardingsphere-jdbc-memory-local-sharding-springboot-starter-jdbc-example/src/main/resources/application.properties
@@ -0,0 +1,69 @@
+#
+# 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.schema.name=sharding
+
+spring.shardingsphere.datasource.names=ds-0,ds-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://localhost:3307/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-0.username=root
+spring.shardingsphere.datasource.ds-0.password=123456
+spring.shardingsphere.datasource.ds-0.max-active=16
+
+spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
+spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
+spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3307/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+spring.shardingsphere.datasource.ds-1.username=root
+spring.shardingsphere.datasource.ds-1.password=123456
+spring.shardingsphere.datasource.ds-1.max-active=16
+
+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
+
+spring.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item
+spring.shardingsphere.rules.sharding.broadcast-tables=t_address
+
+spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds-$->{0..1}.t_order_$->{0..1}
+spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
+spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=t-order-inline
+
+spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id
+spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake
+
+spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds-$->{0..1}.t_order_item_$->{0..1}
+spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column=order_id
+spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name=t-order-item-inline
+
+spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id
+spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
+
+spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds-$->{user_id
% 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.type=INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.props.algorithm-expression=t_order_$->{order_id
% 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.type=INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.props.algorithm-expression=t_order_item_$->{order_id
% 2}
+
+spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
+spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=123
+
+spring.shardingsphere.rules.encrypt.encryptors.status-encryptor.type=AES
+spring.shardingsphere.rules.encrypt.encryptors.status-encryptor.props.aes-key-value=123456
+spring.shardingsphere.rules.encrypt.tables.t_order.columns.status.cipher-column=status
+spring.shardingsphere.rules.encrypt.tables.t_order.columns.status.encryptor-name=status-encryptor