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

第一种 mapper方式,原生方式在主页看第二章

一、添加pom文件

java 复制代码
		<!-- 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中配置好数据库连接

java 复制代码
spring:
  datasource:
    url: jdbc:TAOS://localhost:6030/test
    username: root
    password: yourPassWord
    driver-class-name: com.taosdata.jdbc.TSDBDriver
    hikari:
      maximum-pool-size: 10 # 连接池最大连接数
      minimum-idle: 2       # 连接池最小空闲连接数
      idle-timeout: 30000   # 空闲连接超时时间(毫秒)
      max-lifetime: 1800000 # 连接最大存活时间(毫秒)
      connection-timeout: 30000 # 连接超时时间(毫秒)

其中test是你的database,填好你的username和password

三、如果需要,配置好配置类

java 复制代码
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类

java 复制代码
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class DeviceModel implements Serializable {

    // 时间 == 必须
    private Date createTime;
    // 设备名字/设备id
    private String deviceId;
    // 标签值
    private String ip;
    // 值
    private Object value;
    // 类型  
    private String valueType;
    //采集状态 
    private int status;
    // 设备名称
    private String deviceName;
    // 设备parentName
    private String deviceParentName;
    
    // int类型的value值
    private Integer intValue;
    // 布尔类型的value值
    private Boolean booleanValue;
    // 小数类型的value值
    private Float floatValue;
    private Date ts;

}

五、写好mapper接口

java 复制代码
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

java 复制代码
/**
 * 数据处理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即可。完成。

下班啦,原生方式改天写

相关推荐
老赵全栈实战1 天前
【每日一技MyBatis trim标签核心用法
java·mybatis·orm
追风筝的人er11 天前
企业管理系统如何实现自定义首页与千人千面?RuoYi Office 给出了完整方案
vue.js·spring boot·spring cloud
TDengine (老段)11 天前
TDengine IDMP 数据可视化——散点图
大数据·数据库·物联网·信息可视化·时序数据库·tdengine·涛思数据
莫寒清11 天前
Mybatis的插件原理
面试·mybatis
莫寒清11 天前
MyBatis 中动态 SQL 的作用
面试·mybatis
吹晚风吧11 天前
实现一个mybatis插件,方便在开发中清楚的看出sql的执行及执行耗时
java·sql·mybatis
码云数智-大飞11 天前
像写 SQL 一样搜索:dbVisitor 如何用 MyBatis 范式颠覆 ElasticSearch 开发
sql·elasticsearch·mybatis
三水不滴11 天前
利用SpringCloud Gateway 重试 + 降级解决第三方接口频繁超时问题,提升性能
经验分享·笔记·后端·spring·spring cloud·gateway
Mr__Miss12 天前
mybatisPlus分页组件3.5.15版本报错解决方案
mybatis
无名-CODING12 天前
MyBatis中#{}和${}完全指南:从原理到实战
mybatis