5-TDengine集成SpringBoot,MyBatis,MyBatisPlus

简介: 5-TDengine集成SpringBoot,MyBatis,MyBatisPlus

背景


前面的体验中,用到了 taos 的客户端、 RESTful Connector 以及 JDBC-JNI 等连接方式,这次我们体验下更接近实际应用场景的示例: TDengineSpringBootMyBatisMyBatisPlus 等的集成。


官方已经自带了示例,

https://github.com/taosdata/TDengine/tree/develop/tests/examples/JDBC


这些示例在安装的客户端目录也有: /usr/local/taos/examples/JDBC 或者 C:\TDengine\examples\JDBC ,我把这些示例都跑了一遍,大体都可以跑起来,但是也存在一些问题或bug,尤其是一些测试用例不太严谨。我把改过的示例放到了我的GitHub上,修改的地方与说明在README.md中。

image.png


SpringBoot+TDengine+MyBatis


示例内容基本上就是建库、建表、插入数据,具体看代码即可。下面只列一下遇到的问题以及解决方法。


  • 依赖版本与服务端相兼容
<dependency>
    <groupId>com.taosdata.jdbc</groupId>
    <artifactId>taos-jdbcdriver</artifactId>
    <version>2.0.28</version>
</dependency>

修改为:

<dependency>
    <groupId>com.taosdata.jdbc</groupId>
    <artifactId>taos-jdbcdriver</artifactId>
    <version>2.0.28</version>
</dependency>

如果不修改依赖版本,当与服务端版本不一致时,则报错:

Cause: java.sql. SQLException: TDengine ERROR (216): Syntax error in SQL; uncategorized SQLException; SQL state []; error code [534]; TDengine ERROR (216): Syntax error in SQL; nested exception is java.sql. SQLException: TDengine ERROR (216): Syntax error in SQL] with root cause

关于版本兼容性说明,见官方这个表(我的服务端TDengine版本为:2.1.2.0):

image.png

  • TDengine批量插入数据遇到bug


这里用三种方式写了批量插入的语句(这里仅选择一个普通表批量插入,后续实现动态向多个普通表中批量插入数据),但是有一种写法按道理是正确的,但实际就是报错,懵逼一脸,经过调试,应该是个bug。。


第一种:使用后端sql语句中的时间戳作为主键,now+步长

<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>

Note: (now + #{index}a, #{weather.temperature}, #{weather.humidity}) 这样的写法,让我一度怀疑是不是不小心打错了,多打了个字母a。。


第二种:使用前端传参列表中的时间戳作为主键,表名在循环体外,这种方法报错,应该是个bug。。

<!--用这种写法,直接使用前端传过来的时间戳,报错: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>

第一种方法可以,第二种就不行,让人怀疑人生。。然后就跟着调试了一番。

image.png

image.png

image.png

image.png

结果:在批量插入时,第二条记录的时间戳竟然没有自动加上引号,导致报错。。

第三种:使用前端传参列表中的时间戳作为主键,表名在循环体内,批量插入成功~

<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>

附: Postman 里的批量数据传参方式(向后端传递一个数组类型的参数):

curl --location --request POST 'localhost:8080/weather/batch' \
--header 'Content-Type: application/json' \
--data-raw '[
    {
        "ts": 1626324781093,
        "temperature": 30.00000,
        "humidity": 99.00000
    },
    {
        "ts": 1626324981322,
        "temperature": 9.50609,
        "humidity": 10.00000
    }
]'
# 或者(换了传递的时间戳格式):
curl --location --request POST 'localhost:8080/weather/batch' \
--header 'Content-Type: application/json' \
--data-raw '[
    {
        "ts": "2021-07-19 14:53:01.093",
        "temperature": 30.00000,
        "humidity": 99.00000
    },
    {
        "ts": "2021-07-19 14:53:01.200",
        "temperature": 9.50609,
        "humidity": 10.00000
    }
]'

