目录
- 环境准备
- 整合TDengine 数据源
- 1. 添加驱动依赖
- 2. 添加数据源配置
- 3. 添加Mapper
- 4. 添加建表sql脚本
- 5. Controller
- 测试效果
环境准备
- RuoYi-Vue-Plus v5.1.2
- JDK17
- Maven 3.6.3
- Redis 5.X
- MySQL 5.7+
- TDengine 2.6.0.34 客户端
整合TDengine 数据源
1. 添加驱动依赖
注意: 驱动依赖的版本需要根据官网 说明选择你所安装的 TDengine 对应版本。
<dependency><groupId>com.taosdata.jdbc</groupId><artifactId>taos-jdbcdriver</artifactId><version>2.0.38</version></dependency>
驱动依赖添加在 RuoYi-Vue-Plus
的 ruoyi-admin
模块的 pom
文件中。
2. 添加数据源配置
数据源配置添加在 RuoYi-Vue-Plus
的 ruoyi-admin
模块的 application-dev
文件中,具体配置如下图。
注意: 此处 TDengine
是数据源采用的是 taos原生连接
,并且数据源配置信息没有指定到具体的数据库
。
3. 添加Mapper
DatabaseMapper.java
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import org.apache.ibatis.annotations.Param;import java.util.List;
import java.util.Map;public interface DatabaseMapper {@DS("taos")@InterceptorIgnore(tenantLine = "true")List<Map<String, Object>> showDatabaseList();/*** 建数据库** @param databaseName 数据库名*/@DS("taos")@InterceptorIgnore(tenantLine = "true")void createDatabase(@Param("databaseName") String databaseName);/*** 切换使用指定数据库** @param databaseName 数据库名*/@DS("taos")@InterceptorIgnore(tenantLine = "true")void useDatabase(@Param("databaseName") String databaseName);/*** 根据传入的sql建表** @param sql 建表sql*/@DS("taos")@InterceptorIgnore(tenantLine = "true")void createTableBySql(@Param("sql") String sql);
}
注意:
@DS
指定数据源为application-dev.yml
中配置的TDengine的数据源taos
@InterceptorIgnore(tenantLine = "true")
是忽略Mybatis-Plus 行级租户拦截器插件对SQL的处理。此处不设置忽略的话,在执行建库建表语句时,会抛出net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "create" "CREATE"
的异常。
DatabaseMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dromara.taos.mapper.DatabaseMapper"><select id="showDatabaseList" resultType="java.util.Map">show databases</select><update id="createDatabase">create database if not exists `${databaseName}`</update><update id="useDatabase">use `${databaseName}`</update><update id="createTableBySql">${sql}</update></mapper>
4. 添加建表sql脚本
init.sql示例内容如下:
create table if not exists `demo_user`(`create_time` timestamp,`username` nchar(32),`gender` int,`phone` nchar(20),`login_ip` binary(32)
);CREATE TABLE IF NOT EXISTS `alarm_device_data1` (
`ts` timestamp,
`id` BIGINT,
`project_unique_code` NCHAR(32),
`device_unique_code` NCHAR(32),
`alarm_type` NCHAR(10),
`alarm_record_num` NCHAR(32),
`alarm_desc` NCHAR(255),
`alarm_time` timestamp,
`alarm_threshold` NCHAR(32),
`outlier_value` NCHAR(32),
`numeric_unit` NCHAR(32),
`scene_data` NCHAR(255),
`del_flag` NCHAR(1),
`create_dept` BIGINT,
`create_by` BIGINT,
`create_time` timestamp,
`alarm_pictures` NCHAR(200),
`monitor_device_unique_code` NCHAR(32)
);CREATE TABLE IF NOT EXISTS `bim_drawing1` (
`ts` timestamp,
`id` BIGINT,
`project_unique_code` NCHAR(32),
`bim_drawing_num` NCHAR(32),
`diagram_paper_id` NCHAR(30),
`bim_model_id` NCHAR(30),
`diagram_paper_name` NCHAR(64),
`del_flag` NCHAR(1),
`create_dept` BIGINT,
`create_by` BIGINT,
`create_time` timestamp,
`drawing_unique_code` NCHAR(32)
);
5. Controller
此处省略了service层封装
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.domain.R;
import org.dromara.taos.mapper.DatabaseMapper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;@RequiredArgsConstructor
@RestController
@RequestMapping("/taos/database")
public class TaosDatabaseController {private final DatabaseMapper databaseMapper;/*** 测试建库建表** @return*/@GetMapping("createDatabase")public R<Void> createDatabase(String databaseName) throws IOException {if (StringUtils.isBlank(databaseName)) {return R.fail("数据库名不能为空");}List<Map<String, Object>> list = databaseMapper.showDatabaseList();System.out.println("list-----:" + list);System.out.println("list.size-----:" + list.size());// 创建数据库databaseMapper.createDatabase(databaseName);// 切换到新建的数据库databaseMapper.useDatabase(databaseName);// 建表Resource resource1 = new ClassPathResource("/db/init.sql");String sqlText = IOUtils.toString(resource1.getInputStream(), StandardCharsets.UTF_8);Arrays.stream(sqlText.split(";")).filter(StringUtils::isNotBlank).forEach(sql -> {databaseMapper.createTableBySql(sql);System.out.println("sql-------" + sql);});System.out.println("sqlText-----:" + sqlText);return R.ok();}
}
本地postman接口测试需要在application.yml
文件中设置接口忽略登录授权认证,配置在 security.excludes
列表下。
测试效果
postman
TDengine中数据库以及表
ps:查看工具是 DBeaver 下载指定版本的 驱动即可连接访问 TDengine 时序数据库。