enh(driver): add spring + mybatis type:byte[] example (#20050)
* enh(driver): add spring + mybatis type:byte[] example * doc: add init description * docs: add byte[] description
This commit is contained in:
parent
3e9cc93b9f
commit
0e613ea3f3
|
@ -1,6 +1,13 @@
|
||||||
## TDengine SpringBoot + Mybatis Demo
|
## TDengine SpringBoot + Mybatis Demo
|
||||||
|
|
||||||
## 需要提前创建 test 数据库
|
## 需要提前创建 test 数据库
|
||||||
|
|
||||||
|
```
|
||||||
|
$ taos -s 'create database if not exists test'
|
||||||
|
|
||||||
|
$ curl http://localhost:8080/weather/init
|
||||||
|
```
|
||||||
|
|
||||||
### 配置 application.properties
|
### 配置 application.properties
|
||||||
```properties
|
```properties
|
||||||
# datasource config
|
# datasource config
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
<id column="ts" jdbcType="TIMESTAMP" property="ts"/>
|
<id column="ts" jdbcType="TIMESTAMP" property="ts"/>
|
||||||
<result column="temperature" jdbcType="FLOAT" property="temperature"/>
|
<result column="temperature" jdbcType="FLOAT" property="temperature"/>
|
||||||
<result column="humidity" jdbcType="FLOAT" property="humidity"/>
|
<result column="humidity" jdbcType="FLOAT" property="humidity"/>
|
||||||
|
<result column="bytes" jdbcType="BINARY" property="bytes" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<select id="lastOne" resultType="java.util.Map">
|
<select id="lastOne" resultType="java.util.Map">
|
||||||
|
@ -36,6 +37,11 @@
|
||||||
binary
|
binary
|
||||||
(
|
(
|
||||||
64
|
64
|
||||||
|
),
|
||||||
|
bytes
|
||||||
|
binary
|
||||||
|
(
|
||||||
|
64
|
||||||
)) tags
|
)) tags
|
||||||
(
|
(
|
||||||
location nchar
|
location nchar
|
||||||
|
@ -63,8 +69,8 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<insert id="insert" parameterType="com.taosdata.example.springbootdemo.domain.Weather">
|
<insert id="insert" parameterType="com.taosdata.example.springbootdemo.domain.Weather">
|
||||||
insert into test.t#{groupId} (ts, temperature, humidity, note)
|
insert into test.t#{groupId} (ts, temperature, humidity, note, bytes)
|
||||||
values (#{ts}, ${temperature}, ${humidity}, #{note})
|
values (#{ts}, ${temperature}, ${humidity}, #{note}, #{bytes})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<select id="getSubTables" resultType="String">
|
<select id="getSubTables" resultType="String">
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.taosdata.example.springbootdemo.domain;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
public class Weather {
|
public class Weather {
|
||||||
|
@ -12,6 +13,9 @@ public class Weather {
|
||||||
private Float humidity;
|
private Float humidity;
|
||||||
private String location;
|
private String location;
|
||||||
private String note;
|
private String note;
|
||||||
|
// In rest mode, the byte[] type is not recommended.
|
||||||
|
// UTF-8 is used to encode the byte arrays, that result may affect the SQL correctness
|
||||||
|
private byte[] bytes;
|
||||||
private int groupId;
|
private int groupId;
|
||||||
|
|
||||||
public Weather() {
|
public Weather() {
|
||||||
|
@ -70,4 +74,30 @@ public class Weather {
|
||||||
public void setNote(String note) {
|
public void setNote(String note) {
|
||||||
this.note = note;
|
this.note = note;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes() {
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBytes(byte[] bytes) {
|
||||||
|
this.bytes = bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuilder sb = new StringBuilder("Weather{");
|
||||||
|
sb.append("ts=").append(ts);
|
||||||
|
sb.append(", temperature=").append(temperature);
|
||||||
|
sb.append(", humidity=").append(humidity);
|
||||||
|
sb.append(", location='").append(location).append('\'');
|
||||||
|
sb.append(", note='").append(note).append('\'');
|
||||||
|
sb.append(", bytes -> string=");
|
||||||
|
if (bytes == null) sb.append("null");
|
||||||
|
else {
|
||||||
|
sb.append(new String(bytes, StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
sb.append(", groupId=").append(groupId);
|
||||||
|
sb.append('}');
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.taosdata.example.springbootdemo.domain.Weather;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -30,6 +31,7 @@ public class WeatherService {
|
||||||
weather.setLocation(locations[random.nextInt(locations.length)]);
|
weather.setLocation(locations[random.nextInt(locations.length)]);
|
||||||
weather.setGroupId(i % locations.length);
|
weather.setGroupId(i % locations.length);
|
||||||
weather.setNote("note-" + i);
|
weather.setNote("note-" + i);
|
||||||
|
weather.setBytes(locations[random.nextInt(locations.length)].getBytes(StandardCharsets.UTF_8));
|
||||||
weatherMapper.createTable(weather);
|
weatherMapper.createTable(weather);
|
||||||
count += weatherMapper.insert(weather);
|
count += weatherMapper.insert(weather);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue