多数据源集成时序数据库TDengined+MybatisPlus——个人demo示例

🌸 TDengined 🌸

🌸 注解形式添加

  1. 引入依赖
      <!-- dynamic-datasource 多数据源-->
      <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        <version>3.5.2</version>
      </dependency>
      
      <!--  tdengine 涛思数据 -->
      <dependency>
        <groupId>com.taosdata.jdbc</groupId>
        <artifactId>taos-jdbcdriver</artifactId>
        <version>3.1.0</version>
      </dependency>
  1. 修改配置文件
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    dynamic:
      # 设置默认的数据源或者数据源组,默认值即为 master
      primary: master
      # 严格模式 匹配不到数据源则报错
      strict: true
      datasource:
        # 主库数据源
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: xxxxx
          url: jdbc:mysql://127.0.0.1:3306/ifssc-iot?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai
        # 从库库数据源
        slave:
          driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
          url: jdbc:TAOS-RS://127.0.0.1:6041/power?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8
          username: root
          password: xxxxx 
        # 从库库数据源
        slave2:
          driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
          url: jdbc:TAOS-RS://127.0.0.1:6041/power?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8
          username: root
          password: xxxxx
      druid:
        enable: true
        max-active: 50
        min-idle: 50
        initial-size: 50
        max-wait: 60000
        time-between-eviction-runs-millis: 60000
        validation-query: select server_status()
        test-on-return: false
        test-while-idle: true
        test-on-borrow: false
        async-close-connection-enable: true
        async-init: true
  1. 实体类
@Data
@ApiModel(description = "iot消息表")
public class Temperature implements Serializable {

    private Timestamp ts;
    private float temperature;
    private String location;
    @TableField(value = "tbindex")
    private int tbIndex;
    protected Long id;
    protected Long createBy;
    protected Timestamp createTime;
    protected Long updateBy;
    protected Timestamp updateTime;
    protected String remark;
    @TableField(select = false)
    protected Integer delFlag;

}
  1. mapper
public interface TemperatureMapper extends BaseMapper<Temperature> {

    @Update("CREATE TABLE if not exists temperature(ts timestamp, temperature float) tags(location nchar(64), tbIndex int)")
    int createSuperTable();

    @Update("create table #{tbName} using temperature tags( #{location}, #{tbindex})")
    int createTable(@Param("tbName") String tbName, @Param("location") String location, @Param("tbindex") int tbindex);

    @Update("drop table if exists temperature")
    void dropSuperTable();

    @Insert("insert into t${tbIndex} (ts, temperature) values(#{ts}, #{temperature})")
    int insertOne(Temperature one);

    int insertBatch(List<Temperature> list);
}
<?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.lyzw.cloud.iot.mapper.TemperatureMapper">
    <insert id="insertBatch">
        insert into t8(ts, temperature, id, del_flag)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.ts}, #{item.temperature}, #{item.id}, 0)
        </foreach>
    </insert>
</mapper>
  1. 使用 直接加在使用的方法上即可
    • @Slave
    • @DS(“slave”)
    @Slave
    public ResultVO test() {
        LambdaQueryWrapper<Temperature> lqw = new QueryWrapper<Temperature>().lambda()
                .eq(Temperature::getLocation, "杭州")
                .last("limit 10");
        List<Temperature> temperatureList = temperatureMapper.selectList(lqw);
        return ResultVO.success(temperatureList);
    }
    
    @Slave
    public ResultVO insertBatch() {
            List<Temperature> insertBatch = new ArrayList<>();
            for (int j = 1; j <= 50; j++) {
                Temperature one = new Temperature();
                long l = System.currentTimeMillis() + j * 1000;
                one.setTs(new Timestamp(l));
                Random random = new Random(System.currentTimeMillis());
                one.setTemperature(random.nextFloat() * IdUtil.getSnowflakeNextId());
                one.setId(IdUtil.getSnowflakeNextId());
                one.setDelFlag(0);
                insertBatch.add(one);
            }
            int asd = temperatureMapper.insertBatch(insertBatch);
        return ResultVO.success();
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值