Note: 我用的这个版本,已经不允许往超级表中直接插入数据了,但示例代码自带的README.md中还是这样写的: insert into test.weather (ts, temperature, humidity) values ,总之,文档不太严谨。。


SpringBoot+TDengine+MyBatisPlus


  • 依赖修改


同样,不贴源码了,下面只列一下遇到的问题以及解决方法。


  1. 删除了h2依赖


  1. druid依赖替换为starter
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.17</version>
</dependency>
  1. 升级了taos-jdbcdriver依赖版本
<dependency>
    <groupId>com.taosdata.jdbc</groupId>
    <artifactId>taos-jdbcdriver</artifactId>
    <version>2.0.30</version>
</dependency>
  • 配置修改


user: root 改为: username: root


不过这里改不改都不影响结果,就比较奇怪。。


  • 代码修改


循环生成记录时,时间戳应考虑递增,而不是所有记录使用同一个时间戳,这样会导致覆盖、或者丢弃(依赖于数据库的配置)。


TemperatureMapperTest.java

// insert into table
    int affectRows = 0;
    // insert 10 tables
    for (int i = 0; i < 10; i++) {
        // each table insert 5 rows
        for (int j = 0; j < 5; j++) {
            Temperature one = new Temperature();
            // 如果是插入单条记录,这样写没问题,可这里是在循环体里,TDengine表中时间戳为主键,相同的时间戳会发生覆盖,导致最终插入的记录数量与预期不符
            one.setTs(new Timestamp(1605024000000l));
            one.setTemperature(random.nextFloat() * 50);
            one.setLocation("望京");
            one.setTbIndex(i);
            affectRows += mapper.insertOne(one);
        }
    }
    Assert.assertEquals(50, affectRows);

改为

// insert into table
    int affectRows = 0;
    long ts = System.currentTimeMillis();
    // insert 10 tables
    for (int i = 0; i < 10; i++) {
        // each table insert 5 rows
        for (int j = 0; j < 5; j++) {
            Temperature one = new Temperature();
            // 如果是插入单条记录,这样写没问题,可这里是在循环体里,TDengine表中时间戳为主键,相同的时间戳会发生覆盖,导致最终插入的记录数量与预期不符
//                one.setTs(new Timestamp(1605024000000l));
            Timestamp timestamp = new Timestamp(ts + j);
            one.setTs(timestamp);
            one.setTemperature(random.nextFloat() * 50);
            one.setLocation("望京");
            one.setTbIndex(i);
            affectRows += mapper.insertOne(one);
        }
    }
    Assert.assertEquals(50, affectRows);


Source Code



目录
相关文章
|
6月前
|
SQL 缓存 Java
Mybatis及MybatisPlus
MyBatis 是一款优秀的持久层框架,支持自定义 SQL、存储过程及高级映射。其系统架构通过 mybatis-config.xml 配置全局信息,结合 mapper.xml 映射 SQL 语句,构建 SqlSessionFactory 并创建 SqlSession 操作数据库。MyBatis 底层通过 Executor 执行器和 Mapped Statement 对象实现 SQL 的输入输出映射与执行。支持复杂结果集映射,
|
6月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
360 3
|
7月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
1268 1
Spring boot 使用mybatis generator 自动生成代码插件
|
6月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
610 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
6月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
678 2
|
6月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
379 2
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
131 0
|
6月前
|
存储 人工智能 Java
Springboot集成AI Springboot3 集成阿里云百炼大模型CosyVoice2 实现Ai克隆语音(未持久化存储)
本项目基于Spring Boot 3.5.3与Java 17,集成阿里云百炼大模型CosyVoice2实现音色克隆与语音合成。内容涵盖项目搭建、音色创建、音频合成、音色管理等功能,适用于希望快速掌握Spring Boot集成语音AI技术的开发者。需提前注册阿里云并获取API Key。
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 项目管理
springboot项目集成dolphinscheduler调度器 项目管理
200 0