commit
feb22af676
10 changed files with 633 additions and 0 deletions
@ -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/ |
@ -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 |
||||
<!-- weatherMapper.xml --> |
||||
<update id="createDB" > |
||||
create database if not exists test; |
||||
</update> |
||||
|
||||
<update id="createTable" > |
||||
create table if not exists test.weather(ts timestamp, temperature int, humidity float); |
||||
</update> |
||||
``` |
||||
|
||||
* 插入单条记录 |
||||
```xml |
||||
<!-- weatherMapper.xml --> |
||||
<insert id="insert" parameterType="Weather" > |
||||
insert into test.weather (ts, temperature, humidity) values (now, #{temperature,jdbcType=INTEGER}, #{humidity,jdbcType=FLOAT}) |
||||
</insert> |
||||
``` |
||||
* 插入多条记录 |
||||
```xml |
||||
<!-- weatherMapper.xml --> |
||||
<insert id="batchInsert" parameterType="java.util.List" > |
||||
insert into test.weather (ts, temperature, humidity) values |
||||
<foreach separator=" " collection="list" item="weather" index="index" > |
||||
(now + #{index}a, #{weather.temperature}, #{weather.humidity}) |
||||
</foreach> |
||||
</insert> |
||||
``` |
||||
* 分页查询 |
||||
```xml |
||||
<!-- weatherMapper.xml --> |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
||||
<mapper namespace="WeatherMapper"> |
||||
|
||||
<resultMap id="BaseResultMap" type="Weather"> |
||||
<id column="ts" jdbcType="TIMESTAMP" property="ts" /> |
||||
<result column="temperature" jdbcType="INTEGER" property="temperature" /> |
||||
<result column="humidity" jdbcType="FLOAT" property="humidity" /> |
||||
</resultMap> |
||||
|
||||
<sql id="Base_Column_List"> |
||||
ts, temperature, humidity |
||||
</sql> |
||||
|
||||
<select id="select" resultMap="BaseResultMap"> |
||||
select |
||||
<include refid="Base_Column_List" /> |
||||
from test.weather |
||||
order by ts desc |
||||
<if test="limit != null"> |
||||
limit #{limit,jdbcType=BIGINT} |
||||
</if> |
||||
<if test="offset != null"> |
||||
offset #{offset,jdbcType=BIGINT} |
||||
</if> |
||||
</select> |
||||
</mapper> |
||||
``` |
||||
|
@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-parent</artifactId> |
||||
<version>2.2.1.RELEASE</version> |
||||
<relativePath/> <!-- lookup parent from repository --> |
||||
</parent> |
||||
<groupId>com.taosdata.example</groupId> |
||||
<artifactId>springboot-tdengine-demo</artifactId> |
||||
<version>0.0.1-SNAPSHOT</version> |
||||
<name>springboot-tdengine-demo</name> |
||||
<description>Demo project for using tdengine with Spring Boot</description> |
||||
|
||||
<properties> |
||||
<java.version>1.8</java.version> |
||||
</properties> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-data-jdbc</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-web</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.mybatis.spring.boot</groupId> |
||||
<artifactId>mybatis-spring-boot-starter</artifactId> |
||||
<version>2.1.1</version> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>com.taosdata.jdbc</groupId> |
||||
<artifactId>taos-jdbcdriver</artifactId> |
||||
<version>3.0.0</version> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>com.alibaba</groupId> |
||||
<artifactId>druid-spring-boot-starter</artifactId> |
||||
<version>1.1.17</version> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-devtools</artifactId> |
||||
<scope>runtime</scope> |
||||
<optional>true</optional> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-test</artifactId> |
||||
<scope>test</scope> |
||||
<exclusions> |
||||
<exclusion> |
||||
<groupId>org.junit.vintage</groupId> |
||||
<artifactId>junit-vintage-engine</artifactId> |
||||
</exclusion> |
||||
</exclusions> |
||||
</dependency> |
||||
</dependencies> |
||||
|
||||
<build> |
||||
<resources> |
||||
<resource> |
||||
<directory>src/main/resources</directory> |
||||
<includes> |
||||
<include>**/*.properties</include> |
||||
<include>**/*.xml</include> |
||||
</includes> |
||||
<filtering>true</filtering> |
||||
</resource> |
||||
<resource> |
||||
<directory>src/main/java</directory> |
||||
<includes> |
||||
<include>**/*.properties</include> |
||||
<include>**/*.xml</include> |
||||
</includes> |
||||
</resource> |
||||
|
||||
</resources> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
|
||||
</project> |
@ -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); |
||||
} |
||||
} |
@ -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<Weather> 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<Weather> weatherList) { |
||||
return weatherService.saveBatch(weatherList); |
||||
} |
||||
|
||||
/** |
||||
* 查询数量 |
||||
* @return |
||||
*/ |
||||
@GetMapping("/count") |
||||
public int count() { |
||||
return weatherService.count(); |
||||
} |
||||
|
||||
/** |
||||
* 获取分表名称 |
||||
* @return |
||||
*/ |
||||
@GetMapping("/subTables") |
||||
public List<String> getSubTables() { |
||||
return weatherService.getSubTables(); |
||||
} |
||||
|
||||
/** |
||||
* 获取平均温度 |
||||
* @return |
||||
*/ |
||||
@GetMapping("/avg") |
||||
public List<Weather> avg() { |
||||
return weatherService.avg(); |
||||
} |
||||
|
||||
@GetMapping("/max") |
||||
public List<Weather> max() { |
||||
return weatherService.max(); |
||||
} |
||||
|
||||
@DeleteMapping("/delete/{number}") |
||||
public void delNumber(@PathVariable String number) { |
||||
weatherService.deleteTemperature(number); |
||||
} |
||||
} |
@ -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<Weather> select(@Param("limit") Long limit, @Param("offset") Long offset); |
||||
|
||||
int insert(Weather weather); |
||||
|
||||
int insertBatch(List<Weather> weatherList); |
||||
|
||||
int count(); |
||||
|
||||
List<String> getSubTables(); |
||||
|
||||
List<Weather> avg(); |
||||
|
||||
List<Weather> max(); |
||||
|
||||
void deleteTemperature( @Param("number") String number); |
||||
} |
@ -0,0 +1,108 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
||||
<mapper namespace="com.taosdata.example.springboot.tdengine.demo.dao.WeatherMapper"> |
||||
|
||||
<resultMap id="BaseResultMap" type="com.taosdata.example.springboot.tdengine.demo.domain.Weather"> |
||||
<id column="ts" jdbcType="TIMESTAMP" property="ts"/> |
||||
<result column="temperature" jdbcType="FLOAT" property="temperature"/> |
||||
<result column="humidity" jdbcType="FLOAT" property="humidity"/> |
||||
</resultMap> |
||||
|
||||
<sql id="Base_Column_List"> |
||||
ts, temperature, humidity, location, groupid |
||||
</sql> |
||||
|
||||
<update id="dropDB"> |
||||
drop database if exists demo |
||||
</update> |
||||
|
||||
<update id="createDB"> |
||||
create database if not exists demo |
||||
</update> |
||||
|
||||
<update id="createSuperTable"> |
||||
create table if not exists demo.weather(ts timestamp, temperature float, humidity float) tags(location nchar(64), groupId int) |
||||
</update> |
||||
|
||||
<update id="createTable" parameterType="com.taosdata.example.springboot.tdengine.demo.domain.Weather"> |
||||
create table if not exists demo.t#{groupId} using demo.weather tags(#{location}, #{groupId}) |
||||
</update> |
||||
|
||||
<select id="select" resultMap="BaseResultMap"> |
||||
select |
||||
<include refid="Base_Column_List"/> |
||||
from demo.weather order by ts desc |
||||
<if test="limit != null"> |
||||
limit #{limit,jdbcType=BIGINT} |
||||
</if> |
||||
<if test="offset != null"> |
||||
offset #{offset,jdbcType=BIGINT} |
||||
</if> |
||||
</select> |
||||
|
||||
<insert id="insert" parameterType="com.taosdata.example.springboot.tdengine.demo.domain.Weather"> |
||||
<!--insert into demo.t#{groupId} (ts, temperature, humidity) values (#{ts}, ${temperature}, ${humidity})--> |
||||
insert into demo.t#{groupId} (ts, temperature, humidity) values (now, ${temperature}, ${humidity}) |
||||
</insert> |
||||
|
||||
<!-- <insert id="insertBatch" parameterType="java.util.List">--> |
||||
<!-- insert into demo.t0 (ts, temperature, humidity) values--> |
||||
<!-- <foreach separator=" " collection="list" item="weather" index="index" >--> |
||||
<!-- <!– 参考涛思数据官方文档:https://www.taosdata.com/cn/documentation/taos-sql#data-type--> |
||||
<!-- 数字后面的时间单位可以是 u(微秒)、a(毫秒)、s(秒)、m(分)、h(小时)、d(天)、w(周)--> |
||||
<!-- 在指定降频操作(down sampling)的时间窗口(interval)时,时间单位还可以使用 n(自然月) 和 y(自然年)。–>--> |
||||
<!-- (now + #{index}a, #{weather.temperature}, #{weather.humidity})--> |
||||
<!-- </foreach>--> |
||||
<!-- </insert>--> |
||||
|
||||
<!--用这种写法,直接使用前端传过来的时间戳,报错:uncategorized SQLException; SQL state []; error code [534]; TDengine ERROR (216): Syntax error in SQL; --> |
||||
<!-- <insert id="insertBatch" parameterType="java.util.List">--> |
||||
<!-- insert into demo.t0 (ts, temperature, humidity) values--> |
||||
<!-- <foreach separator=" " collection="list" item="weather" index="index">--> |
||||
<!-- (#{weather.ts}, #{weather.temperature}, #{weather.humidity})--> |
||||
<!-- </foreach>--> |
||||
<!-- </insert>--> |
||||
|
||||
<insert id="insertBatch" parameterType="java.util.List"> |
||||
insert into |
||||
<foreach separator=" " collection="list" item="weather" index="index"> |
||||
demo.t0 values |
||||
(#{weather.ts}, #{weather.temperature}, #{weather.humidity}) |
||||
</foreach> |
||||
</insert> |
||||
|
||||
<select id="getSubTables" resultType="String"> |
||||
select tbname from demo.weather |
||||
</select> |
||||
|
||||
<select id="count" resultType="int"> |
||||
select count(*) from demo.weather |
||||
</select> |
||||
|
||||
<resultMap id="avgResultSet" type="com.taosdata.example.springboot.tdengine.demo.domain.Weather"> |
||||
<id column="ts" jdbcType="TIMESTAMP" property="ts"/> |
||||
<result column="avg(temperature)" jdbcType="FLOAT" property="temperature"/> |
||||
<result column="avg(humidity)" jdbcType="FLOAT" property="humidity"/> |
||||
</resultMap> |
||||
|
||||
<select id="avg" resultMap="avgResultSet"> |
||||
select avg(temperature), avg(humidity)from demo.weather interval(1m) |
||||
</select> |
||||
|
||||
|
||||
<resultMap id="maxResultSet" type="com.taosdata.example.springboot.tdengine.demo.domain.Weather"> |
||||
<id column="ts" jdbcType="TIMESTAMP" property="ts"/> |
||||
<result column="max(temperature)" jdbcType="FLOAT" property="temperature"/> |
||||
<result column="max(humidity)" jdbcType="FLOAT" property="humidity"/> |
||||
</resultMap> |
||||
|
||||
<select id="max" resultMap="maxResultSet"> |
||||
select max(temperature), max(humidity) from demo.weather interval(1m) |
||||
</select> |
||||
|
||||
<delete id="deleteTemperature" > |
||||
delete from demo.weather where ts = #{number} |
||||
</delete> |
||||
|
||||
</mapper> |
@ -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; |
||||
} |
||||
} |
@ -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<Weather> 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<Weather> weatherList) { |
||||
return weatherMapper.insertBatch(weatherList); |
||||
} |
||||
|
||||
public int count() { |
||||
return weatherMapper.count(); |
||||
} |
||||
|
||||
public List<String> getSubTables() { |
||||
return weatherMapper.getSubTables(); |
||||
} |
||||
|
||||
public List<Weather> avg() { |
||||
return weatherMapper.avg(); |
||||
} |
||||
|
||||
public List<Weather> max() { |
||||
return weatherMapper.max(); |
||||
} |
||||
|
||||
public void deleteTemperature(String number) { |
||||
weatherMapper.deleteTemperature(number); |
||||
} |
||||
} |
@ -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 |
Loading…
Reference in new issue