From feb22af6762e59a162718f49263740a48f2d55af Mon Sep 17 00:00:00 2001
From: lishipeng <1572427111@qq.com>
Date: Thu, 8 Sep 2022 11:38:27 +0800
Subject: [PATCH] first commit
---
.gitignore | 30 +++++
README.md | 88 ++++++++++++++
pom.xml | 96 ++++++++++++++++
.../SpringbootTdengineDemoApplication.java | 14 +++
.../demo/controller/WeatherController.java | 98 ++++++++++++++++
.../tdengine/demo/dao/WeatherMapper.java | 35 ++++++
.../tdengine/demo/dao/WeatherMapper.xml | 108 ++++++++++++++++++
.../tdengine/demo/domain/Weather.java | 70 ++++++++++++
.../tdengine/demo/service/WeatherService.java | 76 ++++++++++++
src/main/resources/application.properties | 18 +++
10 files changed, 633 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md
create mode 100644 pom.xml
create mode 100644 src/main/java/com/taosdata/example/springboot/tdengine/demo/SpringbootTdengineDemoApplication.java
create mode 100644 src/main/java/com/taosdata/example/springboot/tdengine/demo/controller/WeatherController.java
create mode 100644 src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.java
create mode 100644 src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.xml
create mode 100644 src/main/java/com/taosdata/example/springboot/tdengine/demo/domain/Weather.java
create mode 100644 src/main/java/com/taosdata/example/springboot/tdengine/demo/service/WeatherService.java
create mode 100644 src/main/resources/application.properties
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b8a47ad
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,30 @@
+.mvn/
+target/
+!**/src/main/**
+!**/src/test/**
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+
+### VS Code ###
+.vscode/
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ef7a377
--- /dev/null
+++ b/README.md
@@ -0,0 +1,88 @@
+## TDengine SpringBoot + Mybatis Demo
+
+### 配置 application.properties
+```properties
+# datasource config - JDBC-RESTful
+spring.datasource.driver-class-name=com.taosdata.jdbc.rs.RestfulDriver
+spring.datasource.url=jdbc:TAOS-RS://你的IP地址:6041/demo?timezone=UTC-8&charset=UTF-8&locale=en_US.UTF-8
+spring.datasource.username=root
+spring.datasource.password=taosdata
+
+spring.datasource.druid.initial-size=5
+spring.datasource.druid.min-idle=5
+spring.datasource.druid.max-active=5
+spring.datasource.druid.max-wait=30000
+spring.datasource.druid.validation-query=select server_status();
+
+#mybatis
+#mybatis.mapper-locations=classpath:mapper/*.xml
+
+logging.level.com.taosdata.example.springbootdemo.dao=debug
+
+server.port=8081
+```
+
+### 主要功能
+
+* 创建数据库和表
+```xml
+
+
+ create database if not exists test;
+
+
+
+ create table if not exists test.weather(ts timestamp, temperature int, humidity float);
+
+```
+
+* 插入单条记录
+```xml
+
+
+ insert into test.weather (ts, temperature, humidity) values (now, #{temperature,jdbcType=INTEGER}, #{humidity,jdbcType=FLOAT})
+
+```
+* 插入多条记录
+```xml
+
+
+ insert into test.weather (ts, temperature, humidity) values
+
+ (now + #{index}a, #{weather.temperature}, #{weather.humidity})
+
+
+```
+* 分页查询
+```xml
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ts, temperature, humidity
+
+
+
+
+```
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..8d8c10b
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,96 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.1.RELEASE
+
+
+ com.taosdata.example
+ springboot-tdengine-demo
+ 0.0.1-SNAPSHOT
+ springboot-tdengine-demo
+ Demo project for using tdengine with Spring Boot
+
+
+ 1.8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jdbc
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.mybatis.spring.boot
+ mybatis-spring-boot-starter
+ 2.1.1
+
+
+
+ com.taosdata.jdbc
+ taos-jdbcdriver
+ 3.0.0
+
+
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.1.17
+
+
+
+ org.springframework.boot
+ spring-boot-devtools
+ runtime
+ true
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+
+
+
+
+
+
+
+
+ src/main/resources
+
+ **/*.properties
+ **/*.xml
+
+ true
+
+
+ src/main/java
+
+ **/*.properties
+ **/*.xml
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/src/main/java/com/taosdata/example/springboot/tdengine/demo/SpringbootTdengineDemoApplication.java b/src/main/java/com/taosdata/example/springboot/tdengine/demo/SpringbootTdengineDemoApplication.java
new file mode 100644
index 0000000..1f63b2a
--- /dev/null
+++ b/src/main/java/com/taosdata/example/springboot/tdengine/demo/SpringbootTdengineDemoApplication.java
@@ -0,0 +1,14 @@
+package com.taosdata.example.springboot.tdengine.demo;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@MapperScan(basePackages = {"com.taosdata.example.springboot.tdengine.demo.dao"})
+@SpringBootApplication
+public class SpringbootTdengineDemoApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SpringbootTdengineDemoApplication.class, args);
+ }
+}
diff --git a/src/main/java/com/taosdata/example/springboot/tdengine/demo/controller/WeatherController.java b/src/main/java/com/taosdata/example/springboot/tdengine/demo/controller/WeatherController.java
new file mode 100644
index 0000000..9daaaed
--- /dev/null
+++ b/src/main/java/com/taosdata/example/springboot/tdengine/demo/controller/WeatherController.java
@@ -0,0 +1,98 @@
+package com.taosdata.example.springboot.tdengine.demo.controller;
+
+import com.taosdata.example.springboot.tdengine.demo.domain.Weather;
+import com.taosdata.example.springboot.tdengine.demo.service.WeatherService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@RequestMapping("/weather")
+@RestController
+public class WeatherController {
+
+ @Autowired
+ private WeatherService weatherService;
+
+ /**
+ * create database and table
+ * 初始化
+ * @return
+ */
+ @GetMapping("/init")
+ public int init() {
+ return weatherService.init();
+ }
+
+ /**
+ * Pagination Query
+ * 分页查询
+ * @param limit
+ * @param offset
+ * @return
+ */
+ @GetMapping("/{limit}/{offset}")
+ public List queryWeather(@PathVariable Long limit, @PathVariable Long offset) {
+ return weatherService.query(limit, offset);
+ }
+
+ /**
+ * upload single weather info
+ * 新增天气数据
+ * @param temperature
+ * @param humidity
+ * @return
+ */
+ @PostMapping("/{temperature}/{humidity}")
+ public int saveWeather(@PathVariable float temperature, @PathVariable float humidity) {
+ return weatherService.save(temperature, humidity);
+ }
+
+
+ /**
+ * 批量插入
+ * @param weatherList
+ * @return
+ */
+ @PostMapping("/batch")
+ public int saveBatchWeather(@RequestBody List weatherList) {
+ return weatherService.saveBatch(weatherList);
+ }
+
+ /**
+ * 查询数量
+ * @return
+ */
+ @GetMapping("/count")
+ public int count() {
+ return weatherService.count();
+ }
+
+ /**
+ * 获取分表名称
+ * @return
+ */
+ @GetMapping("/subTables")
+ public List getSubTables() {
+ return weatherService.getSubTables();
+ }
+
+ /**
+ * 获取平均温度
+ * @return
+ */
+ @GetMapping("/avg")
+ public List avg() {
+ return weatherService.avg();
+ }
+
+ @GetMapping("/max")
+ public List max() {
+ return weatherService.max();
+ }
+
+ @DeleteMapping("/delete/{number}")
+ public void delNumber(@PathVariable String number) {
+ weatherService.deleteTemperature(number);
+ }
+}
diff --git a/src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.java b/src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.java
new file mode 100644
index 0000000..fa76c6d
--- /dev/null
+++ b/src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.java
@@ -0,0 +1,35 @@
+package com.taosdata.example.springboot.tdengine.demo.dao;
+
+import com.taosdata.example.springboot.tdengine.demo.domain.Weather;
+import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public interface WeatherMapper {
+
+ void dropDB();
+
+ void createDB();
+
+ void createSuperTable();
+
+ void createTable(Weather weather);
+
+ List select(@Param("limit") Long limit, @Param("offset") Long offset);
+
+ int insert(Weather weather);
+
+ int insertBatch(List weatherList);
+
+ int count();
+
+ List getSubTables();
+
+ List avg();
+
+ List max();
+
+ void deleteTemperature( @Param("number") String number);
+}
diff --git a/src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.xml b/src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.xml
new file mode 100644
index 0000000..ae80f46
--- /dev/null
+++ b/src/main/java/com/taosdata/example/springboot/tdengine/demo/dao/WeatherMapper.xml
@@ -0,0 +1,108 @@
+
+
+
+
+
+
+
+
+
+
+
+
+ ts, temperature, humidity, location, groupid
+
+
+
+ drop database if exists demo
+
+
+
+ create database if not exists demo
+
+
+
+ create table if not exists demo.weather(ts timestamp, temperature float, humidity float) tags(location nchar(64), groupId int)
+
+
+
+ create table if not exists demo.t#{groupId} using demo.weather tags(#{location}, #{groupId})
+
+
+
+
+
+
+ insert into demo.t#{groupId} (ts, temperature, humidity) values (now, ${temperature}, ${humidity})
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ insert into
+
+ demo.t0 values
+ (#{weather.ts}, #{weather.temperature}, #{weather.humidity})
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ delete from demo.weather where ts = #{number}
+
+
+
diff --git a/src/main/java/com/taosdata/example/springboot/tdengine/demo/domain/Weather.java b/src/main/java/com/taosdata/example/springboot/tdengine/demo/domain/Weather.java
new file mode 100644
index 0000000..866ccb7
--- /dev/null
+++ b/src/main/java/com/taosdata/example/springboot/tdengine/demo/domain/Weather.java
@@ -0,0 +1,70 @@
+package com.taosdata.example.springboot.tdengine.demo.domain;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonInclude;
+
+import java.sql.Timestamp;
+
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class Weather {
+
+ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
+ private Timestamp ts;
+ // 温度
+ private Float temperature;
+ // 湿度
+ private Float humidity;
+ // 地址
+ private String location;
+ // 分组
+ private int groupId;
+
+ public Weather() {
+ }
+
+ public Weather(Timestamp ts, float temperature, float humidity) {
+ this.ts = ts;
+ this.temperature = temperature;
+ this.humidity = humidity;
+ }
+
+ public Timestamp getTs() {
+ return ts;
+ }
+
+ public void setTs(Timestamp ts) {
+ this.ts = ts;
+ }
+
+ public Float getTemperature() {
+ return temperature;
+ }
+
+ public void setTemperature(Float temperature) {
+ this.temperature = temperature;
+ }
+
+ public Float getHumidity() {
+ return humidity;
+ }
+
+ public void setHumidity(Float humidity) {
+ this.humidity = humidity;
+ }
+
+ public String getLocation() {
+ return location;
+ }
+
+ public void setLocation(String location) {
+ this.location = location;
+ }
+
+ public int getGroupId() {
+ return groupId;
+ }
+
+ public void setGroupId(int groupId) {
+ this.groupId = groupId;
+ }
+}
diff --git a/src/main/java/com/taosdata/example/springboot/tdengine/demo/service/WeatherService.java b/src/main/java/com/taosdata/example/springboot/tdengine/demo/service/WeatherService.java
new file mode 100644
index 0000000..06ce49c
--- /dev/null
+++ b/src/main/java/com/taosdata/example/springboot/tdengine/demo/service/WeatherService.java
@@ -0,0 +1,76 @@
+package com.taosdata.example.springboot.tdengine.demo.service;
+
+import com.taosdata.example.springboot.tdengine.demo.domain.Weather;
+import com.taosdata.example.springboot.tdengine.demo.dao.WeatherMapper;
+import org.springframework.stereotype.Service;
+
+import java.sql.Timestamp;
+import java.util.List;
+import java.util.Random;
+
+@Service
+public class WeatherService {
+
+ private final WeatherMapper weatherMapper;
+
+ public WeatherService(WeatherMapper weatherMapper) {
+ this.weatherMapper = weatherMapper;
+ }
+
+ private Random random = new Random(System.currentTimeMillis());
+ private String[] locations = {"北京", "上海", "广州", "深圳", "天津"};
+
+ public int init() {
+ weatherMapper.dropDB();
+ weatherMapper.createDB();
+ weatherMapper.createSuperTable();
+ long ts = System.currentTimeMillis();
+ long thirtySec = 1000 * 30;
+ int count = 0;
+ for (int i = 0; i < 20; i++) {
+ Weather weather = new Weather(new Timestamp(ts + (thirtySec * i)), 30 * random.nextFloat(), random.nextInt(100));
+ weather.setLocation(locations[random.nextInt(locations.length)]);
+ weather.setGroupId(i % locations.length);
+ weatherMapper.createTable(weather);
+ count += weatherMapper.insert(weather);
+ }
+ return count;
+ }
+
+ public List query(Long limit, Long offset) {
+ return weatherMapper.select(limit, offset);
+ }
+
+ public int save(float temperature, float humidity) {
+ Weather weather = new Weather();
+// weather.setTs(new Timestamp(System.currentTimeMillis()));
+ weather.setTemperature(temperature);
+ weather.setHumidity(humidity);
+
+ return weatherMapper.insert(weather);
+ }
+
+ public int saveBatch(List weatherList) {
+ return weatherMapper.insertBatch(weatherList);
+ }
+
+ public int count() {
+ return weatherMapper.count();
+ }
+
+ public List getSubTables() {
+ return weatherMapper.getSubTables();
+ }
+
+ public List avg() {
+ return weatherMapper.avg();
+ }
+
+ public List max() {
+ return weatherMapper.max();
+ }
+
+ public void deleteTemperature(String number) {
+ weatherMapper.deleteTemperature(number);
+ }
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
new file mode 100644
index 0000000..09e783d
--- /dev/null
+++ b/src/main/resources/application.properties
@@ -0,0 +1,18 @@
+# datasource config - JDBC-RESTful
+spring.datasource.driver-class-name=com.taosdata.jdbc.rs.RestfulDriver
+spring.datasource.url=jdbc:TAOS-RS://47.97.5.58:6041/demo?charset=UTF-8&locale=en_US.UTF-8
+spring.datasource.username=root
+spring.datasource.password=taosdata
+
+spring.datasource.druid.initial-size=5
+spring.datasource.druid.min-idle=5
+spring.datasource.druid.max-active=5
+spring.datasource.druid.max-wait=30000
+spring.datasource.druid.validation-query=select server_status();
+
+#mybatis
+#mybatis.mapper-locations=classpath:mapper/*.xml
+
+logging.level.com.taosdata.example.springboot.tdengine.demo.dao=debug
+
+server.port=8081