This is an automated email from the ASF dual-hosted git repository.
morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git
The following commit(s) were added to refs/heads/master by this push:
new b5d8ee3 [Demo] Add spring mybatis jdbc demo (#6349)
b5d8ee3 is described below
commit b5d8ee35f5622991fe4b2eea576e6a39cfe392ba
Author: jiafeng.zhang <[email protected]>
AuthorDate: Thu Aug 5 14:34:34 2021 +0800
[Demo] Add spring mybatis jdbc demo (#6349)
add spring mybatis jdbc demo
---
.../docs/images/20210730144136.png | Bin 0 -> 37098 bytes
.../docs/images/20210730145555.png | Bin 0 -> 110067 bytes
.../docs/images/20210730145906.png | Bin 0 -> 40747 bytes
.../spring-jdbc-demo/docs/instructions.md | 61 ++++++++++
samples/doris-demo/spring-jdbc-demo/pom.xml | 84 +++++++++++++-
.../apache/doris/demo/spring/DorisApplication.java | 32 ++++++
.../demo/spring/config/ApplicationConfig.java | 37 ++++++
.../doris/demo/spring/config/DruidConfig.java | 128 +++++++++++++++++++++
.../doris/demo/spring/config/DruidProperties.java | 91 +++++++++++++++
.../demo/spring/controller/DemoController.java | 51 ++++++++
.../demo/spring/datasource/DataSourceType.java | 34 ++++++
.../demo/spring/datasource/DynamicDataSource.java | 37 ++++++
.../datasource/DynamicDataSourceContextHolder.java | 55 +++++++++
.../doris/demo/spring/mapper/DemoMapper.java | 27 +++++
.../doris/demo/spring/service/DemoService.java | 37 ++++++
.../apache/doris/demo/spring/util/SpringUtil.java | 108 +++++++++++++++++
.../src/main/resources/application-druid.yml | 50 ++++++++
.../src/main/resources/application.yml | 35 ++++++
.../src/main/resources/logback.xml | 88 ++++++++++++++
.../src/main/resources/mybatis/DemoMapper.xml | 31 +++++
.../src/main/resources/mybatis/mybatis-config.xml | 32 ++++++
21 files changed, 1012 insertions(+), 6 deletions(-)
diff --git a/samples/doris-demo/spring-jdbc-demo/docs/images/20210730144136.png
b/samples/doris-demo/spring-jdbc-demo/docs/images/20210730144136.png
new file mode 100644
index 0000000..a3c0f8b
Binary files /dev/null and
b/samples/doris-demo/spring-jdbc-demo/docs/images/20210730144136.png differ
diff --git a/samples/doris-demo/spring-jdbc-demo/docs/images/20210730145555.png
b/samples/doris-demo/spring-jdbc-demo/docs/images/20210730145555.png
new file mode 100644
index 0000000..52edde2
Binary files /dev/null and
b/samples/doris-demo/spring-jdbc-demo/docs/images/20210730145555.png differ
diff --git a/samples/doris-demo/spring-jdbc-demo/docs/images/20210730145906.png
b/samples/doris-demo/spring-jdbc-demo/docs/images/20210730145906.png
new file mode 100644
index 0000000..30b38a7
Binary files /dev/null and
b/samples/doris-demo/spring-jdbc-demo/docs/images/20210730145906.png differ
diff --git a/samples/doris-demo/spring-jdbc-demo/docs/instructions.md
b/samples/doris-demo/spring-jdbc-demo/docs/instructions.md
new file mode 100644
index 0000000..3f64f34
--- /dev/null
+++ b/samples/doris-demo/spring-jdbc-demo/docs/instructions.md
@@ -0,0 +1,61 @@
+<!--
+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.
+-->
+
+# Instructions for use
+
+This example is based on SpringBoot and Mybatis to integrate, access doris
data through JDBC, and then provide data API access services through rest.
+
+For specific table structure and sample data, please refer to the
documentation in the stream-load-demo project.
+
+## Directory structure description
+
+
+
+Package instruction:
+
+1. config : Data source configuration class, and Application configuration
+2. controller:rest interface class
+3. datasource : Data source configuration, mainly dynamic switching of
multiple data sources
+4. domain:Entity java bean
+5. mapper:mybatis data access interface definition
+6. service:Business service interface
+7. util:Tools
+8. DorisApplication : SpringBoot startup class
+
+Configuration instruction:
+
+1. application.yml :SpringBoot startup class
+2. application-druid.yml :Database connection configuration parameters
+3. mybatis : Related parameters of mybatis configuration, and mapper.xml
configuration file for mybatis data access
+
+## Interface call
+
+Start Springboot, enter restful url in the browser address bar
+
+### Query list
+
+visit:[localhost:8080/rest/demo/skulist](http://localhost:8080/rest/demo/skulist)
+
+
+
+### Query by ID
+
+
+
+
diff --git a/samples/doris-demo/spring-jdbc-demo/pom.xml
b/samples/doris-demo/spring-jdbc-demo/pom.xml
index 2622a80..5134c7e 100644
--- a/samples/doris-demo/spring-jdbc-demo/pom.xml
+++ b/samples/doris-demo/spring-jdbc-demo/pom.xml
@@ -29,18 +29,90 @@ under the License.
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
+ <parent>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-parent</artifactId>
+ <version>2.2.0.RELEASE</version>
+ <relativePath/> <!-- lookup parent from repository -->
+ </parent>
+
<properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <java.version>1.8</java.version>
+
<mybatis.spring.boot.starter.version>1.3.2</mybatis.spring.boot.starter.version>
+ <fastjson.version>1.2.70</fastjson.version>
+ <druid.version>1.1.14</druid.version>
+ <commons.io.version>2.5</commons.io.version>
</properties>
<dependencies>
+ <!-- alibaba druid -->
+ <dependency>
+ <groupId>com.alibaba</groupId>
+ <artifactId>druid-spring-boot-starter</artifactId>
+ <version>${druid.version}</version>
+ </dependency>
+
+
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-jdbc</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>mysql</groupId>
+ <artifactId>mysql-connector-java</artifactId>
+ <version>8.0.15</version>
+ </dependency>
+
+ <!-- SpringBoot Web -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ </dependency>
+
+
+ <!-- pool -->
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-pool2</artifactId>
</dependency>
+
+
+ <!-- SpringBoot mybatis -->
+ <dependency>
+ <groupId>org.mybatis.spring.boot</groupId>
+ <artifactId>mybatis-spring-boot-starter</artifactId>
+ <version>${mybatis.spring.boot.starter.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>${commons.io.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.alibaba</groupId>
+ <artifactId>fastjson</artifactId>
+ <version>${fastjson.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context-support</artifactId>
+ </dependency>
+
</dependencies>
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/DorisApplication.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/DorisApplication.java
new file mode 100644
index 0000000..d0e10f3
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/DorisApplication.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.doris.demo.spring;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+
+@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
+public class DorisApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DorisApplication.class, args);
+ }
+
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/ApplicationConfig.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/ApplicationConfig.java
new file mode 100644
index 0000000..d975231
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/ApplicationConfig.java
@@ -0,0 +1,37 @@
+// 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.doris.demo.spring.config;
+
+import java.util.TimeZone;
+import org.mybatis.spring.annotation.MapperScan;
+import
org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@MapperScan("org.apache.doris.demo.spring.**.mapper")
+public class ApplicationConfig {
+ /**
+ * Time zone configuration
+ */
+ @Bean
+ public Jackson2ObjectMapperBuilderCustomizer
jacksonObjectMapperCustomization() {
+ return jacksonObjectMapperBuilder ->
jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
+ }
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/DruidConfig.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/DruidConfig.java
new file mode 100644
index 0000000..7214476
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/DruidConfig.java
@@ -0,0 +1,128 @@
+// 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.doris.demo.spring.config;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.sql.DataSource;
+
+import org.apache.doris.demo.spring.datasource.DataSourceType;
+import org.apache.doris.demo.spring.datasource.DynamicDataSource;
+import org.apache.doris.demo.spring.util.SpringUtil;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import com.alibaba.druid.pool.DruidDataSource;
+import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
+import
com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
+import com.alibaba.druid.util.Utils;
+
+
+@Configuration
+public class DruidConfig {
+ @Bean
+ @ConfigurationProperties("spring.datasource.druid.master")
+ public DataSource masterDataSource(DruidProperties druidProperties) {
+ DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
+ return druidProperties.dataSource(dataSource);
+ }
+
+ @Bean
+ @ConfigurationProperties("spring.datasource.druid.slave")
+ @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name =
"enabled", havingValue = "true")
+ public DataSource slaveDataSource(DruidProperties druidProperties) {
+ DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
+ return druidProperties.dataSource(dataSource);
+ }
+
+ @Bean(name = "dynamicDataSource")
+ @Primary
+ public DynamicDataSource dataSource(DataSource masterDataSource) {
+ Map<Object, Object> targetDataSources = new HashMap<>();
+ targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
+ setDataSource(targetDataSources, DataSourceType.SLAVE.name(),
"slaveDataSource");
+ return new DynamicDataSource(masterDataSource, targetDataSources);
+ }
+
+ /**
+ * Set up the data source
+ *
+ * @param targetDataSources
+ * @param sourceName
+ * @param beanName
+ */
+ public void setDataSource(Map<Object, Object> targetDataSources, String
sourceName, String beanName) {
+ try {
+ DataSource dataSource = SpringUtil.getBean(beanName);
+ targetDataSources.put(sourceName, dataSource);
+ } catch (Exception e) {
+ }
+ }
+
+ /**
+ * Remove the ads at the bottom of the druid monitoring page
+ */
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Bean
+ @ConditionalOnProperty(name =
"spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
+ public FilterRegistrationBean
removeDruidFilterRegistrationBean(DruidStatProperties properties) {
+ // Get the parameters of the web monitoring page
+ DruidStatProperties.StatViewServlet config =
properties.getStatViewServlet();
+ // Extract the configuration path of common.js
+ String pattern = config.getUrlPattern() != null ?
config.getUrlPattern() : "/druid/*";
+ String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
+ final String filePath = "support/http/resources/js/common.js";
+ // Create filter to filter
+ Filter filter = new Filter() {
+ @Override
+ public void init(javax.servlet.FilterConfig filterConfig) throws
ServletException {
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain)
+ throws IOException, ServletException {
+ chain.doFilter(request, response);
+ // Reset the buffer, the response header will not be reset
+ response.resetBuffer();
+ // Get common.js
+ String text = Utils.readFromResource(filePath);
+ // Regularly replace the banner, remove the advertising
information at the bottom
+ text = text.replaceAll("<a.*?banner\"></a><br/>", "");
+ text = text.replaceAll("powered.*?shrek.wang</a>", "");
+ response.getWriter().write(text);
+ }
+
+ @Override
+ public void destroy() {
+ }
+ };
+ FilterRegistrationBean registrationBean = new FilterRegistrationBean();
+ registrationBean.setFilter(filter);
+ registrationBean.addUrlPatterns(commonJsPattern);
+ return registrationBean;
+ }
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/DruidProperties.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/DruidProperties.java
new file mode 100644
index 0000000..b4ba81e
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/config/DruidProperties.java
@@ -0,0 +1,91 @@
+// 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.doris.demo.spring.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+import com.alibaba.druid.pool.DruidDataSource;
+
+@Configuration
+public class DruidProperties
+{
+ @Value("${spring.datasource.druid.initialSize}")
+ private int initialSize;
+
+ @Value("${spring.datasource.druid.minIdle}")
+ private int minIdle;
+
+ @Value("${spring.datasource.druid.maxActive}")
+ private int maxActive;
+
+ @Value("${spring.datasource.druid.maxWait}")
+ private int maxWait;
+
+ @Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
+ private int timeBetweenEvictionRunsMillis;
+
+ @Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
+ private int minEvictableIdleTimeMillis;
+
+ @Value("${spring.datasource.druid.maxEvictableIdleTimeMillis}")
+ private int maxEvictableIdleTimeMillis;
+
+ @Value("${spring.datasource.druid.validationQuery}")
+ private String validationQuery;
+
+ @Value("${spring.datasource.druid.testWhileIdle}")
+ private boolean testWhileIdle;
+
+ @Value("${spring.datasource.druid.testOnBorrow}")
+ private boolean testOnBorrow;
+
+ @Value("${spring.datasource.druid.testOnReturn}")
+ private boolean testOnReturn;
+
+ public DruidDataSource dataSource(DruidDataSource datasource)
+ {
+ /** Configure initial size, minimum, maximum */
+ datasource.setInitialSize(initialSize);
+ datasource.setMaxActive(maxActive);
+ datasource.setMinIdle(minIdle);
+
+ /** Configure the timeout period to wait for the connection to be
acquired */
+ datasource.setMaxWait(maxWait);
+
+ /** Configure how long the interval is to perform a check, check idle
connections that need to be closed, the unit is milliseconds */
+
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
+
+ /** Configure the minimum and maximum survival time of a connection in
the pool, in milliseconds */
+ datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
+ datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);
+
+ /**
+ * The sql used to check whether the connection is valid, the
requirement is a query statement, usually select'x'. If the validationQuery is
null, testOnBorrow, testOnReturn, testWhileIdle will not work.
+ */
+ datasource.setValidationQuery(validationQuery);
+ /** It is recommended to configure it as true to not affect
performance and ensure safety. Check when applying for a connection. If the
idle time is greater than timeBetweenEvictionRunsMillis, execute a
validationQuery to check whether the connection is valid. */
+ datasource.setTestWhileIdle(testWhileIdle);
+ /** When applying for a connection, execute validationQuery to check
whether the connection is valid. This configuration will reduce performance. */
+ datasource.setTestOnBorrow(testOnBorrow);
+ /** When the connection is returned, the validationQuery is executed
to check whether the connection is valid. This configuration will reduce
performance. */
+ datasource.setTestOnReturn(testOnReturn);
+ return datasource;
+ }
+}
+
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/controller/DemoController.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/controller/DemoController.java
new file mode 100644
index 0000000..65f1b2a
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/controller/DemoController.java
@@ -0,0 +1,51 @@
+// 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.doris.demo.spring.controller;
+
+import org.apache.doris.demo.spring.service.DemoService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/rest/demo")
+public class DemoController {
+
+ @Autowired
+ private DemoService demoService;
+
+ @GetMapping("/skulist/{skuId}")
+ public Object getSkuList(@PathVariable(value = "skuId") String skuId){
+ Map<String,Object> params = new HashMap<>();
+ if(skuId != null){
+ params.put("skuId",skuId);
+ }
+ return demoService.getSkuList(params);
+ }
+ @GetMapping("/skulist")
+ public Object getSkuList(){
+ Map<String,Object> params = new HashMap<>();
+ return demoService.getSkuList(params);
+ }
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DataSourceType.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DataSourceType.java
new file mode 100644
index 0000000..1ad272a
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DataSourceType.java
@@ -0,0 +1,34 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+
+package org.apache.doris.demo.spring.datasource;
+
+/**
+ * data source type
+ */
+public enum DataSourceType {
+ /**
+ * master
+ */
+ MASTER,
+
+ /**
+ * slave
+ */
+ SLAVE
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DynamicDataSource.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DynamicDataSource.java
new file mode 100644
index 0000000..fbda38d
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DynamicDataSource.java
@@ -0,0 +1,37 @@
+// 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.doris.demo.spring.datasource;
+
+import java.util.Map;
+import javax.sql.DataSource;
+
+import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
+
+public class DynamicDataSource extends AbstractRoutingDataSource {
+ public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object,
Object> targetDataSources) {
+ super.setDefaultTargetDataSource(defaultTargetDataSource);
+ super.setTargetDataSources(targetDataSources);
+ super.afterPropertiesSet();
+ }
+
+ @Override
+ protected Object determineCurrentLookupKey() {
+ return DynamicDataSourceContextHolder.getDataSourceType();
+ }
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DynamicDataSourceContextHolder.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DynamicDataSourceContextHolder.java
new file mode 100644
index 0000000..e227c8d
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/datasource/DynamicDataSourceContextHolder.java
@@ -0,0 +1,55 @@
+// 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.doris.demo.spring.datasource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DynamicDataSourceContextHolder {
+
+ public static final Logger log =
LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
+
+ /**
+ * Use ThreadLocal to maintain variables. ThreadLocal provides an
independent copy of the variable for each thread that uses the variable.
+ * Therefore, each thread can independently change its own copy without
affecting the copies corresponding to other threads.
+ */
+ private static final ThreadLocal<String> CONTEXT_HOLDER = new
ThreadLocal<>();
+
+ /**
+ * Set the variables of the data source
+ */
+ public static void setDataSourceType(String dsType) {
+ log.info("Switch to {} data source", dsType);
+ CONTEXT_HOLDER.set(dsType);
+ }
+
+ /**
+ * Get the variables of the data source
+ */
+ public static String getDataSourceType() {
+ return CONTEXT_HOLDER.get();
+ }
+
+ /**
+ * Clear data source variables
+ */
+ public static void clearDataSourceType() {
+ CONTEXT_HOLDER.remove();
+ }
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/mapper/DemoMapper.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/mapper/DemoMapper.java
new file mode 100644
index 0000000..8dd0d29
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/mapper/DemoMapper.java
@@ -0,0 +1,27 @@
+// 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.doris.demo.spring.mapper;
+
+import java.util.List;
+import java.util.Map;
+
+public interface DemoMapper {
+
+ public List<Map<String,Object>> selectSkuList(Map<String,Object> param);
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/service/DemoService.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/service/DemoService.java
new file mode 100644
index 0000000..4c3c526
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/service/DemoService.java
@@ -0,0 +1,37 @@
+// 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.doris.demo.spring.service;
+
+import org.apache.doris.demo.spring.mapper.DemoMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+import java.util.Map;
+
+@Service
+public class DemoService {
+ @Autowired
+ private DemoMapper demoMapper;
+
+ public List<Map<String,Object>> getSkuList(Map<String,Object> params){
+ return demoMapper.selectSkuList(params);
+ }
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/util/SpringUtil.java
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/util/SpringUtil.java
new file mode 100644
index 0000000..7e6f54e
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/java/org/apache/doris/demo/spring/util/SpringUtil.java
@@ -0,0 +1,108 @@
+// 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.doris.demo.spring.util;
+
+import org.springframework.aop.framework.AopContext;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
+import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * The spring tool class is convenient for obtaining beans in a non-spring
management environment
+ */
+@Component
+public final class SpringUtil implements BeanFactoryPostProcessor {
+ /**
+ * Spring application context
+ */
+ private static ConfigurableListableBeanFactory beanFactory;
+
+ @Override
+ public void postProcessBeanFactory(ConfigurableListableBeanFactory
beanFactory) throws BeansException {
+ SpringUtil.beanFactory = beanFactory;
+ }
+
+ /**
+ * get object
+ *
+ * @param name
+ * @return Object
+ * @throws org.springframework.beans.BeansException
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T getBean(String name) throws BeansException {
+ return (T) beanFactory.getBean(name);
+ }
+
+ /**
+ * Get an object of type requiredType
+ *
+ * @param clz
+ * @return
+ * @throws org.springframework.beans.BeansException
+ */
+ public static <T> T getBean(Class<T> clz) throws BeansException {
+ T result = (T) beanFactory.getBean(clz);
+ return result;
+ }
+
+ /**
+ * Returns true if the BeanFactory contains a bean definition matching the
given name
+ *
+ * @param name
+ * @return boolean
+ */
+ public static boolean containsBean(String name) {
+ return beanFactory.containsBean(name);
+ }
+
+ /**
+ * Determine whether the bean definition registered with the given name is
a singleton or a prototype. If the bean definition corresponding to the given
name is not found, an exception (NoSuchBeanDefinitionException) will be thrown
+ *
+ * @param name
+ * @return boolean
+ * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
+ */
+ public static boolean isSingleton(String name) throws
NoSuchBeanDefinitionException {
+ return beanFactory.isSingleton(name);
+ }
+
+ /**
+ * @param name
+ * @return Class Type of registered object
+ * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
+ */
+ public static Class<?> getType(String name) throws
NoSuchBeanDefinitionException {
+ return beanFactory.getType(name);
+ }
+
+ /**
+ * If the given bean name has aliases in the bean definition, these
aliases are returned
+ *
+ * @param name
+ * @return
+ * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
+ */
+ public static String[] getAliases(String name) throws
NoSuchBeanDefinitionException {
+ return beanFactory.getAliases(name);
+ }
+
+}
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/resources/application-druid.yml
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/application-druid.yml
new file mode 100644
index 0000000..21996a1
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/application-druid.yml
@@ -0,0 +1,50 @@
+# Data source configuration
+spring:
+ datasource:
+ type: com.alibaba.druid.pool.DruidDataSource
+ driverClassName: com.mysql.cj.jdbc.Driver
+ druid:
+ # Master Data source configuration
+ master:
+ url: jdbc:mysql://10.220.146.10:9030/test_2
+ username: root
+ password:
+ # Number of initial connections
+ initialSize: 5
+ # Minimum number of connection pools
+ minIdle: 10
+ # Maximum number of connection pools
+ maxActive: 20
+ # Configure the timeout period to wait for the connection to be
acquired
+ maxWait: 60000
+ # Configure how long the interval is to perform a check, check
idle connections that need to be closed, the unit is milliseconds
+ timeBetweenEvictionRunsMillis: 60000
+ # Configure the minimum survival time of a connection in the pool,
in milliseconds
+ minEvictableIdleTimeMillis: 300000
+ # Configure the maximum survival time of a connection in the pool,
in milliseconds
+ maxEvictableIdleTimeMillis: 900000
+ # Configure to detect whether the connection is valid
+ validationQuery: SELECT 1 FROM DUAL
+ testWhileIdle: true
+ testOnBorrow: false
+ testOnReturn: false
+ webStatFilter:
+ enabled: true
+ statViewServlet:
+ enabled: true
+ # Set a whitelist, leave it blank to allow all access
+ allow:
+ url-pattern: /druid/*
+ #Console management user name and password
+ login-username: admin
+ login-password: admin
+ filter:
+ stat:
+ enabled: true
+ # Slow SQL records
+ log-slow-sql: true
+ slow-sql-millis: 1000
+ merge-sql: true
+ wall:
+ config:
+ multi-statement-allow: true
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/resources/application.yml
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/application.yml
new file mode 100644
index 0000000..c7174ea
--- /dev/null
+++ b/samples/doris-demo/spring-jdbc-demo/src/main/resources/application.yml
@@ -0,0 +1,35 @@
+# Development environment configuration
+server:
+ # The HTTP port of the server, the default is 8080
+ port: 8080
+ servlet:
+ # Access path of the application
+ context-path: /
+ tomcat:
+ # URI encoding of tomcat
+ uri-encoding: UTF-8
+ #The maximum number of tomcat threads, the default is 200
+ max-threads: 800
+ # The number of threads that Tomcat starts to initialize, the default
value is 25
+ min-spare-threads: 30
+
+# Log configuration
+logging:
+ level:
+ com.data: debug
+ org.springframework: warn
+
+# Spring configuration
+spring:
+ profiles:
+ active: druid
+ resources:
+ static-locations: classpath:/static
+# MyBatis configuration
+mybatis:
+ # Search for specified package alias
+ typeAliasesPackage: org.apache.doris.demo.spring.**.domain
+ # Configure mapper scanning and find all mapper.xml mapping files
+ mapperLocations: classpath*:mybatis/*Mapper.xml
+ # Load the global configuration file
+ configLocation: classpath:mybatis/mybatis-config.xml
diff --git a/samples/doris-demo/spring-jdbc-demo/src/main/resources/logback.xml
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/logback.xml
new file mode 100644
index 0000000..e7a4665
--- /dev/null
+++ b/samples/doris-demo/spring-jdbc-demo/src/main/resources/logback.xml
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<configuration>
+
+ <property name="log.path" value="./logs/" />
+ <property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level
%logger{20} - [%method,%line] - %msg%n" />
+ <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ </appender>
+
+
+ <appender name="file_info"
class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <file>${log.path}/sys-info.log</file>
+ <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
+ <maxHistory>60</maxHistory>
+ </rollingPolicy>
+ <encoder>
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ <filter class="ch.qos.logback.classic.filter.LevelFilter">
+ <level>INFO</level>
+ <onMatch>ACCEPT</onMatch>
+ <onMismatch>DENY</onMismatch>
+ </filter>
+ </appender>
+
+ <appender name="file_error"
class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <file>${log.path}/sys-error.log</file>
+ <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
+ <maxHistory>60</maxHistory>
+ </rollingPolicy>
+ <encoder>
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ <filter class="ch.qos.logback.classic.filter.LevelFilter">
+ <level>ERROR</level>
+ <onMatch>ACCEPT</onMatch>
+ <onMismatch>DENY</onMismatch>
+ </filter>
+ </appender>
+
+ <appender name="sys-user"
class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <file>${log.path}/sys-user.log</file>
+ <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
+ <maxHistory>60</maxHistory>
+ </rollingPolicy>
+ <encoder>
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ </appender>
+
+ <logger name="com.apache.doris.demo" level="info" />
+ <logger name="org.springframework" level="warn" />
+ <root level="info">
+ <appender-ref ref="console" />
+ </root>
+
+ <root level="info">
+ <appender-ref ref="file_info" />
+ <appender-ref ref="file_error" />
+ </root>
+
+ <logger name="sys-user" level="info">
+ <appender-ref ref="sys-user"/>
+ </logger>
+</configuration>
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/resources/mybatis/DemoMapper.xml
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/mybatis/DemoMapper.xml
new file mode 100644
index 0000000..bcd3897
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/mybatis/DemoMapper.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="org.apache.doris.demo.spring.mapper.DemoMapper">
+
+ <select id="selectSkuList" parameterType="java.util.Map"
resultType="java.util.Map">
+ select id,number,price,skuname,skudesc from doris_test_sink
+ <where>
+ <if test="skuId !='' and skuId != null">
+ id=#{skuId}
+ </if>
+ </where>
+ </select>
+</mapper>
diff --git
a/samples/doris-demo/spring-jdbc-demo/src/main/resources/mybatis/mybatis-config.xml
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/mybatis/mybatis-config.xml
new file mode 100644
index 0000000..c268992
--- /dev/null
+++
b/samples/doris-demo/spring-jdbc-demo/src/main/resources/mybatis/mybatis-config.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<!DOCTYPE configuration
+PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+"http://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+
+ <settings>
+ <setting name="cacheEnabled" value="true" />
+ <setting name="useGeneratedKeys" value="true" />
+ <setting name="defaultExecutorType" value="REUSE" />
+ <setting name="logImpl" value="SLF4J" />
+ </settings>
+
+</configuration>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]