当前位置: 首页 > news >正文

springCloud集成tdengine(原生和mapper方式) 其一

第一种 mapper方式,原生方式在主页看第二章
一、添加pom文件

		<!-- HikariCP 连接池 --><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></dependency><!-- TDengine 连接器--><dependency><groupId>com.taosdata.jdbc</groupId><artifactId>taos-jdbcdriver</artifactId><version>3.5.3</version></dependency>

二、在nacos中配置好数据库连接

spring:datasource:url: jdbc:TAOS://localhost:6030/testusername: rootpassword: yourPassWorddriver-class-name: com.taosdata.jdbc.TSDBDriverhikari:maximum-pool-size: 10 # 连接池最大连接数minimum-idle: 2       # 连接池最小空闲连接数idle-timeout: 30000   # 空闲连接超时时间(毫秒)max-lifetime: 1800000 # 连接最大存活时间(毫秒)connection-timeout: 30000 # 连接超时时间(毫秒)

其中test是你的database,填好你的username和password
三、如果需要,配置好配置类


import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;/*** 如果多数据源, 可以放开这边的config代码*/
@Configuration
public class DataSourceConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource() {return new HikariDataSource();}}

四、写好model类


import lombok.Data;import java.io.Serializable;
import java.util.Date;@Data
public class DeviceModel implements Serializable {// 时间 == 必须private Date createTime;// 设备名字/设备idprivate String deviceId;// 标签值private String ip;// 值private Object value;// 类型  private String valueType;//采集状态 private int status;// 设备名称private String deviceName;// 设备parentNameprivate String deviceParentName;// int类型的value值private Integer intValue;// 布尔类型的value值private Boolean booleanValue;// 小数类型的value值private Float floatValue;private Date ts;}

五、写好mapper接口

import com.apex.iot.device.model.DeviceModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface DeviceMapper {/*** 根据deviceId获取设备最新的值,注意,只返回一个值* @param deviceId 要查询的设备id*/@Select("select ts,int_value as intValue,boolean_value as booleanValue,float_value as floatValue,status,device_id as deviceId,device_name as deviceName,device_parent_name as deviceParentName,ip from device_data_super_table where device_id = #{deviceId} order by ts desc limit 1")DeviceModel getDeviceNewMsgByDeviceId(@Param("deviceId") String deviceId );@Insert({"<script>","INSERT INTO ","<foreach collection='deviceList' item='data' separator=','>","#{data.deviceId} using #{superTable} tags(#{data.deviceId},#{data.deviceName},#{data.deviceParentName},#{data.ip}) values(","#{data.createTime},#{data.intValue},#{data.booleanValue},#{data.floatValue},#{data.status}) ","</foreach>","</script>"})int insertDeviceList(@Param("deviceList")List<DeviceModel> deviceList,@Param("superTable") String superTable);}

六、写service


/*** 数据处理service*/
@Service
@Slf4j
public class DeviceService {//TDengine中的superTable的名字private static final String superTable = "super_table";public void saveToTDengineNew(List<DeviceModel> deviceList){this.deviceMapper.insertDeviceList(deviceList,superTable);}public DeviceModel getDeviceMsgByTDengine(String deviceId) throws Exception {return deviceMapper.getDeviceNewMsgByDeviceId(deviceId);}}

七、处理controller即可。完成。

下班啦,原生方式改天写


http://www.mrgr.cn/news/95092.html

相关文章:

  • SpringBoot对接DeepSeek
  • dify+deepseek联网搜索:免费开源搜索引擎Searxng使用(让你的大模型也拥有联网的功能)
  • Python功能完美的宝库——内置的强大“武器库”builtins
  • 春天遇到了冬天的吻
  • 【Java】Mybatis学习笔记
  • 火星探测发展概述2025.3.20
  • 如何判断 MSF 的 Payload 是 Staged 还是 Stageless(含 Meterpreter 与普通 Shell 对比)
  • scrollIntoView 的behavior都有哪些属性
  • STM32HAL库,解决串口UART中断接收到的第一个字节数据丢失
  • 基于springboot的房屋租赁系统(008)
  • L2TP实验 作业
  • 数学之握手问题
  • 基于单片机控制的电动汽车双闭环调速系统(论文+源码)
  • 微前端 qiankun vite vue3
  • Day20:丑数
  • 爬虫案例-爬取某狗音乐
  • 神经网络中层与层之间的关联
  • C++ 各种map对比
  • C语言的内存函数
  • 动平衡仿真程序设计