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

TDengined

注解形式添加

  1. 引入依赖
      
      <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>dynamic-datasource-spring-boot-starterartifactId>
        <version>3.5.2version>
      dependency>
      
      
      <dependency>
        <groupId>com.taosdata.jdbcgroupId>
        <artifactId>taos-jdbcdriverartifactId>
        <version>3.1.0version>
      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);
}

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();
    }

你可能感兴趣的:(时序数据库,java,数据库)